1/*
2 * fireworks_proc.c - a part of driver for Fireworks based devices
3 *
4 * Copyright (c) 2009-2010 Clemens Ladisch
5 * Copyright (c) 2013-2014 Takashi Sakamoto
6 *
7 * Licensed under the terms of the GNU General Public License, version 2.
8 */
9
10#include "./fireworks.h"
11
12static inline const char*
13get_phys_name(struct snd_efw_phys_grp *grp, bool input)
14{
15	const char *const ch_type[] = {
16		"Analog", "S/PDIF", "ADAT", "S/PDIF or ADAT", "Mirroring",
17		"Headphones", "I2S", "Guitar", "Pirzo Guitar", "Guitar String",
18	};
19
20	if (grp->type < ARRAY_SIZE(ch_type))
21		return ch_type[grp->type];
22	else if (input)
23		return "Input";
24	else
25		return "Output";
26}
27
28static void
29proc_read_hwinfo(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
30{
31	struct snd_efw *efw = entry->private_data;
32	unsigned short i;
33	struct snd_efw_hwinfo *hwinfo;
34
35	hwinfo = kmalloc(sizeof(struct snd_efw_hwinfo), GFP_KERNEL);
36	if (hwinfo == NULL)
37		return;
38
39	if (snd_efw_command_get_hwinfo(efw, hwinfo) < 0)
40		goto end;
41
42	snd_iprintf(buffer, "guid_hi: 0x%X\n", hwinfo->guid_hi);
43	snd_iprintf(buffer, "guid_lo: 0x%X\n", hwinfo->guid_lo);
44	snd_iprintf(buffer, "type: 0x%X\n", hwinfo->type);
45	snd_iprintf(buffer, "version: 0x%X\n", hwinfo->version);
46	snd_iprintf(buffer, "vendor_name: %s\n", hwinfo->vendor_name);
47	snd_iprintf(buffer, "model_name: %s\n", hwinfo->model_name);
48
49	snd_iprintf(buffer, "dsp_version: 0x%X\n", hwinfo->dsp_version);
50	snd_iprintf(buffer, "arm_version: 0x%X\n", hwinfo->arm_version);
51	snd_iprintf(buffer, "fpga_version: 0x%X\n", hwinfo->fpga_version);
52
53	snd_iprintf(buffer, "flags: 0x%X\n", hwinfo->flags);
54
55	snd_iprintf(buffer, "max_sample_rate: 0x%X\n", hwinfo->max_sample_rate);
56	snd_iprintf(buffer, "min_sample_rate: 0x%X\n", hwinfo->min_sample_rate);
57	snd_iprintf(buffer, "supported_clock: 0x%X\n",
58		    hwinfo->supported_clocks);
59
60	snd_iprintf(buffer, "phys out: 0x%X\n", hwinfo->phys_out);
61	snd_iprintf(buffer, "phys in: 0x%X\n", hwinfo->phys_in);
62
63	snd_iprintf(buffer, "phys in grps: 0x%X\n",
64		    hwinfo->phys_in_grp_count);
65	for (i = 0; i < hwinfo->phys_in_grp_count; i++) {
66		snd_iprintf(buffer,
67			    "phys in grp[%d]: type 0x%X, count 0x%X\n",
68			    i, hwinfo->phys_out_grps[i].type,
69			    hwinfo->phys_out_grps[i].count);
70	}
71
72	snd_iprintf(buffer, "phys out grps: 0x%X\n",
73		    hwinfo->phys_out_grp_count);
74	for (i = 0; i < hwinfo->phys_out_grp_count; i++) {
75		snd_iprintf(buffer,
76			    "phys out grps[%d]: type 0x%X, count 0x%X\n",
77			    i, hwinfo->phys_out_grps[i].type,
78			    hwinfo->phys_out_grps[i].count);
79	}
80
81	snd_iprintf(buffer, "amdtp rx pcm channels 1x: 0x%X\n",
82		    hwinfo->amdtp_rx_pcm_channels);
83	snd_iprintf(buffer, "amdtp tx pcm channels 1x: 0x%X\n",
84		    hwinfo->amdtp_tx_pcm_channels);
85	snd_iprintf(buffer, "amdtp rx pcm channels 2x: 0x%X\n",
86		    hwinfo->amdtp_rx_pcm_channels_2x);
87	snd_iprintf(buffer, "amdtp tx pcm channels 2x: 0x%X\n",
88		    hwinfo->amdtp_tx_pcm_channels_2x);
89	snd_iprintf(buffer, "amdtp rx pcm channels 4x: 0x%X\n",
90		    hwinfo->amdtp_rx_pcm_channels_4x);
91	snd_iprintf(buffer, "amdtp tx pcm channels 4x: 0x%X\n",
92		    hwinfo->amdtp_tx_pcm_channels_4x);
93
94	snd_iprintf(buffer, "midi out ports: 0x%X\n", hwinfo->midi_out_ports);
95	snd_iprintf(buffer, "midi in ports: 0x%X\n", hwinfo->midi_in_ports);
96
97	snd_iprintf(buffer, "mixer playback channels: 0x%X\n",
98		    hwinfo->mixer_playback_channels);
99	snd_iprintf(buffer, "mixer capture channels: 0x%X\n",
100		    hwinfo->mixer_capture_channels);
101end:
102	kfree(hwinfo);
103}
104
105static void
106proc_read_clock(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
107{
108	struct snd_efw *efw = entry->private_data;
109	enum snd_efw_clock_source clock_source;
110	unsigned int sampling_rate;
111
112	if (snd_efw_command_get_clock_source(efw, &clock_source) < 0)
113		return;
114
115	if (snd_efw_command_get_sampling_rate(efw, &sampling_rate) < 0)
116		return;
117
118	snd_iprintf(buffer, "Clock Source: %d\n", clock_source);
119	snd_iprintf(buffer, "Sampling Rate: %d\n", sampling_rate);
120}
121
122/*
123 * NOTE:
124 *  dB = 20 * log10(linear / 0x01000000)
125 *  -144.0 dB when linear is 0
126 */
127static void
128proc_read_phys_meters(struct snd_info_entry *entry,
129		      struct snd_info_buffer *buffer)
130{
131	struct snd_efw *efw = entry->private_data;
132	struct snd_efw_phys_meters *meters;
133	unsigned int g, c, m, max, size;
134	const char *name;
135	u32 *linear;
136	int err;
137
138	size = sizeof(struct snd_efw_phys_meters) +
139	       (efw->phys_in + efw->phys_out) * sizeof(u32);
140	meters = kzalloc(size, GFP_KERNEL);
141	if (meters == NULL)
142		return;
143
144	err = snd_efw_command_get_phys_meters(efw, meters, size);
145	if (err < 0)
146		goto end;
147
148	snd_iprintf(buffer, "Physical Meters:\n");
149
150	m = 0;
151	max = min(efw->phys_out, meters->out_meters);
152	linear = meters->values;
153	snd_iprintf(buffer, " %d Outputs:\n", max);
154	for (g = 0; g < efw->phys_out_grp_count; g++) {
155		name = get_phys_name(&efw->phys_out_grps[g], false);
156		for (c = 0; c < efw->phys_out_grps[g].count; c++) {
157			if (m < max)
158				snd_iprintf(buffer, "\t%s [%d]: %d\n",
159					    name, c, linear[m++]);
160		}
161	}
162
163	m = 0;
164	max = min(efw->phys_in, meters->in_meters);
165	linear = meters->values + meters->out_meters;
166	snd_iprintf(buffer, " %d Inputs:\n", max);
167	for (g = 0; g < efw->phys_in_grp_count; g++) {
168		name = get_phys_name(&efw->phys_in_grps[g], true);
169		for (c = 0; c < efw->phys_in_grps[g].count; c++)
170			if (m < max)
171				snd_iprintf(buffer, "\t%s [%d]: %d\n",
172					    name, c, linear[m++]);
173	}
174end:
175	kfree(meters);
176}
177
178static void
179proc_read_queues_state(struct snd_info_entry *entry,
180		       struct snd_info_buffer *buffer)
181{
182	struct snd_efw *efw = entry->private_data;
183	unsigned int consumed;
184
185	if (efw->pull_ptr > efw->push_ptr)
186		consumed = snd_efw_resp_buf_size -
187			   (unsigned int)(efw->pull_ptr - efw->push_ptr);
188	else
189		consumed = (unsigned int)(efw->push_ptr - efw->pull_ptr);
190
191	snd_iprintf(buffer, "%d %d/%d\n",
192		    efw->resp_queues, consumed, snd_efw_resp_buf_size);
193}
194
195static void
196add_node(struct snd_efw *efw, struct snd_info_entry *root, const char *name,
197	 void (*op)(struct snd_info_entry *e, struct snd_info_buffer *b))
198{
199	struct snd_info_entry *entry;
200
201	entry = snd_info_create_card_entry(efw->card, name, root);
202	if (entry == NULL)
203		return;
204
205	snd_info_set_text_ops(entry, efw, op);
206	if (snd_info_register(entry) < 0)
207		snd_info_free_entry(entry);
208}
209
210void snd_efw_proc_init(struct snd_efw *efw)
211{
212	struct snd_info_entry *root;
213
214	/*
215	 * All nodes are automatically removed at snd_card_disconnect(),
216	 * by following to link list.
217	 */
218	root = snd_info_create_card_entry(efw->card, "firewire",
219					  efw->card->proc_root);
220	if (root == NULL)
221		return;
222	root->mode = S_IFDIR | S_IRUGO | S_IXUGO;
223	if (snd_info_register(root) < 0) {
224		snd_info_free_entry(root);
225		return;
226	}
227
228	add_node(efw, root, "clock", proc_read_clock);
229	add_node(efw, root, "firmware", proc_read_hwinfo);
230	add_node(efw, root, "meters", proc_read_phys_meters);
231	add_node(efw, root, "queues", proc_read_queues_state);
232}
233