18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Information interface for ALSA driver
48c2ecf20Sopenharmony_ci *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/slab.h>
88c2ecf20Sopenharmony_ci#include <linux/time.h>
98c2ecf20Sopenharmony_ci#include <linux/string.h>
108c2ecf20Sopenharmony_ci#include <linux/export.h>
118c2ecf20Sopenharmony_ci#include <sound/core.h>
128c2ecf20Sopenharmony_ci#include <sound/minors.h>
138c2ecf20Sopenharmony_ci#include <sound/info.h>
148c2ecf20Sopenharmony_ci#include <linux/utsname.h>
158c2ecf20Sopenharmony_ci#include <linux/mutex.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/*
188c2ecf20Sopenharmony_ci *  OSS compatible part
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(strings);
228c2ecf20Sopenharmony_cistatic char *snd_sndstat_strings[SNDRV_CARDS][SNDRV_OSS_INFO_DEV_COUNT];
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ciint snd_oss_info_register(int dev, int num, char *string)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	char *x;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	if (snd_BUG_ON(dev < 0 || dev >= SNDRV_OSS_INFO_DEV_COUNT))
298c2ecf20Sopenharmony_ci		return -ENXIO;
308c2ecf20Sopenharmony_ci	if (snd_BUG_ON(num < 0 || num >= SNDRV_CARDS))
318c2ecf20Sopenharmony_ci		return -ENXIO;
328c2ecf20Sopenharmony_ci	mutex_lock(&strings);
338c2ecf20Sopenharmony_ci	if (string == NULL) {
348c2ecf20Sopenharmony_ci		if ((x = snd_sndstat_strings[num][dev]) != NULL) {
358c2ecf20Sopenharmony_ci			kfree(x);
368c2ecf20Sopenharmony_ci			x = NULL;
378c2ecf20Sopenharmony_ci		}
388c2ecf20Sopenharmony_ci	} else {
398c2ecf20Sopenharmony_ci		x = kstrdup(string, GFP_KERNEL);
408c2ecf20Sopenharmony_ci		if (x == NULL) {
418c2ecf20Sopenharmony_ci			mutex_unlock(&strings);
428c2ecf20Sopenharmony_ci			return -ENOMEM;
438c2ecf20Sopenharmony_ci		}
448c2ecf20Sopenharmony_ci	}
458c2ecf20Sopenharmony_ci	snd_sndstat_strings[num][dev] = x;
468c2ecf20Sopenharmony_ci	mutex_unlock(&strings);
478c2ecf20Sopenharmony_ci	return 0;
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_oss_info_register);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic int snd_sndstat_show_strings(struct snd_info_buffer *buf, char *id, int dev)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	int idx, ok = -1;
548c2ecf20Sopenharmony_ci	char *str;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	snd_iprintf(buf, "\n%s:", id);
578c2ecf20Sopenharmony_ci	mutex_lock(&strings);
588c2ecf20Sopenharmony_ci	for (idx = 0; idx < SNDRV_CARDS; idx++) {
598c2ecf20Sopenharmony_ci		str = snd_sndstat_strings[idx][dev];
608c2ecf20Sopenharmony_ci		if (str) {
618c2ecf20Sopenharmony_ci			if (ok < 0) {
628c2ecf20Sopenharmony_ci				snd_iprintf(buf, "\n");
638c2ecf20Sopenharmony_ci				ok++;
648c2ecf20Sopenharmony_ci			}
658c2ecf20Sopenharmony_ci			snd_iprintf(buf, "%i: %s\n", idx, str);
668c2ecf20Sopenharmony_ci		}
678c2ecf20Sopenharmony_ci	}
688c2ecf20Sopenharmony_ci	mutex_unlock(&strings);
698c2ecf20Sopenharmony_ci	if (ok < 0)
708c2ecf20Sopenharmony_ci		snd_iprintf(buf, " NOT ENABLED IN CONFIG\n");
718c2ecf20Sopenharmony_ci	return ok;
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic void snd_sndstat_proc_read(struct snd_info_entry *entry,
758c2ecf20Sopenharmony_ci				  struct snd_info_buffer *buffer)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "Sound Driver:3.8.1a-980706 (ALSA emulation code)\n");
788c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "Kernel: %s %s %s %s %s\n",
798c2ecf20Sopenharmony_ci		    init_utsname()->sysname,
808c2ecf20Sopenharmony_ci		    init_utsname()->nodename,
818c2ecf20Sopenharmony_ci		    init_utsname()->release,
828c2ecf20Sopenharmony_ci		    init_utsname()->version,
838c2ecf20Sopenharmony_ci		    init_utsname()->machine);
848c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "Config options: 0\n");
858c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "\nInstalled drivers: \n");
868c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "Type 10: ALSA emulation\n");
878c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "\nCard config: \n");
888c2ecf20Sopenharmony_ci	snd_card_info_read_oss(buffer);
898c2ecf20Sopenharmony_ci	snd_sndstat_show_strings(buffer, "Audio devices", SNDRV_OSS_INFO_DEV_AUDIO);
908c2ecf20Sopenharmony_ci	snd_sndstat_show_strings(buffer, "Synth devices", SNDRV_OSS_INFO_DEV_SYNTH);
918c2ecf20Sopenharmony_ci	snd_sndstat_show_strings(buffer, "Midi devices", SNDRV_OSS_INFO_DEV_MIDI);
928c2ecf20Sopenharmony_ci	snd_sndstat_show_strings(buffer, "Timers", SNDRV_OSS_INFO_DEV_TIMERS);
938c2ecf20Sopenharmony_ci	snd_sndstat_show_strings(buffer, "Mixers", SNDRV_OSS_INFO_DEV_MIXERS);
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ciint __init snd_info_minor_register(void)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	struct snd_info_entry *entry;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	memset(snd_sndstat_strings, 0, sizeof(snd_sndstat_strings));
1018c2ecf20Sopenharmony_ci	entry = snd_info_create_module_entry(THIS_MODULE, "sndstat",
1028c2ecf20Sopenharmony_ci					     snd_oss_root);
1038c2ecf20Sopenharmony_ci	if (!entry)
1048c2ecf20Sopenharmony_ci		return -ENOMEM;
1058c2ecf20Sopenharmony_ci	entry->c.text.read = snd_sndstat_proc_read;
1068c2ecf20Sopenharmony_ci	return snd_info_register(entry); /* freed in error path */
1078c2ecf20Sopenharmony_ci}
108