ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / pci / emu10k1 / emuproc.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *                   Creative Labs, Inc.
4  *  Routines for control of EMU10K1 chips / proc interface routines
5  *
6  *  BUGS:
7  *    --
8  *
9  *  TODO:
10  *    --
11  *
12  *   This program is free software; you can redistribute it and/or modify
13  *   it under the terms of the GNU General Public License as published by
14  *   the Free Software Foundation; either version 2 of the License, or
15  *   (at your option) any later version.
16  *
17  *   This program is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with this program; if not, write to the Free Software
24  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25  *
26  */
27
28 #include <sound/driver.h>
29 #include <linux/slab.h>
30 #include <linux/init.h>
31 #include <sound/core.h>
32 #include <sound/emu10k1.h>
33
34 static void snd_emu10k1_proc_spdif_status(emu10k1_t * emu,
35                                           snd_info_buffer_t * buffer,
36                                           char *title,
37                                           int status_reg,
38                                           int rate_reg)
39 {
40         static char *clkaccy[4] = { "1000ppm", "50ppm", "variable", "unknown" };
41         static int samplerate[16] = { 44100, 1, 48000, 32000, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
42         static char *channel[16] = { "unspec", "left", "right", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" };
43         static char *emphasis[8] = { "none", "50/15 usec 2 channel", "2", "3", "4", "5", "6", "7" };
44         unsigned int status, rate = 0;
45         
46         status = snd_emu10k1_ptr_read(emu, status_reg, 0);
47         if (rate_reg > 0)
48                 rate = snd_emu10k1_ptr_read(emu, rate_reg, 0);
49
50         snd_iprintf(buffer, "\n%s\n", title);
51
52         snd_iprintf(buffer, "Professional Mode     : %s\n", (status & SPCS_PROFESSIONAL) ? "yes" : "no");
53         snd_iprintf(buffer, "Not Audio Data        : %s\n", (status & SPCS_NOTAUDIODATA) ? "yes" : "no");
54         snd_iprintf(buffer, "Copyright             : %s\n", (status & SPCS_COPYRIGHT) ? "yes" : "no");
55         snd_iprintf(buffer, "Emphasis              : %s\n", emphasis[(status & SPCS_EMPHASISMASK) >> 3]);
56         snd_iprintf(buffer, "Mode                  : %i\n", (status & SPCS_MODEMASK) >> 6);
57         snd_iprintf(buffer, "Category Code         : 0x%x\n", (status & SPCS_CATEGORYCODEMASK) >> 8);
58         snd_iprintf(buffer, "Generation Status     : %s\n", status & SPCS_GENERATIONSTATUS ? "original" : "copy");
59         snd_iprintf(buffer, "Source Mask           : %i\n", (status & SPCS_SOURCENUMMASK) >> 16);
60         snd_iprintf(buffer, "Channel Number        : %s\n", channel[(status & SPCS_CHANNELNUMMASK) >> 20]);
61         snd_iprintf(buffer, "Sample Rate           : %iHz\n", samplerate[(status & SPCS_SAMPLERATEMASK) >> 24]);
62         snd_iprintf(buffer, "Clock Accuracy        : %s\n", clkaccy[(status & SPCS_CLKACCYMASK) >> 28]);
63
64         if (rate_reg > 0) {
65                 snd_iprintf(buffer, "S/PDIF Locked         : %s\n", rate & SRCS_SPDIFLOCKED ? "on" : "off");
66                 snd_iprintf(buffer, "Rate Locked           : %s\n", rate & SRCS_RATELOCKED ? "on" : "off");
67                 snd_iprintf(buffer, "Estimated Sample Rate : 0x%x\n", rate & SRCS_ESTSAMPLERATE);
68         }
69 }
70
71 static void snd_emu10k1_proc_read(snd_info_entry_t *entry, 
72                                   snd_info_buffer_t * buffer)
73 {
74         static char *outputs[32] = {
75                 /* 00 */ "PCM Left",
76                 /* 01 */ "PCM Right",
77                 /* 02 */ "PCM Surround Left",
78                 /* 03 */ "PCM Surround Right",
79                 /* 04 */ "MIDI Left",
80                 /* 05 */ "MIDI Right",
81                 /* 06 */ "PCM Center",
82                 /* 07 */ "PCM LFE",
83                 /* 08 */ "???",
84                 /* 09 */ "???",
85                 /* 10 */ "???",
86                 /* 11 */ "???",
87                 /* 12 */ "MIDI Reverb",
88                 /* 13 */ "MIDI Chorus",
89                 /* 14 */ "???",
90                 /* 15 */ "???",
91                 /* 16 */ "???",
92                 /* 17 */ "???",
93                 /* 18 */ "ADC Left / CDROM S/PDIF Left",
94                 /* 19 */ "ADC Right / CDROM S/PDIF Right",
95                 /* 20 */ "MIC / Zoom Video Left",
96                 /* 21 */ "Zoom Video Right",
97                 /* 22 */ "S/PDIF Left",
98                 /* 23 */ "S/PDIF Right",
99                 /* 24 */ "???",
100                 /* 25 */ "???",
101                 /* 26 */ "???",
102                 /* 27 */ "???",
103                 /* 28 */ "???",
104                 /* 29 */ "???",
105                 /* 30 */ "???",
106                 /* 31 */ "???"
107         };
108         emu10k1_t *emu = snd_magic_cast(emu10k1_t, entry->private_data, return);
109         unsigned int val;
110         int nefx = emu->audigy ? 64 : 32;
111         int idx;
112         
113         snd_iprintf(buffer, "EMU10K1\n\n");
114         val = emu->audigy ?
115                 snd_emu10k1_ptr_read(emu, A_FXRT1, 0) :
116                 snd_emu10k1_ptr_read(emu, FXRT, 0);
117         snd_iprintf(buffer, "Card                  : %s\n",
118                     emu->audigy ? "Audigy" : (emu->APS ? "EMU APS" : "Creative"));
119         snd_iprintf(buffer, "Internal TRAM (words) : 0x%x\n", emu->fx8010.itram_size);
120         snd_iprintf(buffer, "External TRAM (words) : 0x%x\n", emu->fx8010.etram_pages.bytes);
121         snd_iprintf(buffer, "\n");
122         if (emu->audigy) {
123                 snd_iprintf(buffer, "Effect Send Routing   : A=%i, B=%i, C=%i, D=%i\n",
124                             val & 0x3f,
125                             (val >> 8) & 0x3f,
126                             (val >> 16) & 0x3f,
127                             (val >> 24) & 0x3f);
128         } else {
129                 snd_iprintf(buffer, "Effect Send Routing   : A=%i, B=%i, C=%i, D=%i\n",
130                             (val >> 16) & 0x0f,
131                             (val >> 20) & 0x0f,
132                             (val >> 24) & 0x0f,
133                             (val >> 28) & 0x0f);
134         }
135         snd_iprintf(buffer, "\nCaptured FX Outputs   :\n");
136         for (idx = 0; idx < nefx; idx++) {
137                 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32)))
138                         snd_iprintf(buffer, "  Output %02i [%s]\n", idx, outputs[idx%32]);
139         }
140         snd_iprintf(buffer, "\nAll FX Outputs        :\n");
141         for (idx = 0; idx < 32; idx++)
142                 snd_iprintf(buffer, "  Output %02i [%s]\n", idx, outputs[idx]);
143         snd_emu10k1_proc_spdif_status(emu, buffer, "S/PDIF Output 0", SPCS0, -1);
144         snd_emu10k1_proc_spdif_status(emu, buffer, "S/PDIF Output 1", SPCS1, -1);
145         snd_emu10k1_proc_spdif_status(emu, buffer, "S/PDIF Output 2/3", SPCS2, -1);
146         snd_emu10k1_proc_spdif_status(emu, buffer, "CD-ROM S/PDIF", CDCS, CDSRCS);
147         snd_emu10k1_proc_spdif_status(emu, buffer, "General purpose S/PDIF", GPSCS, GPSRCS);
148         val = snd_emu10k1_ptr_read(emu, ZVSRCS, 0);
149         snd_iprintf(buffer, "\nZoomed Video\n");
150         snd_iprintf(buffer, "Rate Locked           : %s\n", val & SRCS_RATELOCKED ? "on" : "off");
151         snd_iprintf(buffer, "Estimated Sample Rate : 0x%x\n", val & SRCS_ESTSAMPLERATE);
152 }
153
154 static void snd_emu10k1_proc_acode_read(snd_info_entry_t *entry, 
155                                         snd_info_buffer_t * buffer)
156 {
157         u32 pc;
158         emu10k1_t *emu = snd_magic_cast(emu10k1_t, entry->private_data, return);
159
160         snd_iprintf(buffer, "FX8010 Instruction List '%s'\n", emu->fx8010.name);
161         snd_iprintf(buffer, "  Code dump      :\n");
162         for (pc = 0; pc < 512; pc++) {
163                 u32 low, high;
164                         
165                 low = snd_emu10k1_efx_read(emu, pc * 2);
166                 high = snd_emu10k1_efx_read(emu, pc * 2 + 1);
167                 if (emu->audigy)
168                         snd_iprintf(buffer, "    OP(0x%02x, 0x%03x, 0x%03x, 0x%03x, 0x%03x) /* 0x%04x: 0x%08x%08x */\n",
169                                     (high >> 24) & 0x0f,
170                                     (high >> 12) & 0x7ff,
171                                     (high >> 0) & 0x7ff,
172                                     (low >> 12) & 0x7ff,
173                                     (low >> 0) & 0x7ff,
174                                     pc,
175                                     high, low);
176                 else
177                         snd_iprintf(buffer, "    OP(0x%02x, 0x%03x, 0x%03x, 0x%03x, 0x%03x) /* 0x%04x: 0x%08x%08x */\n",
178                                     (high >> 20) & 0x0f,
179                                     (high >> 10) & 0x3ff,
180                                     (high >> 0) & 0x3ff,
181                                     (low >> 10) & 0x3ff,
182                                     (low >> 0) & 0x3ff,
183                                     pc,
184                                     high, low);
185         }
186 }
187
188 #define TOTAL_SIZE_GPR          (0x100*4)
189 #define TOTAL_SIZE_TANKMEM_DATA (0xa0*4)
190 #define TOTAL_SIZE_TANKMEM_ADDR (0xa0*4)
191 #define TOTAL_SIZE_CODE         (0x200*8)
192
193 static long snd_emu10k1_fx8010_read(snd_info_entry_t *entry, void *file_private_data,
194                                     struct file *file, char *buf, long count)
195 {
196         long size;
197         emu10k1_t *emu = snd_magic_cast(emu10k1_t, entry->private_data, return -ENXIO);
198         unsigned int offset;
199         
200         if (!strcmp(entry->name, "fx8010_tram_addr")) {
201                 if (emu->audigy) return -EINVAL;
202                 offset = TANKMEMADDRREGBASE;
203         } else if (!strcmp(entry->name, "fx8010_tram_data")) {
204                 if (emu->audigy) return -EINVAL;
205                 offset = TANKMEMDATAREGBASE;
206         } else if (!strcmp(entry->name, "fx8010_code")) {
207                 offset = emu->audigy ? A_MICROCODEBASE : MICROCODEBASE;
208         } else {
209                 offset = emu->audigy ? A_FXGPREGBASE : FXGPREGBASE;
210         }
211         size = count;
212         if (file->f_pos + size > entry->size)
213                 size = (long)entry->size - file->f_pos;
214         if (size > 0) {
215                 unsigned int *tmp;
216                 long res;
217                 unsigned int idx;
218                 if ((tmp = kmalloc(size + 8, GFP_KERNEL)) == NULL)
219                         return -ENOMEM;
220                 for (idx = 0; idx < ((file->f_pos & 3) + size + 3) >> 2; idx++)
221                         tmp[idx] = snd_emu10k1_ptr_read(emu, offset + idx + (file->f_pos >> 2), 0);
222                 if (copy_to_user(buf, ((char *)tmp) + (file->f_pos & 3), size))
223                         res = -EFAULT;
224                 else {
225                         res = size;
226                         file->f_pos += size;
227                 }
228                 kfree(tmp);
229                 return res;
230         }
231         return 0;
232 }
233
234 static struct snd_info_entry_ops snd_emu10k1_proc_ops_fx8010 = {
235         .read = snd_emu10k1_fx8010_read,
236 };
237
238 int __devinit snd_emu10k1_proc_init(emu10k1_t * emu)
239 {
240         snd_info_entry_t *entry;
241         
242         if (! snd_card_proc_new(emu->card, "emu10k1", &entry))
243                 snd_info_set_text_ops(entry, emu, 1024, snd_emu10k1_proc_read);
244
245         if (! snd_card_proc_new(emu->card, "fx8010_gpr", &entry)) {
246                 entry->content = SNDRV_INFO_CONTENT_DATA;
247                 entry->private_data = emu;
248                 entry->mode = S_IFREG | S_IRUGO /*| S_IWUSR*/;
249                 entry->size = TOTAL_SIZE_GPR;
250                 entry->c.ops = &snd_emu10k1_proc_ops_fx8010;
251         }
252         if (!emu->audigy && ! snd_card_proc_new(emu->card, "fx8010_tram_data", &entry)) {
253                 entry->content = SNDRV_INFO_CONTENT_DATA;
254                 entry->private_data = emu;
255                 entry->mode = S_IFREG | S_IRUGO /*| S_IWUSR*/;
256                 entry->size = TOTAL_SIZE_TANKMEM_DATA;
257                 entry->c.ops = &snd_emu10k1_proc_ops_fx8010;
258         }
259         if (!emu->audigy && ! snd_card_proc_new(emu->card, "fx8010_tram_addr", &entry)) {
260                 entry->content = SNDRV_INFO_CONTENT_DATA;
261                 entry->private_data = emu;
262                 entry->mode = S_IFREG | S_IRUGO /*| S_IWUSR*/;
263                 entry->size = TOTAL_SIZE_TANKMEM_ADDR;
264                 entry->c.ops = &snd_emu10k1_proc_ops_fx8010;
265         }
266         if (! snd_card_proc_new(emu->card, "fx8010_code", &entry)) {
267                 entry->content = SNDRV_INFO_CONTENT_DATA;
268                 entry->private_data = emu;
269                 entry->mode = S_IFREG | S_IRUGO /*| S_IWUSR*/;
270                 entry->size = TOTAL_SIZE_CODE;
271                 entry->c.ops = &snd_emu10k1_proc_ops_fx8010;
272         }
273         if (! snd_card_proc_new(emu->card, "fx8010_acode", &entry)) {
274                 entry->content = SNDRV_INFO_CONTENT_TEXT;
275                 entry->private_data = emu;
276                 entry->mode = S_IFREG | S_IRUGO /*| S_IWUSR*/;
277                 entry->c.text.read_size = 64*1024;
278                 entry->c.text.read = snd_emu10k1_proc_acode_read;
279         }
280         return 0;
281 }