1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4  *                   Lee Revell <rlrevell@joe-job.com>
5  *                   James Courtier-Dutton <James@superbug.co.uk>
6  *                   Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
7  *                   Creative Labs, Inc.
8  *
9  *  Routines for control of EMU10K1 chips / proc interface routines
10  */
11 
12 #include <linux/slab.h>
13 #include <linux/init.h>
14 #include <sound/core.h>
15 #include <sound/emu10k1.h>
16 #include "p16v.h"
17 
snd_emu10k1_proc_spdif_status(struct snd_emu10k1 * emu,struct snd_info_buffer * buffer,char * title,int status_reg,int rate_reg)18 static void snd_emu10k1_proc_spdif_status(struct snd_emu10k1 * emu,
19 					  struct snd_info_buffer *buffer,
20 					  char *title,
21 					  int status_reg,
22 					  int rate_reg)
23 {
24 	static const char * const clkaccy[4] = { "1000ppm", "50ppm", "variable", "unknown" };
25 	static const int samplerate[16] = { 44100, 1, 48000, 32000, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
26 	static const char * const channel[16] = { "unspec", "left", "right", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" };
27 	static const char * const emphasis[8] = { "none", "50/15 usec 2 channel", "2", "3", "4", "5", "6", "7" };
28 	unsigned int status, rate = 0;
29 
30 	status = snd_emu10k1_ptr_read(emu, status_reg, 0);
31 
32 	snd_iprintf(buffer, "\n%s\n", title);
33 
34 	if (status != 0xffffffff) {
35 		snd_iprintf(buffer, "Professional Mode     : %s\n", (status & SPCS_PROFESSIONAL) ? "yes" : "no");
36 		snd_iprintf(buffer, "Not Audio Data        : %s\n", (status & SPCS_NOTAUDIODATA) ? "yes" : "no");
37 		snd_iprintf(buffer, "Copyright             : %s\n", (status & SPCS_COPYRIGHT) ? "yes" : "no");
38 		snd_iprintf(buffer, "Emphasis              : %s\n", emphasis[(status & SPCS_EMPHASISMASK) >> 3]);
39 		snd_iprintf(buffer, "Mode                  : %i\n", (status & SPCS_MODEMASK) >> 6);
40 		snd_iprintf(buffer, "Category Code         : 0x%x\n", (status & SPCS_CATEGORYCODEMASK) >> 8);
41 		snd_iprintf(buffer, "Generation Status     : %s\n", status & SPCS_GENERATIONSTATUS ? "original" : "copy");
42 		snd_iprintf(buffer, "Source Mask           : %i\n", (status & SPCS_SOURCENUMMASK) >> 16);
43 		snd_iprintf(buffer, "Channel Number        : %s\n", channel[(status & SPCS_CHANNELNUMMASK) >> 20]);
44 		snd_iprintf(buffer, "Sample Rate           : %iHz\n", samplerate[(status & SPCS_SAMPLERATEMASK) >> 24]);
45 		snd_iprintf(buffer, "Clock Accuracy        : %s\n", clkaccy[(status & SPCS_CLKACCYMASK) >> 28]);
46 
47 		if (rate_reg > 0) {
48 			rate = snd_emu10k1_ptr_read(emu, rate_reg, 0);
49 			snd_iprintf(buffer, "S/PDIF Valid          : %s\n", rate & SRCS_SPDIFVALID ? "on" : "off");
50 			snd_iprintf(buffer, "S/PDIF Locked         : %s\n", rate & SRCS_SPDIFLOCKED ? "on" : "off");
51 			snd_iprintf(buffer, "Rate Locked           : %s\n", rate & SRCS_RATELOCKED ? "on" : "off");
52 			/* From ((Rate * 48000 ) / 262144); */
53 			snd_iprintf(buffer, "Estimated Sample Rate : %d\n", ((rate & 0xFFFFF ) * 375) >> 11);
54 		}
55 	} else {
56 		snd_iprintf(buffer, "No signal detected.\n");
57 	}
58 
59 }
60 
snd_emu10k1_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)61 static void snd_emu10k1_proc_read(struct snd_info_entry *entry,
62 				  struct snd_info_buffer *buffer)
63 {
64 	struct snd_emu10k1 *emu = entry->private_data;
65 	const char * const *inputs = emu->audigy ?
66 		snd_emu10k1_audigy_ins : snd_emu10k1_sblive_ins;
67 	const char * const *outputs = emu->audigy ?
68 		snd_emu10k1_audigy_outs : snd_emu10k1_sblive_outs;
69 	unsigned short extin_mask = emu->audigy ? ~0 : emu->fx8010.extin_mask;
70 	unsigned short extout_mask = emu->audigy ? ~0 : emu->fx8010.extout_mask;
71 	unsigned int val, val1, ptrx, psst, dsl, snda;
72 	int nefx = emu->audigy ? 32 : 16;
73 	int idx;
74 
75 	snd_iprintf(buffer, "EMU10K1\n\n");
76 	snd_iprintf(buffer, "Card                  : %s\n",
77 		    emu->card_capabilities->emu_model ? "E-MU D.A.S." :
78 		    emu->card_capabilities->ecard ? "E-MU A.P.S." :
79 		    emu->audigy ? "SB Audigy" : "SB Live!");
80 	snd_iprintf(buffer, "Internal TRAM (words) : 0x%x\n", emu->fx8010.itram_size);
81 	snd_iprintf(buffer, "External TRAM (words) : 0x%x\n", (int)emu->fx8010.etram_pages.bytes / 2);
82 
83 	snd_iprintf(buffer, "\nEffect Send Routing & Amounts:\n");
84 	for (idx = 0; idx < NUM_G; idx++) {
85 		ptrx = snd_emu10k1_ptr_read(emu, PTRX, idx);
86 		psst = snd_emu10k1_ptr_read(emu, PSST, idx);
87 		dsl = snd_emu10k1_ptr_read(emu, DSL, idx);
88 		if (emu->audigy) {
89 			val = snd_emu10k1_ptr_read(emu, A_FXRT1, idx);
90 			val1 = snd_emu10k1_ptr_read(emu, A_FXRT2, idx);
91 			snda = snd_emu10k1_ptr_read(emu, A_SENDAMOUNTS, idx);
92 			snd_iprintf(buffer, "Ch%-2i: A=%2i:%02x, B=%2i:%02x, C=%2i:%02x, D=%2i:%02x, ",
93 				idx,
94 				val & 0x3f, REG_VAL_GET(PTRX_FXSENDAMOUNT_A, ptrx),
95 				(val >> 8) & 0x3f, REG_VAL_GET(PTRX_FXSENDAMOUNT_B, ptrx),
96 				(val >> 16) & 0x3f, REG_VAL_GET(PSST_FXSENDAMOUNT_C, psst),
97 				(val >> 24) & 0x3f, REG_VAL_GET(DSL_FXSENDAMOUNT_D, dsl));
98 			snd_iprintf(buffer, "E=%2i:%02x, F=%2i:%02x, G=%2i:%02x, H=%2i:%02x\n",
99 				val1 & 0x3f, (snda >> 24) & 0xff,
100 				(val1 >> 8) & 0x3f, (snda >> 16) & 0xff,
101 				(val1 >> 16) & 0x3f, (snda >> 8) & 0xff,
102 				(val1 >> 24) & 0x3f, snda & 0xff);
103 		} else {
104 			val = snd_emu10k1_ptr_read(emu, FXRT, idx);
105 			snd_iprintf(buffer, "Ch%-2i: A=%2i:%02x, B=%2i:%02x, C=%2i:%02x, D=%2i:%02x\n",
106 				idx,
107 				(val >> 16) & 0x0f, REG_VAL_GET(PTRX_FXSENDAMOUNT_A, ptrx),
108 				(val >> 20) & 0x0f, REG_VAL_GET(PTRX_FXSENDAMOUNT_B, ptrx),
109 				(val >> 24) & 0x0f, REG_VAL_GET(PSST_FXSENDAMOUNT_C, psst),
110 				(val >> 28) & 0x0f, REG_VAL_GET(DSL_FXSENDAMOUNT_D, dsl));
111 		}
112 	}
113 	snd_iprintf(buffer, "\nEffect Send Targets:\n");
114 	// Audigy actually has 64, but we don't use them all.
115 	for (idx = 0; idx < 32; idx++) {
116 		const char *c = snd_emu10k1_fxbus[idx];
117 		if (c)
118 			snd_iprintf(buffer, "  Channel %02i [%s]\n", idx, c);
119 	}
120 	if (!emu->card_capabilities->emu_model) {
121 		snd_iprintf(buffer, "\nOutput Channels:\n");
122 		for (idx = 0; idx < 32; idx++)
123 			if (outputs[idx] && (extout_mask & (1 << idx)))
124 				snd_iprintf(buffer, "  Channel %02i [%s]\n", idx, outputs[idx]);
125 		snd_iprintf(buffer, "\nInput Channels:\n");
126 		for (idx = 0; idx < 16; idx++)
127 			if (inputs[idx] && (extin_mask & (1 << idx)))
128 				snd_iprintf(buffer, "  Channel %02i [%s]\n", idx, inputs[idx]);
129 		snd_iprintf(buffer, "\nMultichannel Capture Sources:\n");
130 		for (idx = 0; idx < nefx; idx++)
131 			if (emu->efx_voices_mask[0] & (1 << idx))
132 				snd_iprintf(buffer, "  Channel %02i [Output: %s]\n",
133 					    idx, outputs[idx] ? outputs[idx] : "???");
134 		if (emu->audigy) {
135 			for (idx = 0; idx < 32; idx++)
136 				if (emu->efx_voices_mask[1] & (1 << idx))
137 					snd_iprintf(buffer, "  Channel %02i [Input: %s]\n",
138 						    idx + 32, inputs[idx] ? inputs[idx] : "???");
139 		} else {
140 			for (idx = 0; idx < 16; idx++) {
141 				if (emu->efx_voices_mask[0] & ((1 << 16) << idx)) {
142 					if (emu->card_capabilities->sblive51) {
143 						s8 c = snd_emu10k1_sblive51_fxbus2_map[idx];
144 						if (c == -1)
145 							snd_iprintf(buffer, "  Channel %02i [Output: %s]\n",
146 								    idx + 16, outputs[idx + 16]);
147 						else
148 							snd_iprintf(buffer, "  Channel %02i [Input: %s]\n",
149 								    idx + 16, inputs[c]);
150 					} else {
151 						snd_iprintf(buffer, "  Channel %02i [Input: %s]\n",
152 							    idx + 16, inputs[idx] ? inputs[idx] : "???");
153 					}
154 				}
155 			}
156 		}
157 	}
158 }
159 
snd_emu10k1_proc_spdif_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)160 static void snd_emu10k1_proc_spdif_read(struct snd_info_entry *entry,
161 				  struct snd_info_buffer *buffer)
162 {
163 	struct snd_emu10k1 *emu = entry->private_data;
164 	u32 value;
165 	u32 value2;
166 
167 	if (emu->card_capabilities->emu_model) {
168 		snd_emu1010_fpga_lock(emu);
169 
170 		// This represents the S/PDIF lock status on 0404b, which is
171 		// kinda weird and unhelpful, because monitoring it via IRQ is
172 		// impractical (one gets an IRQ flood as long as it is desynced).
173 		snd_emu1010_fpga_read(emu, EMU_HANA_IRQ_STATUS, &value);
174 		snd_iprintf(buffer, "Lock status 1: %#x\n", value & 0x10);
175 
176 		// Bit 0x1 in LO being 0 is supposedly for ADAT lock.
177 		// The registers are always all zero on 0404b.
178 		snd_emu1010_fpga_read(emu, EMU_HANA_LOCK_STS_LO, &value);
179 		snd_emu1010_fpga_read(emu, EMU_HANA_LOCK_STS_HI, &value2);
180 		snd_iprintf(buffer, "Lock status 2: %#x %#x\n", value, value2);
181 
182 		snd_iprintf(buffer, "S/PDIF rate: %dHz\n",
183 			    snd_emu1010_get_raw_rate(emu, EMU_HANA_WCLOCK_HANA_SPDIF_IN));
184 		if (emu->card_capabilities->emu_model != EMU_MODEL_EMU0404) {
185 			snd_iprintf(buffer, "ADAT rate: %dHz\n",
186 				    snd_emu1010_get_raw_rate(emu, EMU_HANA_WCLOCK_HANA_ADAT_IN));
187 			snd_iprintf(buffer, "Dock rate: %dHz\n",
188 				    snd_emu1010_get_raw_rate(emu, EMU_HANA_WCLOCK_2ND_HANA));
189 		}
190 		if (emu->card_capabilities->emu_model == EMU_MODEL_EMU0404 ||
191 		    emu->card_capabilities->emu_model == EMU_MODEL_EMU1010)
192 			snd_iprintf(buffer, "BNC rate: %dHz\n",
193 				    snd_emu1010_get_raw_rate(emu, EMU_HANA_WCLOCK_SYNC_BNC));
194 
195 		snd_emu1010_fpga_read(emu, EMU_HANA_SPDIF_MODE, &value);
196 		if (value & EMU_HANA_SPDIF_MODE_RX_INVALID)
197 			snd_iprintf(buffer, "\nS/PDIF input invalid\n");
198 		else
199 			snd_iprintf(buffer, "\nS/PDIF mode: %s%s\n",
200 				    value & EMU_HANA_SPDIF_MODE_RX_PRO ? "professional" : "consumer",
201 				    value & EMU_HANA_SPDIF_MODE_RX_NOCOPY ? ", no copy" : "");
202 
203 		snd_emu1010_fpga_unlock(emu);
204 	} else {
205 		snd_emu10k1_proc_spdif_status(emu, buffer, "CD-ROM S/PDIF In", CDCS, CDSRCS);
206 		snd_emu10k1_proc_spdif_status(emu, buffer, "Optical or Coax S/PDIF In", GPSCS, GPSRCS);
207 	}
208 #if 0
209 	val = snd_emu10k1_ptr_read(emu, ZVSRCS, 0);
210 	snd_iprintf(buffer, "\nZoomed Video\n");
211 	snd_iprintf(buffer, "Rate Locked           : %s\n", val & SRCS_RATELOCKED ? "on" : "off");
212 	snd_iprintf(buffer, "Estimated Sample Rate : 0x%x\n", val & SRCS_ESTSAMPLERATE);
213 #endif
214 }
215 
snd_emu10k1_proc_rates_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)216 static void snd_emu10k1_proc_rates_read(struct snd_info_entry *entry,
217 				  struct snd_info_buffer *buffer)
218 {
219 	static const int samplerate[8] = { 44100, 48000, 96000, 192000, 4, 5, 6, 7 };
220 	struct snd_emu10k1 *emu = entry->private_data;
221 	unsigned int val, tmp, n;
222 	val = snd_emu10k1_ptr20_read(emu, CAPTURE_RATE_STATUS, 0);
223 	for (n = 0; n < 4; n++) {
224 		tmp = val >> (16 + (n*4));
225 		if (tmp & 0x8) snd_iprintf(buffer, "Channel %d: Rate=%d\n", n, samplerate[tmp & 0x7]);
226 		else snd_iprintf(buffer, "Channel %d: No input\n", n);
227 	}
228 }
229 
230 struct emu10k1_reg_entry {
231 	unsigned short base, size;
232 	const char *name;
233 };
234 
235 static const struct emu10k1_reg_entry sblive_reg_entries[] = {
236 	{    0, 0x10, "FXBUS" },
237 	{ 0x10, 0x10, "EXTIN" },
238 	{ 0x20, 0x10, "EXTOUT" },
239 	{ 0x30, 0x10, "FXBUS2" },
240 	{ 0x40, 0x20, NULL },  // Constants
241 	{ 0x100, 0x100, "GPR" },
242 	{ 0x200, 0x80, "ITRAM_DATA" },
243 	{ 0x280, 0x20, "ETRAM_DATA" },
244 	{ 0x300, 0x80, "ITRAM_ADDR" },
245 	{ 0x380, 0x20, "ETRAM_ADDR" },
246 	{ 0x400, 0, NULL }
247 };
248 
249 static const struct emu10k1_reg_entry audigy_reg_entries[] = {
250 	{    0, 0x40, "FXBUS" },
251 	{ 0x40, 0x10, "EXTIN" },
252 	{ 0x50, 0x10, "P16VIN" },
253 	{ 0x60, 0x20, "EXTOUT" },
254 	{ 0x80, 0x20, "FXBUS2" },
255 	{ 0xa0, 0x10, "EMU32OUTH" },
256 	{ 0xb0, 0x10, "EMU32OUTL" },
257 	{ 0xc0, 0x20, NULL },  // Constants
258 	// This can't be quite right - overlap.
259 	//{ 0x100, 0xc0, "ITRAM_CTL" },
260 	//{ 0x1c0, 0x40, "ETRAM_CTL" },
261 	{ 0x160, 0x20, "A3_EMU32IN" },
262 	{ 0x1e0, 0x20, "A3_EMU32OUT" },
263 	{ 0x200, 0xc0, "ITRAM_DATA" },
264 	{ 0x2c0, 0x40, "ETRAM_DATA" },
265 	{ 0x300, 0xc0, "ITRAM_ADDR" },
266 	{ 0x3c0, 0x40, "ETRAM_ADDR" },
267 	{ 0x400, 0x200, "GPR" },
268 	{ 0x600, 0, NULL }
269 };
270 
271 static const char * const emu10k1_const_entries[] = {
272 	"C_00000000",
273 	"C_00000001",
274 	"C_00000002",
275 	"C_00000003",
276 	"C_00000004",
277 	"C_00000008",
278 	"C_00000010",
279 	"C_00000020",
280 	"C_00000100",
281 	"C_00010000",
282 	"C_00000800",
283 	"C_10000000",
284 	"C_20000000",
285 	"C_40000000",
286 	"C_80000000",
287 	"C_7fffffff",
288 	"C_ffffffff",
289 	"C_fffffffe",
290 	"C_c0000000",
291 	"C_4f1bbcdc",
292 	"C_5a7ef9db",
293 	"C_00100000",
294 	"GPR_ACCU",
295 	"GPR_COND",
296 	"GPR_NOISE0",
297 	"GPR_NOISE1",
298 	"GPR_IRQ",
299 	"GPR_DBAC",
300 	"GPR_DBACE",
301 	"???",
302 };
303 
disasm_emu10k1_reg(char * buffer,const struct emu10k1_reg_entry * entries,unsigned reg,const char * pfx)304 static int disasm_emu10k1_reg(char *buffer,
305 			      const struct emu10k1_reg_entry *entries,
306 			      unsigned reg, const char *pfx)
307 {
308 	for (int i = 0; ; i++) {
309 		unsigned base = entries[i].base;
310 		unsigned size = entries[i].size;
311 		if (!size)
312 			return sprintf(buffer, "%s0x%03x", pfx, reg);
313 		if (reg >= base && reg < base + size) {
314 			const char *name = entries[i].name;
315 			reg -= base;
316 			if (name)
317 				return sprintf(buffer, "%s%s(%u)", pfx, name, reg);
318 			return sprintf(buffer, "%s%s", pfx, emu10k1_const_entries[reg]);
319 		}
320 	}
321 }
322 
disasm_sblive_reg(char * buffer,unsigned reg,const char * pfx)323 static int disasm_sblive_reg(char *buffer, unsigned reg, const char *pfx)
324 {
325 	return disasm_emu10k1_reg(buffer, sblive_reg_entries, reg, pfx);
326 }
327 
disasm_audigy_reg(char * buffer,unsigned reg,const char * pfx)328 static int disasm_audigy_reg(char *buffer, unsigned reg, const char *pfx)
329 {
330 	return disasm_emu10k1_reg(buffer, audigy_reg_entries, reg, pfx);
331 }
332 
snd_emu10k1_proc_acode_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)333 static void snd_emu10k1_proc_acode_read(struct snd_info_entry *entry,
334 				        struct snd_info_buffer *buffer)
335 {
336 	u32 pc;
337 	struct snd_emu10k1 *emu = entry->private_data;
338 	static const char * const insns[16] = {
339 		"MAC0", "MAC1", "MAC2", "MAC3", "MACINT0", "MACINT1", "ACC3", "MACMV",
340 		"ANDXOR", "TSTNEG", "LIMITGE", "LIMITLT", "LOG", "EXP", "INTERP", "SKIP",
341 	};
342 	static const char spaces[] = "                              ";
343 	const int nspaces = sizeof(spaces) - 1;
344 
345 	snd_iprintf(buffer, "FX8010 Instruction List '%s'\n", emu->fx8010.name);
346 	snd_iprintf(buffer, "  Code dump      :\n");
347 	for (pc = 0; pc < (emu->audigy ? 1024 : 512); pc++) {
348 		u32 low, high;
349 		int len;
350 		char buf[100];
351 		char *bufp = buf;
352 
353 		low = snd_emu10k1_efx_read(emu, pc * 2);
354 		high = snd_emu10k1_efx_read(emu, pc * 2 + 1);
355 		if (emu->audigy) {
356 			bufp += sprintf(bufp, "    %-7s  ", insns[(high >> 24) & 0x0f]);
357 			bufp += disasm_audigy_reg(bufp, (high >> 12) & 0x7ff, "");
358 			bufp += disasm_audigy_reg(bufp, (high >> 0) & 0x7ff, ", ");
359 			bufp += disasm_audigy_reg(bufp, (low >> 12) & 0x7ff, ", ");
360 			bufp += disasm_audigy_reg(bufp, (low >> 0) & 0x7ff, ", ");
361 		} else {
362 			bufp += sprintf(bufp, "    %-7s  ", insns[(high >> 20) & 0x0f]);
363 			bufp += disasm_sblive_reg(bufp, (high >> 10) & 0x3ff, "");
364 			bufp += disasm_sblive_reg(bufp, (high >> 0) & 0x3ff, ", ");
365 			bufp += disasm_sblive_reg(bufp, (low >> 10) & 0x3ff, ", ");
366 			bufp += disasm_sblive_reg(bufp, (low >> 0) & 0x3ff, ", ");
367 		}
368 		len = (int)(ptrdiff_t)(bufp - buf);
369 		snd_iprintf(buffer, "%s %s /* 0x%04x: 0x%08x%08x */\n",
370 			    buf, &spaces[nspaces - clamp(65 - len, 0, nspaces)],
371 			    pc, high, low);
372 	}
373 }
374 
375 #define TOTAL_SIZE_GPR		(0x100*4)
376 #define A_TOTAL_SIZE_GPR	(0x200*4)
377 #define TOTAL_SIZE_TANKMEM_DATA	(0xa0*4)
378 #define TOTAL_SIZE_TANKMEM_ADDR (0xa0*4)
379 #define A_TOTAL_SIZE_TANKMEM_DATA (0x100*4)
380 #define A_TOTAL_SIZE_TANKMEM_ADDR (0x100*4)
381 #define TOTAL_SIZE_CODE		(0x200*8)
382 #define A_TOTAL_SIZE_CODE	(0x400*8)
383 
snd_emu10k1_fx8010_read(struct snd_info_entry * entry,void * file_private_data,struct file * file,char __user * buf,size_t count,loff_t pos)384 static ssize_t snd_emu10k1_fx8010_read(struct snd_info_entry *entry,
385 				       void *file_private_data,
386 				       struct file *file, char __user *buf,
387 				       size_t count, loff_t pos)
388 {
389 	struct snd_emu10k1 *emu = entry->private_data;
390 	unsigned int offset;
391 	int tram_addr = 0;
392 	unsigned int *tmp;
393 	long res;
394 	unsigned int idx;
395 
396 	if (!strcmp(entry->name, "fx8010_tram_addr")) {
397 		offset = TANKMEMADDRREGBASE;
398 		tram_addr = 1;
399 	} else if (!strcmp(entry->name, "fx8010_tram_data")) {
400 		offset = TANKMEMDATAREGBASE;
401 	} else if (!strcmp(entry->name, "fx8010_code")) {
402 		offset = emu->audigy ? A_MICROCODEBASE : MICROCODEBASE;
403 	} else {
404 		offset = emu->audigy ? A_FXGPREGBASE : FXGPREGBASE;
405 	}
406 
407 	tmp = kmalloc(count + 8, GFP_KERNEL);
408 	if (!tmp)
409 		return -ENOMEM;
410 	for (idx = 0; idx < ((pos & 3) + count + 3) >> 2; idx++) {
411 		unsigned int val;
412 		val = snd_emu10k1_ptr_read(emu, offset + idx + (pos >> 2), 0);
413 		if (tram_addr && emu->audigy) {
414 			val >>= 11;
415 			val |= snd_emu10k1_ptr_read(emu, 0x100 + idx + (pos >> 2), 0) << 20;
416 		}
417 		tmp[idx] = val;
418 	}
419 	if (copy_to_user(buf, ((char *)tmp) + (pos & 3), count))
420 		res = -EFAULT;
421 	else
422 		res = count;
423 	kfree(tmp);
424 	return res;
425 }
426 
snd_emu10k1_proc_voices_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)427 static void snd_emu10k1_proc_voices_read(struct snd_info_entry *entry,
428 				  struct snd_info_buffer *buffer)
429 {
430 	struct snd_emu10k1 *emu = entry->private_data;
431 	struct snd_emu10k1_voice *voice;
432 	int idx;
433 	static const char * const types[] = {
434 		"Unused", "EFX", "EFX IRQ", "PCM", "PCM IRQ", "Synth"
435 	};
436 	static_assert(ARRAY_SIZE(types) == EMU10K1_NUM_TYPES);
437 
438 	snd_iprintf(buffer, "ch\tdirty\tlast\tuse\n");
439 	for (idx = 0; idx < NUM_G; idx++) {
440 		voice = &emu->voices[idx];
441 		snd_iprintf(buffer, "%i\t%u\t%u\t%s\n",
442 			idx,
443 			voice->dirty,
444 			voice->last,
445 			types[voice->use]);
446 	}
447 }
448 
449 #ifdef CONFIG_SND_DEBUG
450 
snd_emu_proc_emu1010_link_read(struct snd_emu10k1 * emu,struct snd_info_buffer * buffer,u32 dst)451 static void snd_emu_proc_emu1010_link_read(struct snd_emu10k1 *emu,
452 					   struct snd_info_buffer *buffer,
453 					   u32 dst)
454 {
455 	u32 src = snd_emu1010_fpga_link_dst_src_read(emu, dst);
456 	snd_iprintf(buffer, "%04x: %04x\n", dst, src);
457 }
458 
snd_emu_proc_emu1010_reg_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)459 static void snd_emu_proc_emu1010_reg_read(struct snd_info_entry *entry,
460 				     struct snd_info_buffer *buffer)
461 {
462 	struct snd_emu10k1 *emu = entry->private_data;
463 	u32 value;
464 	int i;
465 
466 	snd_emu1010_fpga_lock(emu);
467 
468 	snd_iprintf(buffer, "EMU1010 Registers:\n\n");
469 
470 	for(i = 0; i < 0x40; i+=1) {
471 		snd_emu1010_fpga_read(emu, i, &value);
472 		snd_iprintf(buffer, "%02x: %02x\n", i, value);
473 	}
474 
475 	snd_iprintf(buffer, "\nEMU1010 Routes:\n\n");
476 
477 	for (i = 0; i < 16; i++)  // To Alice2/Tina[2] via EMU32
478 		snd_emu_proc_emu1010_link_read(emu, buffer, i);
479 	if (emu->card_capabilities->emu_model != EMU_MODEL_EMU0404)
480 		for (i = 0; i < 32; i++)  // To Dock via EDI
481 			snd_emu_proc_emu1010_link_read(emu, buffer, 0x100 + i);
482 	if (emu->card_capabilities->emu_model != EMU_MODEL_EMU1616)
483 		for (i = 0; i < 8; i++)  // To Hamoa/local
484 			snd_emu_proc_emu1010_link_read(emu, buffer, 0x200 + i);
485 	for (i = 0; i < 8; i++)  // To Hamoa/Mana/local
486 		snd_emu_proc_emu1010_link_read(emu, buffer, 0x300 + i);
487 	if (emu->card_capabilities->emu_model == EMU_MODEL_EMU1616) {
488 		for (i = 0; i < 16; i++)  // To Tina2 via EMU32
489 			snd_emu_proc_emu1010_link_read(emu, buffer, 0x400 + i);
490 	} else if (emu->card_capabilities->emu_model != EMU_MODEL_EMU0404) {
491 		for (i = 0; i < 8; i++)  // To Hana ADAT
492 			snd_emu_proc_emu1010_link_read(emu, buffer, 0x400 + i);
493 		if (emu->card_capabilities->emu_model == EMU_MODEL_EMU1010B) {
494 			for (i = 0; i < 16; i++)  // To Tina via EMU32
495 				snd_emu_proc_emu1010_link_read(emu, buffer, 0x500 + i);
496 		} else {
497 			// To Alice2 via I2S
498 			snd_emu_proc_emu1010_link_read(emu, buffer, 0x500);
499 			snd_emu_proc_emu1010_link_read(emu, buffer, 0x501);
500 			snd_emu_proc_emu1010_link_read(emu, buffer, 0x600);
501 			snd_emu_proc_emu1010_link_read(emu, buffer, 0x601);
502 			snd_emu_proc_emu1010_link_read(emu, buffer, 0x700);
503 			snd_emu_proc_emu1010_link_read(emu, buffer, 0x701);
504 		}
505 	}
506 
507 	snd_emu1010_fpga_unlock(emu);
508 }
509 
snd_emu_proc_io_reg_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)510 static void snd_emu_proc_io_reg_read(struct snd_info_entry *entry,
511 				     struct snd_info_buffer *buffer)
512 {
513 	struct snd_emu10k1 *emu = entry->private_data;
514 	unsigned long value;
515 	int i;
516 	snd_iprintf(buffer, "IO Registers:\n\n");
517 	for(i = 0; i < 0x40; i+=4) {
518 		value = inl(emu->port + i);
519 		snd_iprintf(buffer, "%02X: %08lX\n", i, value);
520 	}
521 }
522 
snd_emu_proc_io_reg_write(struct snd_info_entry * entry,struct snd_info_buffer * buffer)523 static void snd_emu_proc_io_reg_write(struct snd_info_entry *entry,
524                                       struct snd_info_buffer *buffer)
525 {
526 	struct snd_emu10k1 *emu = entry->private_data;
527 	char line[64];
528 	u32 reg, val;
529 	while (!snd_info_get_line(buffer, line, sizeof(line))) {
530 		if (sscanf(line, "%x %x", &reg, &val) != 2)
531 			continue;
532 		if (reg < 0x40 && val <= 0xffffffff) {
533 			outl(val, emu->port + (reg & 0xfffffffc));
534 		}
535 	}
536 }
537 
snd_ptr_read(struct snd_emu10k1 * emu,unsigned int iobase,unsigned int reg,unsigned int chn)538 static unsigned int snd_ptr_read(struct snd_emu10k1 * emu,
539 				 unsigned int iobase,
540 				 unsigned int reg,
541 				 unsigned int chn)
542 {
543 	unsigned int regptr, val;
544 
545 	regptr = (reg << 16) | chn;
546 
547 	spin_lock_irq(&emu->emu_lock);
548 	outl(regptr, emu->port + iobase + PTR);
549 	val = inl(emu->port + iobase + DATA);
550 	spin_unlock_irq(&emu->emu_lock);
551 	return val;
552 }
553 
snd_ptr_write(struct snd_emu10k1 * emu,unsigned int iobase,unsigned int reg,unsigned int chn,unsigned int data)554 static void snd_ptr_write(struct snd_emu10k1 *emu,
555 			  unsigned int iobase,
556 			  unsigned int reg,
557 			  unsigned int chn,
558 			  unsigned int data)
559 {
560 	unsigned int regptr;
561 
562 	regptr = (reg << 16) | chn;
563 
564 	spin_lock_irq(&emu->emu_lock);
565 	outl(regptr, emu->port + iobase + PTR);
566 	outl(data, emu->port + iobase + DATA);
567 	spin_unlock_irq(&emu->emu_lock);
568 }
569 
570 
snd_emu_proc_ptr_reg_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer,int iobase,int offset,int length,int voices)571 static void snd_emu_proc_ptr_reg_read(struct snd_info_entry *entry,
572 				      struct snd_info_buffer *buffer, int iobase, int offset, int length, int voices)
573 {
574 	struct snd_emu10k1 *emu = entry->private_data;
575 	unsigned long value;
576 	int i,j;
577 	if (offset+length > 0xa0) {
578 		snd_iprintf(buffer, "Input values out of range\n");
579 		return;
580 	}
581 	snd_iprintf(buffer, "Registers 0x%x\n", iobase);
582 	for(i = offset; i < offset+length; i++) {
583 		snd_iprintf(buffer, "%02X: ",i);
584 		for (j = 0; j < voices; j++) {
585 			value = snd_ptr_read(emu, iobase, i, j);
586 			snd_iprintf(buffer, "%08lX ", value);
587 		}
588 		snd_iprintf(buffer, "\n");
589 	}
590 }
591 
snd_emu_proc_ptr_reg_write(struct snd_info_entry * entry,struct snd_info_buffer * buffer,int iobase,int length,int voices)592 static void snd_emu_proc_ptr_reg_write(struct snd_info_entry *entry,
593 				       struct snd_info_buffer *buffer,
594 				       int iobase, int length, int voices)
595 {
596 	struct snd_emu10k1 *emu = entry->private_data;
597 	char line[64];
598 	unsigned int reg, channel_id , val;
599 	while (!snd_info_get_line(buffer, line, sizeof(line))) {
600 		if (sscanf(line, "%x %x %x", &reg, &channel_id, &val) != 3)
601 			continue;
602 		if (reg < length && channel_id < voices)
603 			snd_ptr_write(emu, iobase, reg, channel_id, val);
604 	}
605 }
606 
snd_emu_proc_ptr_reg_write00(struct snd_info_entry * entry,struct snd_info_buffer * buffer)607 static void snd_emu_proc_ptr_reg_write00(struct snd_info_entry *entry,
608 					 struct snd_info_buffer *buffer)
609 {
610 	snd_emu_proc_ptr_reg_write(entry, buffer, 0, 0x80, 64);
611 }
612 
snd_emu_proc_ptr_reg_write20(struct snd_info_entry * entry,struct snd_info_buffer * buffer)613 static void snd_emu_proc_ptr_reg_write20(struct snd_info_entry *entry,
614 					 struct snd_info_buffer *buffer)
615 {
616 	struct snd_emu10k1 *emu = entry->private_data;
617 	snd_emu_proc_ptr_reg_write(entry, buffer, 0x20,
618 				   emu->card_capabilities->ca0108_chip ? 0xa0 : 0x80, 4);
619 }
620 
621 
snd_emu_proc_ptr_reg_read00a(struct snd_info_entry * entry,struct snd_info_buffer * buffer)622 static void snd_emu_proc_ptr_reg_read00a(struct snd_info_entry *entry,
623 					 struct snd_info_buffer *buffer)
624 {
625 	snd_emu_proc_ptr_reg_read(entry, buffer, 0, 0, 0x40, 64);
626 }
627 
snd_emu_proc_ptr_reg_read00b(struct snd_info_entry * entry,struct snd_info_buffer * buffer)628 static void snd_emu_proc_ptr_reg_read00b(struct snd_info_entry *entry,
629 					 struct snd_info_buffer *buffer)
630 {
631 	snd_emu_proc_ptr_reg_read(entry, buffer, 0, 0x40, 0x40, 64);
632 }
633 
snd_emu_proc_ptr_reg_read20a(struct snd_info_entry * entry,struct snd_info_buffer * buffer)634 static void snd_emu_proc_ptr_reg_read20a(struct snd_info_entry *entry,
635 					 struct snd_info_buffer *buffer)
636 {
637 	snd_emu_proc_ptr_reg_read(entry, buffer, 0x20, 0, 0x40, 4);
638 }
639 
snd_emu_proc_ptr_reg_read20b(struct snd_info_entry * entry,struct snd_info_buffer * buffer)640 static void snd_emu_proc_ptr_reg_read20b(struct snd_info_entry *entry,
641 					 struct snd_info_buffer *buffer)
642 {
643 	snd_emu_proc_ptr_reg_read(entry, buffer, 0x20, 0x40, 0x40, 4);
644 }
645 
snd_emu_proc_ptr_reg_read20c(struct snd_info_entry * entry,struct snd_info_buffer * buffer)646 static void snd_emu_proc_ptr_reg_read20c(struct snd_info_entry *entry,
647 					 struct snd_info_buffer * buffer)
648 {
649 	snd_emu_proc_ptr_reg_read(entry, buffer, 0x20, 0x80, 0x20, 4);
650 }
651 #endif
652 
653 static const struct snd_info_entry_ops snd_emu10k1_proc_ops_fx8010 = {
654 	.read = snd_emu10k1_fx8010_read,
655 };
656 
snd_emu10k1_proc_init(struct snd_emu10k1 * emu)657 int snd_emu10k1_proc_init(struct snd_emu10k1 *emu)
658 {
659 	struct snd_info_entry *entry;
660 #ifdef CONFIG_SND_DEBUG
661 	if (emu->card_capabilities->emu_model) {
662 		snd_card_ro_proc_new(emu->card, "emu1010_regs",
663 				     emu, snd_emu_proc_emu1010_reg_read);
664 	}
665 	snd_card_rw_proc_new(emu->card, "io_regs", emu,
666 			     snd_emu_proc_io_reg_read,
667 			     snd_emu_proc_io_reg_write);
668 	snd_card_rw_proc_new(emu->card, "ptr_regs00a", emu,
669 			     snd_emu_proc_ptr_reg_read00a,
670 			     snd_emu_proc_ptr_reg_write00);
671 	snd_card_rw_proc_new(emu->card, "ptr_regs00b", emu,
672 			     snd_emu_proc_ptr_reg_read00b,
673 			     snd_emu_proc_ptr_reg_write00);
674 	if (!emu->card_capabilities->emu_model &&
675 	    (emu->card_capabilities->ca0151_chip || emu->card_capabilities->ca0108_chip)) {
676 		snd_card_rw_proc_new(emu->card, "ptr_regs20a", emu,
677 				     snd_emu_proc_ptr_reg_read20a,
678 				     snd_emu_proc_ptr_reg_write20);
679 		snd_card_rw_proc_new(emu->card, "ptr_regs20b", emu,
680 				     snd_emu_proc_ptr_reg_read20b,
681 				     snd_emu_proc_ptr_reg_write20);
682 		if (emu->card_capabilities->ca0108_chip)
683 			snd_card_rw_proc_new(emu->card, "ptr_regs20c", emu,
684 					     snd_emu_proc_ptr_reg_read20c,
685 					     snd_emu_proc_ptr_reg_write20);
686 	}
687 #endif
688 
689 	snd_card_ro_proc_new(emu->card, "emu10k1", emu, snd_emu10k1_proc_read);
690 
691 	if (emu->card_capabilities->emu10k2_chip)
692 		snd_card_ro_proc_new(emu->card, "spdif-in", emu,
693 				     snd_emu10k1_proc_spdif_read);
694 	if (emu->card_capabilities->ca0151_chip)
695 		snd_card_ro_proc_new(emu->card, "capture-rates", emu,
696 				     snd_emu10k1_proc_rates_read);
697 
698 	snd_card_ro_proc_new(emu->card, "voices", emu,
699 			     snd_emu10k1_proc_voices_read);
700 
701 	if (! snd_card_proc_new(emu->card, "fx8010_gpr", &entry)) {
702 		entry->content = SNDRV_INFO_CONTENT_DATA;
703 		entry->private_data = emu;
704 		entry->mode = S_IFREG | 0444 /*| S_IWUSR*/;
705 		entry->size = emu->audigy ? A_TOTAL_SIZE_GPR : TOTAL_SIZE_GPR;
706 		entry->c.ops = &snd_emu10k1_proc_ops_fx8010;
707 	}
708 	if (! snd_card_proc_new(emu->card, "fx8010_tram_data", &entry)) {
709 		entry->content = SNDRV_INFO_CONTENT_DATA;
710 		entry->private_data = emu;
711 		entry->mode = S_IFREG | 0444 /*| S_IWUSR*/;
712 		entry->size = emu->audigy ? A_TOTAL_SIZE_TANKMEM_DATA : TOTAL_SIZE_TANKMEM_DATA ;
713 		entry->c.ops = &snd_emu10k1_proc_ops_fx8010;
714 	}
715 	if (! snd_card_proc_new(emu->card, "fx8010_tram_addr", &entry)) {
716 		entry->content = SNDRV_INFO_CONTENT_DATA;
717 		entry->private_data = emu;
718 		entry->mode = S_IFREG | 0444 /*| S_IWUSR*/;
719 		entry->size = emu->audigy ? A_TOTAL_SIZE_TANKMEM_ADDR : TOTAL_SIZE_TANKMEM_ADDR ;
720 		entry->c.ops = &snd_emu10k1_proc_ops_fx8010;
721 	}
722 	if (! snd_card_proc_new(emu->card, "fx8010_code", &entry)) {
723 		entry->content = SNDRV_INFO_CONTENT_DATA;
724 		entry->private_data = emu;
725 		entry->mode = S_IFREG | 0444 /*| S_IWUSR*/;
726 		entry->size = emu->audigy ? A_TOTAL_SIZE_CODE : TOTAL_SIZE_CODE;
727 		entry->c.ops = &snd_emu10k1_proc_ops_fx8010;
728 	}
729 	snd_card_ro_proc_new(emu->card, "fx8010_acode", emu,
730 			     snd_emu10k1_proc_acode_read);
731 	return 0;
732 }
733