blob: 8c1275f0fcbd7fd487077ea0bf1fadcc02771fcc [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449int __init snd_info_init(void)
450{
451 struct proc_dir_entry *p;
452
Al Viroe55d92b2011-07-24 02:07:46 -0400453 p = proc_mkdir("asound", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (p == NULL)
455 return -ENOMEM;
456 snd_proc_root = p;
457#ifdef CONFIG_SND_OSSEMUL
458 {
Takashi Iwai24c1f932005-11-17 13:58:48 +0100459 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL)
461 return -ENOMEM;
462 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
463 if (snd_info_register(entry) < 0) {
464 snd_info_free_entry(entry);
465 return -ENOMEM;
466 }
467 snd_oss_root = entry;
468 }
469#endif
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100470#if IS_ENABLED(CONFIG_SND_SEQUENCER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 {
Takashi Iwai24c1f932005-11-17 13:58:48 +0100472 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL)
474 return -ENOMEM;
475 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
476 if (snd_info_register(entry) < 0) {
477 snd_info_free_entry(entry);
478 return -ENOMEM;
479 }
480 snd_seq_root = entry;
481 }
482#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;
488}
489
490int __exit snd_info_done(void)
491{
492 snd_card_info_done();
493 snd_minor_info_oss_done();
494 snd_minor_info_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 snd_info_version_done();
496 if (snd_proc_root) {
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100497#if IS_ENABLED(CONFIG_SND_SEQUENCER)
Takashi Iwai746d4a02006-06-23 14:37:59 +0200498 snd_info_free_entry(snd_seq_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499#endif
500#ifdef CONFIG_SND_OSSEMUL
Takashi Iwai746d4a02006-06-23 14:37:59 +0200501 snd_info_free_entry(snd_oss_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502#endif
David Howellsa8ca16e2013-04-12 17:27:28 +0100503 proc_remove(snd_proc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505 return 0;
506}
507
508/*
509
510 */
511
512
513/*
514 * create a card proc file
515 * called from init.c
516 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100517int snd_info_card_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 char str[8];
Takashi Iwai24c1f932005-11-17 13:58:48 +0100520 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200522 if (snd_BUG_ON(!card))
523 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 sprintf(str, "card%i", card->number);
526 if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
527 return -ENOMEM;
528 entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
529 if (snd_info_register(entry) < 0) {
530 snd_info_free_entry(entry);
531 return -ENOMEM;
532 }
533 card->proc_root = entry;
534 return 0;
535}
536
537/*
538 * register the card proc file
539 * called from init.c
540 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100541int snd_info_card_register(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 struct proc_dir_entry *p;
544
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200545 if (snd_BUG_ON(!card))
546 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 if (!strcmp(card->id, card->proc_root->name))
549 return 0;
550
551 p = proc_symlink(card->id, snd_proc_root, card->proc_root->name);
552 if (p == NULL)
553 return -ENOMEM;
554 card->proc_root_link = p;
555 return 0;
556}
557
558/*
Jaroslav Kyselac2eb9c42008-11-12 16:31:37 +0100559 * called on card->id change
560 */
561void snd_info_card_id_change(struct snd_card *card)
562{
563 mutex_lock(&info_mutex);
564 if (card->proc_root_link) {
David Howellsa8ca16e2013-04-12 17:27:28 +0100565 proc_remove(card->proc_root_link);
Jaroslav Kyselac2eb9c42008-11-12 16:31:37 +0100566 card->proc_root_link = NULL;
567 }
568 if (strcmp(card->id, card->proc_root->name))
569 card->proc_root_link = proc_symlink(card->id,
570 snd_proc_root,
571 card->proc_root->name);
572 mutex_unlock(&info_mutex);
573}
574
575/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 * de-register the card proc file
577 * called from init.c
578 */
Takashi Iwai746d4a02006-06-23 14:37:59 +0200579void snd_info_card_disconnect(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200581 if (!card)
582 return;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200583 mutex_lock(&info_mutex);
David Howellsa8ca16e2013-04-12 17:27:28 +0100584 proc_remove(card->proc_root_link);
585 card->proc_root_link = NULL;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200586 if (card->proc_root)
587 snd_info_disconnect(card->proc_root);
588 mutex_unlock(&info_mutex);
589}
590
591/*
592 * release the card proc file resources
593 * called from init.c
594 */
595int snd_info_card_free(struct snd_card *card)
596{
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200597 if (!card)
598 return 0;
Takashi Iwai746d4a02006-06-23 14:37:59 +0200599 snd_info_free_entry(card->proc_root);
600 card->proc_root = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
602}
603
604
605/**
606 * snd_info_get_line - read one line from the procfs buffer
607 * @buffer: the procfs buffer
608 * @line: the buffer to store
Clemens Ladischddc64b22014-08-21 20:55:21 +0200609 * @len: the max. buffer size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 *
611 * Reads one line from the buffer and stores the string.
612 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100613 * Return: Zero if successful, or 1 if error or EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100615int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 int c = -1;
618
Takashi Iwai0bc0ec92013-03-13 12:11:13 +0100619 if (snd_BUG_ON(!buffer || !buffer->buffer))
620 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (len <= 0 || buffer->stop || buffer->error)
622 return 1;
Takashi Iwai0bc0ec92013-03-13 12:11:13 +0100623 while (!buffer->stop) {
Takashi Iwai7e4eeec2006-04-28 15:13:40 +0200624 c = buffer->buffer[buffer->curr++];
625 if (buffer->curr >= buffer->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 buffer->stop = 1;
Takashi Iwai0bc0ec92013-03-13 12:11:13 +0100627 if (c == '\n')
628 break;
Clemens Ladischddc64b22014-08-21 20:55:21 +0200629 if (len > 1) {
Takashi Iwai0bc0ec92013-03-13 12:11:13 +0100630 len--;
631 *line++ = c;
632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634 *line = '\0';
635 return 0;
636}
637
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200638EXPORT_SYMBOL(snd_info_get_line);
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640/**
Henrik Kretzschmar856def82005-07-08 13:53:42 +0200641 * snd_info_get_str - parse a string token
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * @dest: the buffer to store the string token
643 * @src: the original string
644 * @len: the max. length of token - 1
645 *
646 * Parses the original string and copy a token to the given
647 * string buffer.
648 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100649 * Return: The updated pointer of the original string so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 * it can be used for the next call.
651 */
Takashi Iwai4f7454a2009-09-08 14:29:58 +0200652const char *snd_info_get_str(char *dest, const char *src, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 int c;
655
656 while (*src == ' ' || *src == '\t')
657 src++;
658 if (*src == '"' || *src == '\'') {
659 c = *src++;
660 while (--len > 0 && *src && *src != c) {
661 *dest++ = *src++;
662 }
663 if (*src == c)
664 src++;
665 } else {
666 while (--len > 0 && *src && *src != ' ' && *src != '\t') {
667 *dest++ = *src++;
668 }
669 }
670 *dest = 0;
671 while (*src == ' ' || *src == '\t')
672 src++;
673 return src;
674}
675
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200676EXPORT_SYMBOL(snd_info_get_str);
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678/**
679 * snd_info_create_entry - create an info entry
680 * @name: the proc file name
681 *
682 * Creates an info entry with the given file name and initializes as
683 * the default state.
684 *
685 * Usually called from other functions such as
686 * snd_info_create_card_entry().
687 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100688 * Return: The pointer of the new instance, or %NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100690static struct snd_info_entry *snd_info_create_entry(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100692 struct snd_info_entry *entry;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200693 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (entry == NULL)
695 return NULL;
Paulo Marques543537b2005-06-23 00:09:02 -0700696 entry->name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (entry->name == NULL) {
698 kfree(entry);
699 return NULL;
700 }
701 entry->mode = S_IFREG | S_IRUGO;
702 entry->content = SNDRV_INFO_CONTENT_TEXT;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +0100703 mutex_init(&entry->access);
Takashi Iwai746d4a02006-06-23 14:37:59 +0200704 INIT_LIST_HEAD(&entry->children);
705 INIT_LIST_HEAD(&entry->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return entry;
707}
708
709/**
710 * snd_info_create_module_entry - create an info entry for the given module
711 * @module: the module pointer
712 * @name: the file name
713 * @parent: the parent directory
714 *
715 * Creates a new info entry and assigns it to the given module.
716 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100717 * Return: The pointer of the new instance, or %NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100719struct snd_info_entry *snd_info_create_module_entry(struct module * module,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 const char *name,
Takashi Iwai24c1f932005-11-17 13:58:48 +0100721 struct snd_info_entry *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100723 struct snd_info_entry *entry = snd_info_create_entry(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (entry) {
725 entry->module = module;
726 entry->parent = parent;
727 }
728 return entry;
729}
730
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200731EXPORT_SYMBOL(snd_info_create_module_entry);
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/**
734 * snd_info_create_card_entry - create an info entry for the given card
735 * @card: the card instance
736 * @name: the file name
737 * @parent: the parent directory
738 *
739 * Creates a new info entry and assigns it to the given card.
740 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100741 * Return: The pointer of the new instance, or %NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 */
Takashi Iwai24c1f932005-11-17 13:58:48 +0100743struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 const char *name,
Takashi Iwai24c1f932005-11-17 13:58:48 +0100745 struct snd_info_entry * parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Takashi Iwai24c1f932005-11-17 13:58:48 +0100747 struct snd_info_entry *entry = snd_info_create_entry(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (entry) {
749 entry->module = card->module;
750 entry->card = card;
751 entry->parent = parent;
752 }
753 return entry;
754}
755
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200756EXPORT_SYMBOL(snd_info_create_card_entry);
757
Takashi Iwai746d4a02006-06-23 14:37:59 +0200758static void snd_info_disconnect(struct snd_info_entry *entry)
759{
760 struct list_head *p, *n;
761 struct proc_dir_entry *root;
762
763 list_for_each_safe(p, n, &entry->children) {
764 snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
765 }
766
767 if (! entry->p)
768 return;
769 list_del_init(&entry->list);
770 root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200771 snd_BUG_ON(!root);
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 */