Pull one more egcs 1.1.2 workaround.
[linux-2.6/linux-mips.git] / sound / pci / es1968.c
blobdc36e2a8c7edc2df7cb7e81e8e8098b934a1623d
1 /*
2 * Driver for ESS Maestro 1/2/2E Sound Card (started 21.8.99)
3 * Copyright (c) by Matze Braun <MatzeBraun@gmx.de>.
4 * Takashi Iwai <tiwai@suse.de>
5 *
6 * Most of the driver code comes from Zach Brown(zab@redhat.com)
7 * Alan Cox OSS Driver
8 * Rewritted from card-es1938.c source.
10 * TODO:
11 * Perhaps Synth
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Notes from Zach Brown about the driver code
30 * Hardware Description
32 * A working Maestro setup contains the Maestro chip wired to a
33 * codec or 2. In the Maestro we have the APUs, the ASSP, and the
34 * Wavecache. The APUs can be though of as virtual audio routing
35 * channels. They can take data from a number of sources and perform
36 * basic encodings of the data. The wavecache is a storehouse for
37 * PCM data. Typically it deals with PCI and interracts with the
38 * APUs. The ASSP is a wacky DSP like device that ESS is loth
39 * to release docs on. Thankfully it isn't required on the Maestro
40 * until you start doing insane things like FM emulation and surround
41 * encoding. The codecs are almost always AC-97 compliant codecs,
42 * but it appears that early Maestros may have had PT101 (an ESS
43 * part?) wired to them. The only real difference in the Maestro
44 * families is external goop like docking capability, memory for
45 * the ASSP, and initialization differences.
47 * Driver Operation
49 * We only drive the APU/Wavecache as typical DACs and drive the
50 * mixers in the codecs. There are 64 APUs. We assign 6 to each
51 * /dev/dsp? device. 2 channels for output, and 4 channels for
52 * input.
54 * Each APU can do a number of things, but we only really use
55 * 3 basic functions. For playback we use them to convert PCM
56 * data fetched over PCI by the wavecahche into analog data that
57 * is handed to the codec. One APU for mono, and a pair for stereo.
58 * When in stereo, the combination of smarts in the APU and Wavecache
59 * decide which wavecache gets the left or right channel.
61 * For record we still use the old overly mono system. For each in
62 * coming channel the data comes in from the codec, through a 'input'
63 * APU, through another rate converter APU, and then into memory via
64 * the wavecache and PCI. If its stereo, we mash it back into LRLR in
65 * software. The pass between the 2 APUs is supposedly what requires us
66 * to have a 512 byte buffer sitting around in wavecache/memory.
68 * The wavecache makes our life even more fun. First off, it can
69 * only address the first 28 bits of PCI address space, making it
70 * useless on quite a few architectures. Secondly, its insane.
71 * It claims to fetch from 4 regions of PCI space, each 4 meg in length.
72 * But that doesn't really work. You can only use 1 region. So all our
73 * allocations have to be in 4meg of each other. Booo. Hiss.
74 * So we have a module parameter, dsps_order, that is the order of
75 * the number of dsps to provide. All their buffer space is allocated
76 * on open time. The sonicvibes OSS routines we inherited really want
77 * power of 2 buffers, so we have all those next to each other, then
78 * 512 byte regions for the recording wavecaches. This ends up
79 * wasting quite a bit of memory. The only fixes I can see would be
80 * getting a kernel allocator that could work in zones, or figuring out
81 * just how to coerce the WP into doing what we want.
83 * The indirection of the various registers means we have to spinlock
84 * nearly all register accesses. We have the main register indirection
85 * like the wave cache, maestro registers, etc. Then we have beasts
86 * like the APU interface that is indirect registers gotten at through
87 * the main maestro indirection. Ouch. We spinlock around the actual
88 * ports on a per card basis. This means spinlock activity at each IO
89 * operation, but the only IO operation clusters are in non critical
90 * paths and it makes the code far easier to follow. Interrupts are
91 * blocked while holding the locks because the int handler has to
92 * get at some of them :(. The mixer interface doesn't, however.
93 * We also have an OSS state lock that is thrown around in a few
94 * places.
97 #define __SND_OSS_COMPAT__
98 #include <sound/driver.h>
99 #include <asm/io.h>
100 #include <linux/delay.h>
101 #include <linux/interrupt.h>
102 #include <linux/init.h>
103 #include <linux/pci.h>
104 #include <linux/slab.h>
105 #include <sound/core.h>
106 #include <sound/pcm.h>
107 #include <sound/mpu401.h>
108 #include <sound/ac97_codec.h>
109 #define SNDRV_GET_ID
110 #include <sound/initval.h>
112 #define chip_t es1968_t
114 #define CARD_NAME "ESS Maestro1/2"
115 #define DRIVER_NAME "ES1968"
117 MODULE_DESCRIPTION("ESS Maestro");
118 MODULE_CLASSES("{sound}");
119 MODULE_LICENSE("GPL");
120 MODULE_DEVICES("{{ESS,Maestro 2e},"
121 "{ESS,Maestro 2},"
122 "{ESS,Maestro 1},"
123 "{TerraTec,DMX}}");
125 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 1-MAX */
126 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
127 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
128 static int total_bufsize[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1024 };
129 static int pcm_substreams_p[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4 };
130 static int pcm_substreams_c[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1 };
131 static int clock[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
133 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
134 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
135 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
136 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
137 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
138 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
139 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
140 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
141 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
142 MODULE_PARM(total_bufsize, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
143 MODULE_PARM_DESC(total_bufsize, "Total buffer size in kB.");
144 MODULE_PARM_SYNTAX(total_bufsize, SNDRV_ENABLED ",allows:{{1,4096}},skill:advanced");
145 MODULE_PARM(pcm_substreams_p, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
146 MODULE_PARM_DESC(pcm_substreams_p, "PCM Playback substreams for " CARD_NAME " soundcard.");
147 MODULE_PARM_SYNTAX(pcm_substreams_p, SNDRV_ENABLED ",allows:{{1,8}}");
148 MODULE_PARM(pcm_substreams_c, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
149 MODULE_PARM_DESC(pcm_substreams_c, "PCM Capture substreams for " CARD_NAME " soundcard.");
150 MODULE_PARM_SYNTAX(pcm_substreams_c, SNDRV_ENABLED ",allows:{{0,8}}");
151 MODULE_PARM(clock, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
152 MODULE_PARM_DESC(clock, "Clock on " CARD_NAME " soundcard. (0 = auto-detect)");
153 MODULE_PARM_SYNTAX(clock, SNDRV_ENABLED);
156 /* PCI Dev ID's */
158 #ifndef PCI_VENDOR_ID_ESS
159 #define PCI_VENDOR_ID_ESS 0x125D
160 #endif
162 #define PCI_VENDOR_ID_ESS_OLD 0x1285 /* Platform Tech, the people the ESS
163 was bought form */
165 #ifndef PCI_DEVICE_ID_ESS_M2E
166 #define PCI_DEVICE_ID_ESS_M2E 0x1978
167 #endif
168 #ifndef PCI_DEVICE_ID_ESS_M2
169 #define PCI_DEVICE_ID_ESS_M2 0x1968
170 #endif
171 #ifndef PCI_DEVICE_ID_ESS_M1
172 #define PCI_DEVICE_ID_ESS_M1 0x0100
173 #endif
175 #define NR_APUS 64
176 #define NR_APU_REGS 16
178 /* NEC Versas ? */
179 #define NEC_VERSA_SUBID1 0x80581033
180 #define NEC_VERSA_SUBID2 0x803c1033
182 /* Mode Flags */
183 #define ESS_FMT_STEREO 0x01
184 #define ESS_FMT_16BIT 0x02
186 #define DAC_RUNNING 1
187 #define ADC_RUNNING 2
189 /* Values for the ESM_LEGACY_AUDIO_CONTROL */
191 #define ESS_ENABLE_AUDIO 0x8000
192 #define ESS_ENABLE_SERIAL_IRQ 0x4000
193 #define IO_ADRESS_ALIAS 0x0020
194 #define MPU401_IRQ_ENABLE 0x0010
195 #define MPU401_IO_ENABLE 0x0008
196 #define GAME_IO_ENABLE 0x0004
197 #define FM_IO_ENABLE 0x0002
198 #define SB_IO_ENABLE 0x0001
200 /* Values for the ESM_CONFIG_A */
202 #define PIC_SNOOP1 0x4000
203 #define PIC_SNOOP2 0x2000
204 #define SAFEGUARD 0x0800
205 #define DMA_CLEAR 0x0700
206 #define DMA_DDMA 0x0000
207 #define DMA_TDMA 0x0100
208 #define DMA_PCPCI 0x0200
209 #define POST_WRITE 0x0080
210 #define ISA_TIMING 0x0040
211 #define SWAP_LR 0x0020
212 #define SUBTR_DECODE 0x0002
214 /* Values for the ESM_CONFIG_B */
216 #define SPDIF_CONFB 0x0100
217 #define HWV_CONFB 0x0080
218 #define DEBOUNCE 0x0040
219 #define GPIO_CONFB 0x0020
220 #define CHI_CONFB 0x0010
221 #define IDMA_CONFB 0x0008 /*undoc */
222 #define MIDI_FIX 0x0004 /*undoc */
223 #define IRQ_TO_ISA 0x0001 /*undoc */
225 /* Values for Ring Bus Control B */
226 #define RINGB_2CODEC_ID_MASK 0x0003
227 #define RINGB_DIS_VALIDATION 0x0008
228 #define RINGB_EN_SPDIF 0x0010
229 #define RINGB_EN_2CODEC 0x0020
230 #define RINGB_SING_BIT_DUAL 0x0040
232 /* ****Port Adresses**** */
234 /* Write & Read */
235 #define ESM_INDEX 0x02
236 #define ESM_DATA 0x00
238 /* AC97 + RingBus */
239 #define ESM_AC97_INDEX 0x30
240 #define ESM_AC97_DATA 0x32
241 #define ESM_RING_BUS_DEST 0x34
242 #define ESM_RING_BUS_CONTR_A 0x36
243 #define ESM_RING_BUS_CONTR_B 0x38
244 #define ESM_RING_BUS_SDO 0x3A
246 /* WaveCache*/
247 #define WC_INDEX 0x10
248 #define WC_DATA 0x12
249 #define WC_CONTROL 0x14
251 /* ASSP*/
252 #define ASSP_INDEX 0x80
253 #define ASSP_MEMORY 0x82
254 #define ASSP_DATA 0x84
255 #define ASSP_CONTROL_A 0xA2
256 #define ASSP_CONTROL_B 0xA4
257 #define ASSP_CONTROL_C 0xA6
258 #define ASSP_HOSTW_INDEX 0xA8
259 #define ASSP_HOSTW_DATA 0xAA
260 #define ASSP_HOSTW_IRQ 0xAC
261 /* Midi */
262 #define ESM_MPU401_PORT 0x98
263 /* Others */
264 #define ESM_PORT_HOST_IRQ 0x18
266 #define IDR0_DATA_PORT 0x00
267 #define IDR1_CRAM_POINTER 0x01
268 #define IDR2_CRAM_DATA 0x02
269 #define IDR3_WAVE_DATA 0x03
270 #define IDR4_WAVE_PTR_LOW 0x04
271 #define IDR5_WAVE_PTR_HI 0x05
272 #define IDR6_TIMER_CTRL 0x06
273 #define IDR7_WAVE_ROMRAM 0x07
275 #define WRITEABLE_MAP 0xEFFFFF
276 #define READABLE_MAP 0x64003F
278 /* PCI Register */
280 #define ESM_LEGACY_AUDIO_CONTROL 0x40
281 #define ESM_ACPI_COMMAND 0x54
282 #define ESM_CONFIG_A 0x50
283 #define ESM_CONFIG_B 0x52
284 #define ESM_DDMA 0x60
286 /* Bob Bits */
287 #define ESM_BOB_ENABLE 0x0001
288 #define ESM_BOB_START 0x0001
290 /* Host IRQ Control Bits */
291 #define ESM_RESET_MAESTRO 0x8000
292 #define ESM_RESET_DIRECTSOUND 0x4000
293 #define ESM_HIRQ_ClkRun 0x0100
294 #define ESM_HIRQ_HW_VOLUME 0x0040
295 #define ESM_HIRQ_HARPO 0x0030 /* What's that? */
296 #define ESM_HIRQ_ASSP 0x0010
297 #define ESM_HIRQ_DSIE 0x0004
298 #define ESM_HIRQ_MPU401 0x0002
299 #define ESM_HIRQ_SB 0x0001
301 /* Host IRQ Status Bits */
302 #define ESM_MPU401_IRQ 0x02
303 #define ESM_SB_IRQ 0x01
304 #define ESM_SOUND_IRQ 0x04
305 #define ESM_ASSP_IRQ 0x10
306 #define ESM_HWVOL_IRQ 0x40
308 #define ESS_SYSCLK 50000000
309 #define ESM_BOB_FREQ 200
310 #define ESM_BOB_FREQ_MAX 800
312 #define ESM_FREQ_ESM1 (49152000L / 1024L) /* default rate 48000 */
313 #define ESM_FREQ_ESM2 (50000000L / 1024L)
315 /* APU Modes: reg 0x00, bit 4-7 */
316 #define ESM_APU_MODE_SHIFT 4
317 #define ESM_APU_MODE_MASK (0xf << 4)
318 #define ESM_APU_OFF 0x00
319 #define ESM_APU_16BITLINEAR 0x01 /* 16-Bit Linear Sample Player */
320 #define ESM_APU_16BITSTEREO 0x02 /* 16-Bit Stereo Sample Player */
321 #define ESM_APU_8BITLINEAR 0x03 /* 8-Bit Linear Sample Player */
322 #define ESM_APU_8BITSTEREO 0x04 /* 8-Bit Stereo Sample Player */
323 #define ESM_APU_8BITDIFF 0x05 /* 8-Bit Differential Sample Playrer */
324 #define ESM_APU_DIGITALDELAY 0x06 /* Digital Delay Line */
325 #define ESM_APU_DUALTAP 0x07 /* Dual Tap Reader */
326 #define ESM_APU_CORRELATOR 0x08 /* Correlator */
327 #define ESM_APU_INPUTMIXER 0x09 /* Input Mixer */
328 #define ESM_APU_WAVETABLE 0x0A /* Wave Table Mode */
329 #define ESM_APU_SRCONVERTOR 0x0B /* Sample Rate Convertor */
330 #define ESM_APU_16BITPINGPONG 0x0C /* 16-Bit Ping-Pong Sample Player */
331 #define ESM_APU_RESERVED1 0x0D /* Reserved 1 */
332 #define ESM_APU_RESERVED2 0x0E /* Reserved 2 */
333 #define ESM_APU_RESERVED3 0x0F /* Reserved 3 */
335 /* reg 0x00 */
336 #define ESM_APU_FILTER_Q_SHIFT 0
337 #define ESM_APU_FILTER_Q_MASK (3 << 0)
338 /* APU Filtey Q Control */
339 #define ESM_APU_FILTER_LESSQ 0x00
340 #define ESM_APU_FILTER_MOREQ 0x03
342 #define ESM_APU_FILTER_TYPE_SHIFT 2
343 #define ESM_APU_FILTER_TYPE_MASK (3 << 2)
344 #define ESM_APU_ENV_TYPE_SHIFT 8
345 #define ESM_APU_ENV_TYPE_MASK (3 << 8)
346 #define ESM_APU_ENV_STATE_SHIFT 10
347 #define ESM_APU_ENV_STATE_MASK (3 << 10)
348 #define ESM_APU_END_CURVE (1 << 12)
349 #define ESM_APU_INT_ON_LOOP (1 << 13)
350 #define ESM_APU_DMA_ENABLE (1 << 14)
352 /* reg 0x02 */
353 #define ESM_APU_SUBMIX_GROUP_SHIRT 0
354 #define ESM_APU_SUBMIX_GROUP_MASK (7 << 0)
355 #define ESM_APU_SUBMIX_MODE (1 << 3)
356 #define ESM_APU_6dB (1 << 4)
357 #define ESM_APU_DUAL_EFFECT (1 << 5)
358 #define ESM_APU_EFFECT_CHANNELS_SHIFT 6
359 #define ESM_APU_EFFECT_CHANNELS_MASK (3 << 6)
361 /* reg 0x03 */
362 #define ESM_APU_STEP_SIZE_MASK 0x0fff
364 /* reg 0x04 */
365 #define ESM_APU_PHASE_SHIFT 0
366 #define ESM_APU_PHASE_MASK (0xff << 0)
367 #define ESM_APU_WAVE64K_PAGE_SHIFT 8 /* most 8bit of wave start offset */
368 #define ESM_APU_WAVE64K_PAGE_MASK (0xff << 8)
370 /* reg 0x05 - wave start offset */
371 /* reg 0x06 - wave end offset */
372 /* reg 0x07 - wave loop length */
374 /* reg 0x08 */
375 #define ESM_APU_EFFECT_GAIN_SHIFT 0
376 #define ESM_APU_EFFECT_GAIN_MASK (0xff << 0)
377 #define ESM_APU_TREMOLO_DEPTH_SHIFT 8
378 #define ESM_APU_TREMOLO_DEPTH_MASK (0xf << 8)
379 #define ESM_APU_TREMOLO_RATE_SHIFT 12
380 #define ESM_APU_TREMOLO_RATE_MASK (0xf << 12)
382 /* reg 0x09 */
383 /* bit 0-7 amplitude dest? */
384 #define ESM_APU_AMPLITUDE_NOW_SHIFT 8
385 #define ESM_APU_AMPLITUDE_NOW_MASK (0xff << 8)
387 /* reg 0x0a */
388 #define ESM_APU_POLAR_PAN_SHIFT 0
389 #define ESM_APU_POLAR_PAN_MASK (0x3f << 0)
390 /* Polar Pan Control */
391 #define ESM_APU_PAN_CENTER_CIRCLE 0x00
392 #define ESM_APU_PAN_MIDDLE_RADIUS 0x01
393 #define ESM_APU_PAN_OUTSIDE_RADIUS 0x02
395 #define ESM_APU_FILTER_TUNING_SHIFT 8
396 #define ESM_APU_FILTER_TUNING_MASK (0xff << 8)
398 /* reg 0x0b */
399 #define ESM_APU_DATA_SRC_A_SHIFT 0
400 #define ESM_APU_DATA_SRC_A_MASK (0x7f << 0)
401 #define ESM_APU_INV_POL_A (1 << 7)
402 #define ESM_APU_DATA_SRC_B_SHIFT 8
403 #define ESM_APU_DATA_SRC_B_MASK (0x7f << 8)
404 #define ESM_APU_INV_POL_B (1 << 15)
406 #define ESM_APU_VIBRATO_RATE_SHIFT 0
407 #define ESM_APU_VIBRATO_RATE_MASK (0xf << 0)
408 #define ESM_APU_VIBRATO_DEPTH_SHIFT 4
409 #define ESM_APU_VIBRATO_DEPTH_MASK (0xf << 4)
410 #define ESM_APU_VIBRATO_PHASE_SHIFT 8
411 #define ESM_APU_VIBRATO_PHASE_MASK (0xff << 8)
413 /* reg 0x0c */
414 #define ESM_APU_RADIUS_SELECT (1 << 6)
416 /* APU Filter Control */
417 #define ESM_APU_FILTER_2POLE_LOPASS 0x00
418 #define ESM_APU_FILTER_2POLE_BANDPASS 0x01
419 #define ESM_APU_FILTER_2POLE_HIPASS 0x02
420 #define ESM_APU_FILTER_1POLE_LOPASS 0x03
421 #define ESM_APU_FILTER_1POLE_HIPASS 0x04
422 #define ESM_APU_FILTER_OFF 0x05
424 /* APU ATFP Type */
425 #define ESM_APU_ATFP_AMPLITUDE 0x00
426 #define ESM_APU_ATFP_TREMELO 0x01
427 #define ESM_APU_ATFP_FILTER 0x02
428 #define ESM_APU_ATFP_PAN 0x03
430 /* APU ATFP Flags */
431 #define ESM_APU_ATFP_FLG_OFF 0x00
432 #define ESM_APU_ATFP_FLG_WAIT 0x01
433 #define ESM_APU_ATFP_FLG_DONE 0x02
434 #define ESM_APU_ATFP_FLG_INPROCESS 0x03
437 /* capture mixing buffer size */
438 #define ESM_MIXBUF_SIZE 512
440 #define ESM_MODE_PLAY 0
441 #define ESM_MODE_CAPTURE 1
443 /* acpi states */
444 enum {
445 ACPI_D0=0,
446 ACPI_D1,
447 ACPI_D2,
448 ACPI_D3
451 /* bits in the acpi masks */
452 #define ACPI_12MHZ ( 1 << 15)
453 #define ACPI_24MHZ ( 1 << 14)
454 #define ACPI_978 ( 1 << 13)
455 #define ACPI_SPDIF ( 1 << 12)
456 #define ACPI_GLUE ( 1 << 11)
457 #define ACPI__10 ( 1 << 10) /* reserved */
458 #define ACPI_PCIINT ( 1 << 9)
459 #define ACPI_HV ( 1 << 8) /* hardware volume */
460 #define ACPI_GPIO ( 1 << 7)
461 #define ACPI_ASSP ( 1 << 6)
462 #define ACPI_SB ( 1 << 5) /* sb emul */
463 #define ACPI_FM ( 1 << 4) /* fm emul */
464 #define ACPI_RB ( 1 << 3) /* ringbus / aclink */
465 #define ACPI_MIDI ( 1 << 2)
466 #define ACPI_GP ( 1 << 1) /* game port */
467 #define ACPI_WP ( 1 << 0) /* wave processor */
469 #define ACPI_ALL (0xffff)
470 #define ACPI_SLEEP (~(ACPI_SPDIF|ACPI_ASSP|ACPI_SB|ACPI_FM| \
471 ACPI_MIDI|ACPI_GP|ACPI_WP))
472 #define ACPI_NONE (ACPI__10)
474 /* these masks indicate which units we care about at
475 which states */
476 static u16 acpi_state_mask[] = {
477 [ACPI_D0] = ACPI_ALL,
478 [ACPI_D1] = ACPI_SLEEP,
479 [ACPI_D2] = ACPI_SLEEP,
480 [ACPI_D3] = ACPI_NONE
484 typedef struct snd_es1968 es1968_t;
485 typedef struct snd_esschan esschan_t;
486 typedef struct snd_esm_memory esm_memory_t;
488 /* APU use in the driver */
489 enum snd_enum_apu_type {
490 ESM_APU_PCM_PLAY,
491 ESM_APU_PCM_CAPTURE,
492 ESM_APU_PCM_RATECONV,
493 ESM_APU_FREE
496 /* DMA Hack! */
497 struct snd_esm_memory {
498 char *buf;
499 unsigned long addr;
500 int size;
501 int empty; /* status */
502 struct list_head list;
505 /* Playback Channel */
506 struct snd_esschan {
507 int running;
509 u8 apu[4];
510 u8 apu_mode[4];
512 /* playback/capture pcm buffer */
513 esm_memory_t *memory;
514 /* capture mixer buffer */
515 esm_memory_t *mixbuf;
517 unsigned int hwptr; /* current hw pointer in bytes */
518 unsigned int count; /* sample counter in bytes */
519 unsigned int dma_size; /* total buffer size in bytes */
520 unsigned int frag_size; /* period size in bytes */
521 unsigned int wav_shift;
522 u16 base[4]; /* offset for ptr */
524 /* stereo/16bit flag */
525 unsigned char fmt;
526 int mode; /* playback / capture */
528 int bob_freq; /* required timer frequency */
530 snd_pcm_substream_t *substream;
532 /* linked list */
533 struct list_head list;
535 #ifdef CONFIG_PM
536 u16 wc_map[4];
537 #endif
540 struct snd_es1968 {
541 /* Module Config */
542 int total_bufsize; /* in bytes */
544 int playback_streams, capture_streams;
546 unsigned int clock; /* clock */
548 /* buffer */
549 void *dma_buf;
550 dma_addr_t dma_buf_addr;
551 unsigned long dma_buf_size;
553 /* Resources... */
554 int irq;
555 unsigned long io_port;
556 struct resource *res_io_port;
557 int type;
558 struct pci_dev *pci;
559 snd_card_t *card;
560 snd_pcm_t *pcm;
562 /* DMA memory block */
563 struct list_head buf_list;
565 /* ALSA Stuff */
566 ac97_t *ac97;
567 snd_kcontrol_t *master_switch; /* for h/w volume control */
568 snd_kcontrol_t *master_volume;
570 snd_rawmidi_t *rmidi;
572 spinlock_t reg_lock;
573 struct tasklet_struct hwvol_tq;
575 /* Maestro Stuff */
576 u16 maestro_map[32];
577 atomic_t bobclient; /* active timer instancs */
578 int bob_freq; /* timer frequency */
579 spinlock_t bob_lock;
580 struct semaphore memory_mutex; /* memory lock */
582 /* APU states */
583 unsigned char apu[NR_APUS];
585 /* active substreams */
586 struct list_head substream_list;
587 spinlock_t substream_lock;
589 #ifdef CONFIG_PM
590 u16 apu_map[NR_APUS][NR_APU_REGS];
591 #endif
594 static void snd_es1968_interrupt(int irq, void *dev_id, struct pt_regs *regs);
596 #define CARD_TYPE_ESS_ESOLDM1 0x12850100
597 #define CARD_TYPE_ESS_ES1968 0x125d1968
598 #define CARD_TYPE_ESS_ES1978 0x125d1978
600 static struct pci_device_id snd_es1968_ids[] __devinitdata = {
601 /* Maestro 1 */
602 { 0x1285, 0x0100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },
603 /* Maestro 2 */
604 { 0x125d, 0x1968, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },
605 /* Maestro 2E */
606 { 0x125d, 0x1978, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },
607 { 0, }
610 MODULE_DEVICE_TABLE(pci, snd_es1968_ids);
612 /* *********************
613 * Low Level Funcs! *
614 *********************/
616 /* no spinlock */
617 static void __maestro_write(es1968_t *chip, u16 reg, u16 data)
619 outw(reg, chip->io_port + ESM_INDEX);
620 outw(data, chip->io_port + ESM_DATA);
621 chip->maestro_map[reg] = data;
624 inline static void maestro_write(es1968_t *chip, u16 reg, u16 data)
626 unsigned long flags;
627 spin_lock_irqsave(&chip->reg_lock, flags);
628 __maestro_write(chip, reg, data);
629 spin_unlock_irqrestore(&chip->reg_lock, flags);
632 /* no spinlock */
633 static u16 __maestro_read(es1968_t *chip, u16 reg)
635 if (READABLE_MAP & (1 << reg)) {
636 outw(reg, chip->io_port + ESM_INDEX);
637 chip->maestro_map[reg] = inw(chip->io_port + ESM_DATA);
639 return chip->maestro_map[reg];
642 inline static u16 maestro_read(es1968_t *chip, u16 reg)
644 unsigned long flags;
645 u16 result;
646 spin_lock_irqsave(&chip->reg_lock, flags);
647 result = __maestro_read(chip, reg);
648 spin_unlock_irqrestore(&chip->reg_lock, flags);
649 return result;
652 /* Wait for the codec bus to be free */
653 static int snd_es1968_ac97_wait(es1968_t *chip)
655 int timeout = 100000;
657 while (timeout-- > 0) {
658 if (!(inb(chip->io_port + ESM_AC97_INDEX) & 1))
659 return 0;
661 snd_printd("es1968: ac97 timeout\n");
662 return 1; /* timeout */
665 static void snd_es1968_ac97_write(ac97_t *ac97, unsigned short reg, unsigned short val)
667 es1968_t *chip = snd_magic_cast(es1968_t, ac97->private_data, return);
668 unsigned long flags;
670 spin_lock_irqsave(&chip->reg_lock, flags);
672 snd_es1968_ac97_wait(chip);
674 /* Write the bus */
675 outw(val, chip->io_port + ESM_AC97_DATA);
676 mdelay(1);
677 outb(reg, chip->io_port + ESM_AC97_INDEX);
678 mdelay(1);
680 spin_unlock_irqrestore(&chip->reg_lock, flags);
683 static unsigned short snd_es1968_ac97_read(ac97_t *ac97, unsigned short reg)
685 u16 data = 0;
686 es1968_t *chip = snd_magic_cast(es1968_t, ac97->private_data, return 0);
687 unsigned long flags;
689 spin_lock_irqsave(&chip->reg_lock, flags);
691 snd_es1968_ac97_wait(chip);
693 outb(reg | 0x80, chip->io_port + ESM_AC97_INDEX);
694 mdelay(1);
696 if (! snd_es1968_ac97_wait(chip)) {
697 data = inw(chip->io_port + ESM_AC97_DATA);
698 mdelay(1);
700 spin_unlock_irqrestore(&chip->reg_lock, flags);
702 return data;
705 /* no spinlock */
706 static void apu_index_set(es1968_t *chip, u16 index)
708 int i;
709 __maestro_write(chip, IDR1_CRAM_POINTER, index);
710 for (i = 0; i < 1000; i++)
711 if (__maestro_read(chip, IDR1_CRAM_POINTER) == index)
712 return;
713 snd_printd("es1968: APU register select failed. (Timeout)\n");
716 /* no spinlock */
717 static void apu_data_set(es1968_t *chip, u16 data)
719 int i;
720 for (i = 0; i < 1000; i++) {
721 if (__maestro_read(chip, IDR0_DATA_PORT) == data)
722 return;
723 __maestro_write(chip, IDR0_DATA_PORT, data);
725 snd_printd("es1968: APU register set probably failed (Timeout)!\n");
728 /* no spinlock */
729 static void __apu_set_register(es1968_t *chip, u16 channel, u8 reg, u16 data)
731 snd_assert(channel < NR_APUS, return);
732 #ifdef CONFIG_PM
733 chip->apu_map[channel][reg] = data;
734 #endif
735 reg |= (channel << 4);
736 apu_index_set(chip, reg);
737 apu_data_set(chip, data);
740 inline static void apu_set_register(es1968_t *chip, u16 channel, u8 reg, u16 data)
742 unsigned long flags;
743 spin_lock_irqsave(&chip->reg_lock, flags);
744 __apu_set_register(chip, channel, reg, data);
745 spin_unlock_irqrestore(&chip->reg_lock, flags);
748 static u16 __apu_get_register(es1968_t *chip, u16 channel, u8 reg)
750 snd_assert(channel < NR_APUS, return 0);
751 reg |= (channel << 4);
752 apu_index_set(chip, reg);
753 return __maestro_read(chip, IDR0_DATA_PORT);
756 inline static u16 apu_get_register(es1968_t *chip, u16 channel, u8 reg)
758 unsigned long flags;
759 u16 v;
760 spin_lock_irqsave(&chip->reg_lock, flags);
761 v = __apu_get_register(chip, channel, reg);
762 spin_unlock_irqrestore(&chip->reg_lock, flags);
763 return v;
766 #if 0 /* ASSP is not supported */
768 static void assp_set_register(es1968_t *chip, u32 reg, u32 value)
770 unsigned long flags;
772 spin_lock_irqsave(&chip->reg_lock, flags);
773 outl(reg, chip->io_port + ASSP_INDEX);
774 outl(value, chip->io_port + ASSP_DATA);
775 spin_unlock_irqrestore(&chip->reg_lock, flags);
778 static u32 assp_get_register(es1968_t *chip, u32 reg)
780 unsigned long flags;
781 u32 value;
783 spin_lock_irqsave(&chip->reg_lock, flags);
784 outl(reg, chip->io_port + ASSP_INDEX);
785 value = inl(chip->io_port + ASSP_DATA);
786 spin_unlock_irqrestore(&chip->reg_lock, flags);
788 return value;
791 #endif
793 static void wave_set_register(es1968_t *chip, u16 reg, u16 value)
795 unsigned long flags;
797 spin_lock_irqsave(&chip->reg_lock, flags);
798 outw(reg, chip->io_port + WC_INDEX);
799 outw(value, chip->io_port + WC_DATA);
800 spin_unlock_irqrestore(&chip->reg_lock, flags);
803 static u16 wave_get_register(es1968_t *chip, u16 reg)
805 unsigned long flags;
806 u16 value;
808 spin_lock_irqsave(&chip->reg_lock, flags);
809 outw(reg, chip->io_port + WC_INDEX);
810 value = inw(chip->io_port + WC_DATA);
811 spin_unlock_irqrestore(&chip->reg_lock, flags);
813 return value;
816 /* *******************
817 * Bob the Timer! *
818 *******************/
820 static void snd_es1968_bob_stop(es1968_t *chip)
822 u16 reg;
823 unsigned long flags;
825 spin_lock_irqsave(&chip->reg_lock, flags);
826 reg = __maestro_read(chip, 0x11);
827 reg &= ~ESM_BOB_ENABLE;
828 __maestro_write(chip, 0x11, reg);
829 reg = __maestro_read(chip, 0x17);
830 reg &= ~ESM_BOB_START;
831 __maestro_write(chip, 0x17, reg);
832 spin_unlock_irqrestore(&chip->reg_lock, flags);
835 static void snd_es1968_bob_start(es1968_t *chip)
837 int prescale;
838 int divide;
839 unsigned long flags;
841 /* compute ideal interrupt frequency for buffer size & play rate */
842 /* first, find best prescaler value to match freq */
843 for (prescale = 5; prescale < 12; prescale++)
844 if (chip->bob_freq > (ESS_SYSCLK >> (prescale + 9)))
845 break;
847 /* next, back off prescaler whilst getting divider into optimum range */
848 divide = 1;
849 while ((prescale > 5) && (divide < 32)) {
850 prescale--;
851 divide <<= 1;
853 divide >>= 1;
855 /* now fine-tune the divider for best match */
856 for (; divide < 31; divide++)
857 if (chip->bob_freq >
858 ((ESS_SYSCLK >> (prescale + 9)) / (divide + 1))) break;
860 /* divide = 0 is illegal, but don't let prescale = 4! */
861 if (divide == 0) {
862 divide++;
863 if (prescale > 5)
864 prescale--;
865 } else if (divide > 1)
866 divide--;
868 spin_lock_irqsave(&chip->reg_lock, flags);
869 __maestro_write(chip, 6, 0x9000 | (prescale << 5) | divide); /* set reg */
871 /* Now set IDR 11/17 */
872 __maestro_write(chip, 0x11, __maestro_read(chip, 0x11) | 1);
873 __maestro_write(chip, 0x17, __maestro_read(chip, 0x17) | 1);
874 spin_unlock_irqrestore(&chip->reg_lock, flags);
877 static void snd_es1968_bob_inc(es1968_t *chip, int freq)
879 unsigned long flags;
881 spin_lock_irqsave(&chip->bob_lock, flags);
882 atomic_inc(&chip->bobclient);
883 if (atomic_read(&chip->bobclient) == 1) {
884 chip->bob_freq = freq;
885 snd_es1968_bob_start(chip);
886 } else if (chip->bob_freq < freq) {
887 snd_es1968_bob_stop(chip);
888 chip->bob_freq = freq;
889 snd_es1968_bob_start(chip);
891 spin_unlock_irqrestore(&chip->bob_lock, flags);
894 static void snd_es1968_bob_dec(es1968_t *chip)
896 unsigned long flags;
898 spin_lock_irqsave(&chip->bob_lock, flags);
899 atomic_dec(&chip->bobclient);
900 if (atomic_read(&chip->bobclient) <= 0)
901 snd_es1968_bob_stop(chip);
902 else if (chip->bob_freq > ESM_BOB_FREQ) {
903 /* check reduction of timer frequency */
904 struct list_head *p;
905 int max_freq = ESM_BOB_FREQ;
906 spin_lock(&chip->substream_lock);
907 list_for_each(p, &chip->substream_list) {
908 esschan_t *es = list_entry(p, esschan_t, list);
909 if (max_freq < es->bob_freq)
910 max_freq = es->bob_freq;
912 spin_unlock(&chip->substream_lock);
913 if (max_freq != chip->bob_freq) {
914 snd_es1968_bob_stop(chip);
915 chip->bob_freq = max_freq;
916 snd_es1968_bob_start(chip);
919 spin_unlock_irqrestore(&chip->bob_lock, flags);
922 static int
923 snd_es1968_calc_bob_rate(es1968_t *chip, esschan_t *es,
924 snd_pcm_runtime_t *runtime)
926 /* we acquire 4 interrupts per period for precise control.. */
927 int freq = runtime->rate * 4;
928 if (es->fmt & ESS_FMT_STEREO)
929 freq <<= 1;
930 if (es->fmt & ESS_FMT_16BIT)
931 freq <<= 1;
932 freq /= es->frag_size;
933 if (freq < ESM_BOB_FREQ)
934 freq = ESM_BOB_FREQ;
935 else if (freq > ESM_BOB_FREQ_MAX)
936 freq = ESM_BOB_FREQ_MAX;
937 return freq;
941 /*************
942 * PCM Part *
943 *************/
945 static u32 snd_es1968_compute_rate(es1968_t *chip, u32 freq)
947 u32 rate = (freq << 16) / chip->clock;
948 #if 0 /* XXX: do we need this? */
949 if (rate > 0x10000)
950 rate = 0x10000;
951 #endif
952 return rate;
955 /* get current pointer */
956 inline static unsigned int
957 snd_es1968_get_dma_ptr(es1968_t *chip, esschan_t *es)
959 unsigned int offset;
961 offset = apu_get_register(chip, es->apu[0], 5);
963 offset -= es->base[0];
965 return (offset & 0xFFFE); /* hardware is in words */
968 static void snd_es1968_apu_set_freq(es1968_t *chip, int apu, int freq)
970 apu_set_register(chip, apu, 2,
971 (apu_get_register(chip, apu, 2) & 0x00FF) |
972 ((freq & 0xff) << 8) | 0x10);
973 apu_set_register(chip, apu, 3, freq >> 8);
976 /* spin lock held */
977 inline static void snd_es1968_trigger_apu(es1968_t *esm, int apu, int mode)
979 /* dma on, no envelopes, filter to all 1s) */
980 __apu_set_register(esm, apu, 0, 0x400f | mode);
983 static void snd_es1968_pcm_start(es1968_t *chip, esschan_t *es)
985 unsigned long flags;
987 spin_lock_irqsave(&chip->reg_lock, flags);
988 if (es->running) {
989 spin_unlock_irqrestore(&chip->reg_lock, flags);
990 return;
992 __apu_set_register(chip, es->apu[0], 5, es->base[0]);
993 snd_es1968_trigger_apu(chip, es->apu[0], es->apu_mode[0]);
994 if (es->mode == ESM_MODE_CAPTURE) {
995 __apu_set_register(chip, es->apu[2], 5, es->base[2]);
996 snd_es1968_trigger_apu(chip, es->apu[2], es->apu_mode[2]);
998 if (es->fmt & ESS_FMT_STEREO) {
999 __apu_set_register(chip, es->apu[1], 5, es->base[1]);
1000 snd_es1968_trigger_apu(chip, es->apu[1], es->apu_mode[1]);
1001 if (es->mode == ESM_MODE_CAPTURE) {
1002 __apu_set_register(chip, es->apu[3], 5, es->base[3]);
1003 snd_es1968_trigger_apu(chip, es->apu[3], es->apu_mode[3]);
1006 es->running = 1;
1007 spin_unlock_irqrestore(&chip->reg_lock, flags);
1010 static void snd_es1968_pcm_stop(es1968_t *chip, esschan_t *es)
1012 unsigned long flags;
1014 spin_lock_irqsave(&chip->reg_lock, flags);
1015 if (! es->running) {
1016 spin_unlock_irqrestore(&chip->reg_lock, flags);
1017 return;
1019 snd_es1968_trigger_apu(chip, es->apu[0], 0);
1020 snd_es1968_trigger_apu(chip, es->apu[1], 0);
1021 if (es->mode == ESM_MODE_CAPTURE) {
1022 snd_es1968_trigger_apu(chip, es->apu[2], 0);
1023 snd_es1968_trigger_apu(chip, es->apu[3], 0);
1025 es->running = 0;
1026 spin_unlock_irqrestore(&chip->reg_lock, flags);
1029 /* set the wavecache control reg */
1030 static void snd_es1968_program_wavecache(es1968_t *chip, esschan_t *es,
1031 int channel, u32 addr, int capture)
1033 u32 tmpval = (addr - 0x10) & 0xFFF8;
1035 if (! capture) {
1036 if (!(es->fmt & ESS_FMT_16BIT))
1037 tmpval |= 4; /* 8bit */
1038 if (es->fmt & ESS_FMT_STEREO)
1039 tmpval |= 2; /* stereo */
1042 /* set the wavecache control reg */
1043 wave_set_register(chip, es->apu[channel] << 3, tmpval);
1045 #ifdef CONFIG_PM
1046 es->wc_map[channel] = tmpval;
1047 #endif
1051 static void snd_es1968_playback_setup(es1968_t *chip, esschan_t *es,
1052 snd_pcm_runtime_t *runtime)
1054 u32 pa;
1055 int high_apu = 0;
1056 int channel, apu;
1057 int i, size;
1058 unsigned long flags;
1059 u32 freq;
1061 size = es->dma_size >> es->wav_shift;
1063 if (es->fmt & ESS_FMT_STEREO)
1064 high_apu++;
1066 for (channel = 0; channel <= high_apu; channel++) {
1067 apu = es->apu[channel];
1069 snd_es1968_program_wavecache(chip, es, channel, es->memory->addr, 0);
1071 /* Offset to PCMBAR */
1072 pa = es->memory->addr;
1073 pa -= chip->dma_buf_addr;
1074 pa >>= 1; /* words */
1076 pa |= 0x00400000; /* System RAM (Bit 22) */
1078 if (es->fmt & ESS_FMT_STEREO) {
1079 /* Enable stereo */
1080 if (channel)
1081 pa |= 0x00800000; /* (Bit 23) */
1082 if (es->fmt & ESS_FMT_16BIT)
1083 pa >>= 1;
1086 /* base offset of dma calcs when reading the pointer
1087 on this left one */
1088 es->base[channel] = pa & 0xFFFF;
1090 for (i = 0; i < 16; i++)
1091 apu_set_register(chip, apu, i, 0x0000);
1093 /* Load the buffer into the wave engine */
1094 apu_set_register(chip, apu, 4, ((pa >> 16) & 0xFF) << 8);
1095 apu_set_register(chip, apu, 5, pa & 0xFFFF);
1096 apu_set_register(chip, apu, 6, (pa + size) & 0xFFFF);
1097 /* setting loop == sample len */
1098 apu_set_register(chip, apu, 7, size);
1100 /* clear effects/env.. */
1101 apu_set_register(chip, apu, 8, 0x0000);
1102 /* set amp now to 0xd0 (?), low byte is 'amplitude dest'? */
1103 apu_set_register(chip, apu, 9, 0xD000);
1105 /* clear routing stuff */
1106 apu_set_register(chip, apu, 11, 0x0000);
1107 /* dma on, no envelopes, filter to all 1s) */
1108 // apu_set_register(chip, apu, 0, 0x400F);
1110 if (es->fmt & ESS_FMT_16BIT)
1111 es->apu_mode[channel] = 0x10; /* 16bit mono */
1112 else
1113 es->apu_mode[channel] = 0x30; /* 8bit mono */
1115 if (es->fmt & ESS_FMT_STEREO) {
1116 /* set panning: left or right */
1117 /* Check: different panning. On my Canyon 3D Chipset the
1118 Channels are swapped. I don't know, about the output
1119 to the SPDif Link. Perhaps you have to change this
1120 and not the APU Regs 4-5. */
1121 apu_set_register(chip, apu, 10,
1122 0x8F00 | (channel ? 0 : 0x10));
1123 es->apu_mode[channel] += 0x10; /* stereo */
1124 } else
1125 apu_set_register(chip, apu, 10, 0x8F08);
1128 spin_lock_irqsave(&chip->reg_lock, flags);
1129 /* clear WP interupts */
1130 outw(1, chip->io_port + 0x04);
1131 /* enable WP ints */
1132 outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ);
1133 spin_unlock_irqrestore(&chip->reg_lock, flags);
1135 freq = runtime->rate;
1136 /* set frequency */
1137 if (freq > 48000)
1138 freq = 48000;
1139 if (freq < 4000)
1140 freq = 4000;
1142 /* hmmm.. */
1143 if (!(es->fmt & ESS_FMT_16BIT) && !(es->fmt & ESS_FMT_STEREO))
1144 freq >>= 1;
1146 freq = snd_es1968_compute_rate(chip, freq);
1148 /* Load the frequency, turn on 6dB */
1149 snd_es1968_apu_set_freq(chip, es->apu[0], freq);
1150 snd_es1968_apu_set_freq(chip, es->apu[1], freq);
1153 static void snd_es1968_capture_setup(es1968_t *chip, esschan_t *es,
1154 snd_pcm_runtime_t *runtime)
1156 int apu_step = 2;
1157 int channel, apu;
1158 int i, size;
1159 u32 freq;
1160 unsigned long flags;
1162 size = es->dma_size >> es->wav_shift;
1164 /* we're given the full size of the buffer, but
1165 in stereo each channel will only use its half */
1166 if (es->fmt & ESS_FMT_STEREO)
1167 apu_step = 1;
1169 /* APU assignments:
1170 0 = mono/left SRC
1171 1 = right SRC
1172 2 = mono/left Input Mixer
1173 3 = right Input Mixer */
1174 for (channel = 0; channel < 4; channel += apu_step) {
1175 int bsize, route;
1176 u32 pa;
1178 apu = es->apu[channel];
1180 /* data seems to flow from the codec, through an apu into
1181 the 'mixbuf' bit of page, then through the SRC apu
1182 and out to the real 'buffer'. ok. sure. */
1184 if (channel & 2) {
1185 /* ok, we're an input mixer going from adc
1186 through the mixbuf to the other apus */
1188 if (!(channel & 0x01)) {
1189 pa = es->mixbuf->addr;
1190 } else {
1191 pa = es->mixbuf->addr + ESM_MIXBUF_SIZE / 2;
1194 /* we source from a 'magic' apu */
1195 bsize = ESM_MIXBUF_SIZE / 4; /* half of this channels alloc, in words */
1196 /* parallel in crap, see maestro reg 0xC [8-11] */
1197 route = 0x14 + channel - 2;
1198 es->apu_mode[channel] = 0x90; /* Input Mixer */
1199 } else {
1200 /* we're a rate converter taking
1201 input from the input apus and outputing it to
1202 system memory */
1203 if (!(channel & 0x01))
1204 pa = es->memory->addr;
1205 else
1206 pa = es->memory->addr + size * 2; /* size is in word */
1208 es->apu_mode[channel] = 0xB0; /* Sample Rate Converter */
1210 bsize = size;
1211 /* get input from inputing apu */
1212 route = es->apu[channel + 2];
1215 /* set the wavecache control reg */
1216 snd_es1968_program_wavecache(chip, es, channel, pa, 1);
1218 /* Offset to PCMBAR */
1219 pa -= chip->dma_buf_addr;
1220 pa >>= 1; /* words */
1222 /* base offset of dma calcs when reading the pointer
1223 on this left one */
1224 es->base[channel] = pa & 0xFFFF;
1226 pa |= 0x00400000; /* bit 22 -> System RAM */
1228 /* Begin loading the APU */
1229 for (i = 0; i < 16; i++)
1230 apu_set_register(chip, apu, i, 0x0000);
1232 /* need to enable subgroups.. and we should probably
1233 have different groups for different /dev/dsps.. */
1234 apu_set_register(chip, apu, 2, 0x8);
1236 /* Load the buffer into the wave engine */
1237 apu_set_register(chip, apu, 4, ((pa >> 16) & 0xFF) << 8);
1238 /* XXX reg is little endian.. */
1239 apu_set_register(chip, apu, 5, pa & 0xFFFF);
1240 apu_set_register(chip, apu, 6, (pa + bsize) & 0xFFFF);
1241 apu_set_register(chip, apu, 7, bsize);
1242 #if 0
1243 if (es->fmt & ESS_FMT_STEREO) /* ??? really ??? */
1244 apu_set_register(chip, apu, 7, bsize - 1);
1245 #endif
1247 /* clear effects/env.. */
1248 apu_set_register(chip, apu, 8, 0x00F0);
1249 /* amplitude now? sure. why not. */
1250 apu_set_register(chip, apu, 9, 0x0000);
1251 /* set filter tune, radius, polar pan */
1252 apu_set_register(chip, apu, 10, 0x8F08);
1253 /* route input */
1254 apu_set_register(chip, apu, 11, route);
1255 /* dma on, no envelopes, filter to all 1s) */
1256 // apu_set_register(chip, apu, 0, 0x400F);
1259 spin_lock_irqsave(&chip->reg_lock, flags);
1260 /* clear WP interupts */
1261 outw(1, chip->io_port + 0x04);
1262 /* enable WP ints */
1263 outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ);
1264 spin_unlock_irqrestore(&chip->reg_lock, flags);
1266 freq = runtime->rate;
1267 /* Sample Rate conversion APUs don't like 0x10000 for their rate */
1268 if (freq > 47999)
1269 freq = 47999;
1270 if (freq < 4000)
1271 freq = 4000;
1273 freq = snd_es1968_compute_rate(chip, freq);
1275 /* Load the frequency, turn on 6dB */
1276 snd_es1968_apu_set_freq(chip, es->apu[0], freq);
1277 snd_es1968_apu_set_freq(chip, es->apu[1], freq);
1279 /* fix mixer rate at 48khz. and its _must_ be 0x10000. */
1280 freq = 0x10000;
1281 snd_es1968_apu_set_freq(chip, es->apu[2], freq);
1282 snd_es1968_apu_set_freq(chip, es->apu[3], freq);
1285 /*******************
1286 * ALSA Interface *
1287 *******************/
1289 static int snd_es1968_pcm_prepare(snd_pcm_substream_t *substream)
1291 es1968_t *chip = snd_pcm_substream_chip(substream);
1292 snd_pcm_runtime_t *runtime = substream->runtime;
1293 esschan_t *es = snd_magic_cast(esschan_t, runtime->private_data, return -ENXIO);
1295 es->dma_size = snd_pcm_lib_buffer_bytes(substream);
1296 es->frag_size = snd_pcm_lib_period_bytes(substream);
1298 es->wav_shift = 1; /* maestro handles always 16bit */
1299 es->fmt = 0;
1300 if (snd_pcm_format_width(runtime->format) == 16)
1301 es->fmt |= ESS_FMT_16BIT;
1302 if (runtime->channels > 1) {
1303 es->fmt |= ESS_FMT_STEREO;
1304 if (es->fmt & ESS_FMT_16BIT) /* 8bit is already word shifted */
1305 es->wav_shift++;
1307 es->bob_freq = snd_es1968_calc_bob_rate(chip, es, runtime);
1309 switch (es->mode) {
1310 case ESM_MODE_PLAY:
1311 snd_es1968_playback_setup(chip, es, runtime);
1312 break;
1313 case ESM_MODE_CAPTURE:
1314 snd_es1968_capture_setup(chip, es, runtime);
1315 break;
1318 return 0;
1321 static int snd_es1968_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
1323 es1968_t *chip = snd_pcm_substream_chip(substream);
1324 esschan_t *es = snd_magic_cast(esschan_t, substream->runtime->private_data, return -ENXIO);
1325 unsigned long flags;
1327 switch (cmd) {
1328 case SNDRV_PCM_TRIGGER_START:
1329 case SNDRV_PCM_TRIGGER_RESUME:
1330 if (es->running)
1331 return 0;
1332 snd_es1968_bob_inc(chip, es->bob_freq);
1333 es->count = 0;
1334 es->hwptr = 0;
1335 snd_es1968_pcm_start(chip, es);
1336 spin_lock_irqsave(&chip->substream_lock, flags);
1337 list_add(&es->list, &chip->substream_list);
1338 spin_unlock_irqrestore(&chip->substream_lock, flags);
1339 break;
1340 case SNDRV_PCM_TRIGGER_STOP:
1341 case SNDRV_PCM_TRIGGER_SUSPEND:
1342 if (! es->running)
1343 return 0;
1344 snd_es1968_pcm_stop(chip, es);
1345 spin_lock_irqsave(&chip->substream_lock, flags);
1346 list_del(&es->list);
1347 spin_unlock_irqrestore(&chip->substream_lock, flags);
1348 snd_es1968_bob_dec(chip);
1349 break;
1351 return 0;
1354 static snd_pcm_uframes_t snd_es1968_pcm_pointer(snd_pcm_substream_t *substream)
1356 es1968_t *chip = snd_pcm_substream_chip(substream);
1357 esschan_t *es = snd_magic_cast(esschan_t, substream->runtime->private_data, return -ENXIO);
1358 unsigned int ptr;
1360 ptr = snd_es1968_get_dma_ptr(chip, es) << es->wav_shift;
1362 return bytes_to_frames(substream->runtime, ptr % es->dma_size);
1365 static snd_pcm_hardware_t snd_es1968_playback = {
1366 .info = (SNDRV_PCM_INFO_MMAP |
1367 SNDRV_PCM_INFO_MMAP_VALID |
1368 SNDRV_PCM_INFO_INTERLEAVED |
1369 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1370 /*SNDRV_PCM_INFO_PAUSE |*/
1371 SNDRV_PCM_INFO_RESUME),
1372 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1373 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1374 .rate_min = 4000,
1375 .rate_max = 48000,
1376 .channels_min = 1,
1377 .channels_max = 2,
1378 .buffer_bytes_max = 65536,
1379 .period_bytes_min = 256,
1380 .period_bytes_max = 65536,
1381 .periods_min = 1,
1382 .periods_max = 1024,
1383 .fifo_size = 0,
1386 static snd_pcm_hardware_t snd_es1968_capture = {
1387 .info = (SNDRV_PCM_INFO_NONINTERLEAVED |
1388 SNDRV_PCM_INFO_MMAP |
1389 SNDRV_PCM_INFO_MMAP_VALID |
1390 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1391 /*SNDRV_PCM_INFO_PAUSE |*/
1392 SNDRV_PCM_INFO_RESUME),
1393 .formats = /*SNDRV_PCM_FMTBIT_U8 |*/ SNDRV_PCM_FMTBIT_S16_LE,
1394 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1395 .rate_min = 4000,
1396 .rate_max = 48000,
1397 .channels_min = 1,
1398 .channels_max = 2,
1399 .buffer_bytes_max = 65536,
1400 .period_bytes_min = 256,
1401 .period_bytes_max = 65536,
1402 .periods_min = 1,
1403 .periods_max = 1024,
1404 .fifo_size = 0,
1407 /* *************************
1408 * DMA memory management *
1409 *************************/
1411 /* Because the Maestro can only take adresses relative to the PCM base adress
1412 register :( */
1414 static int calc_available_memory_size(es1968_t *chip)
1416 struct list_head *p;
1417 int max_size = 0;
1419 down(&chip->memory_mutex);
1420 list_for_each(p, &chip->buf_list) {
1421 esm_memory_t *buf = list_entry(p, esm_memory_t, list);
1422 if (buf->empty && buf->size > max_size)
1423 max_size = buf->size;
1425 up(&chip->memory_mutex);
1426 if (max_size >= 128*1024)
1427 max_size = 127*1024;
1428 return max_size;
1431 /* allocate a new memory chunk with the specified size */
1432 static esm_memory_t *snd_es1968_new_memory(es1968_t *chip, int size)
1434 esm_memory_t *buf;
1435 struct list_head *p;
1437 down(&chip->memory_mutex);
1438 list_for_each(p, &chip->buf_list) {
1439 buf = list_entry(p, esm_memory_t, list);
1440 if (buf->empty && buf->size >= size)
1441 goto __found;
1443 up(&chip->memory_mutex);
1444 return NULL;
1446 __found:
1447 if (buf->size > size) {
1448 esm_memory_t *chunk = kmalloc(sizeof(*chunk), GFP_KERNEL);
1449 if (chunk == NULL) {
1450 up(&chip->memory_mutex);
1451 return NULL;
1453 chunk->size = buf->size - size;
1454 chunk->buf = buf->buf + size;
1455 chunk->addr = buf->addr + size;
1456 chunk->empty = 1;
1457 buf->size = size;
1458 list_add(&chunk->list, &buf->list);
1460 buf->empty = 0;
1461 up(&chip->memory_mutex);
1462 return buf;
1465 /* free a memory chunk */
1466 static void snd_es1968_free_memory(es1968_t *chip, esm_memory_t *buf)
1468 esm_memory_t *chunk;
1470 down(&chip->memory_mutex);
1471 buf->empty = 1;
1472 if (buf->list.prev != &chip->buf_list) {
1473 chunk = list_entry(buf->list.prev, esm_memory_t, list);
1474 if (chunk->empty) {
1475 chunk->size += buf->size;
1476 list_del(&buf->list);
1477 kfree(buf);
1478 buf = chunk;
1481 if (buf->list.next != &chip->buf_list) {
1482 chunk = list_entry(buf->list.next, esm_memory_t, list);
1483 if (chunk->empty) {
1484 buf->size += chunk->size;
1485 list_del(&chunk->list);
1486 kfree(chunk);
1489 up(&chip->memory_mutex);
1492 static void snd_es1968_free_dmabuf(es1968_t *chip)
1494 struct list_head *p;
1496 if (! chip->dma_buf)
1497 return;
1498 snd_free_pci_pages(chip->pci, chip->dma_buf_size, chip->dma_buf, chip->dma_buf_addr);
1499 while ((p = chip->buf_list.next) != &chip->buf_list) {
1500 esm_memory_t *chunk = list_entry(p, esm_memory_t, list);
1501 list_del(p);
1502 kfree(chunk);
1506 static int __devinit
1507 snd_es1968_init_dmabuf(es1968_t *chip)
1509 esm_memory_t *chunk;
1510 chip->dma_buf = snd_malloc_pci_pages_fallback(chip->pci, chip->total_bufsize,
1511 &chip->dma_buf_addr, &chip->dma_buf_size);
1512 //snd_printd("es1968: allocated buffer size %ld at %p\n", chip->dma_buf_size, chip->dma_buf);
1513 if (chip->dma_buf == NULL) {
1514 snd_printk("es1968: can't allocate dma pages for size %d\n",
1515 chip->total_bufsize);
1516 return -ENOMEM;
1518 if ((chip->dma_buf_addr + chip->dma_buf_size - 1) & ~((1 << 28) - 1)) {
1519 snd_es1968_free_dmabuf(chip);
1520 snd_printk("es1968: DMA buffer beyond 256MB.\n");
1521 return -ENOMEM;
1524 INIT_LIST_HEAD(&chip->buf_list);
1525 /* allocate an empty chunk */
1526 chunk = kmalloc(sizeof(*chunk), GFP_KERNEL);
1527 if (chunk == NULL) {
1528 snd_es1968_free_dmabuf(chip);
1529 return -ENOMEM;
1531 memset(chip->dma_buf, 0, 512);
1532 chunk->buf = chip->dma_buf + 512;
1533 chunk->addr = chip->dma_buf_addr + 512;
1534 chunk->size = chip->dma_buf_size - 512;
1535 chunk->empty = 1;
1536 list_add(&chunk->list, &chip->buf_list);
1538 return 0;
1541 /* setup the dma_areas */
1542 /* buffer is extracted from the pre-allocated memory chunk */
1543 static int snd_es1968_hw_params(snd_pcm_substream_t *substream,
1544 snd_pcm_hw_params_t *hw_params)
1546 es1968_t *chip = snd_pcm_substream_chip(substream);
1547 snd_pcm_runtime_t *runtime = substream->runtime;
1548 esschan_t *chan = snd_magic_cast(esschan_t, runtime->private_data, return -ENXIO);
1549 int size = params_buffer_bytes(hw_params);
1551 if (chan->memory) {
1552 if (chan->memory->size >= size) {
1553 runtime->dma_bytes = size;
1554 return 0;
1556 snd_es1968_free_memory(chip, chan->memory);
1558 chan->memory = snd_es1968_new_memory(chip, size);
1559 if (chan->memory == NULL) {
1560 // snd_printd("cannot allocate dma buffer: size = %d\n", size);
1561 return -ENOMEM;
1563 runtime->dma_bytes = size;
1564 runtime->dma_area = chan->memory->buf;
1565 runtime->dma_addr = chan->memory->addr;
1566 return 1; /* area was changed */
1569 /* remove dma areas if allocated */
1570 static int snd_es1968_hw_free(snd_pcm_substream_t * substream)
1572 es1968_t *chip = snd_pcm_substream_chip(substream);
1573 snd_pcm_runtime_t *runtime = substream->runtime;
1574 esschan_t *chan;
1576 if (runtime->private_data == NULL)
1577 return 0;
1578 chan = snd_magic_cast(esschan_t, runtime->private_data, return -ENXIO);
1579 if (chan->memory) {
1580 snd_es1968_free_memory(chip, chan->memory);
1581 chan->memory = NULL;
1583 return 0;
1588 * allocate APU pair
1590 static int snd_es1968_alloc_apu_pair(es1968_t *chip, int type)
1592 int apu;
1594 for (apu = 0; apu < NR_APUS; apu += 2) {
1595 if (chip->apu[apu] == ESM_APU_FREE &&
1596 chip->apu[apu + 1] == ESM_APU_FREE) {
1597 chip->apu[apu] = chip->apu[apu + 1] = type;
1598 return apu;
1601 return -EBUSY;
1605 * release APU pair
1607 static void snd_es1968_free_apu_pair(es1968_t *chip, int apu)
1609 chip->apu[apu] = chip->apu[apu + 1] = ESM_APU_FREE;
1613 /******************
1614 * PCM open/close *
1615 ******************/
1617 static int snd_es1968_playback_open(snd_pcm_substream_t *substream)
1619 es1968_t *chip = snd_pcm_substream_chip(substream);
1620 snd_pcm_runtime_t *runtime = substream->runtime;
1621 esschan_t *es;
1622 int apu1;
1624 /* search 2 APUs */
1625 apu1 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_PLAY);
1626 if (apu1 < 0)
1627 return apu1;
1629 es = snd_magic_kcalloc(esschan_t, 0, GFP_KERNEL);
1630 if (!es) {
1631 snd_es1968_free_apu_pair(chip, apu1);
1632 return -ENOMEM;
1635 es->apu[0] = apu1;
1636 es->apu[1] = apu1 + 1;
1637 es->apu_mode[0] = 0;
1638 es->apu_mode[1] = 0;
1639 es->running = 0;
1640 es->substream = substream;
1641 es->mode = ESM_MODE_PLAY;
1642 INIT_LIST_HEAD(&es->list);
1644 runtime->private_data = es;
1645 runtime->hw = snd_es1968_playback;
1646 runtime->hw.buffer_bytes_max = runtime->hw.period_bytes_max =
1647 calc_available_memory_size(chip);
1649 return 0;
1652 static int snd_es1968_capture_copy(snd_pcm_substream_t *substream,
1653 int channel, snd_pcm_uframes_t pos,
1654 void *buf, snd_pcm_uframes_t count)
1656 //es1968_t *chip = snd_pcm_substream_chip(substream);
1657 snd_pcm_runtime_t *runtime = substream->runtime;
1658 esschan_t *es = snd_magic_cast(esschan_t, runtime->private_data, return -ENXIO);
1659 char *src = runtime->dma_area;
1661 if (runtime->channels == 1)
1662 return copy_to_user(buf, src + pos, count) ? -EFAULT : 0;
1663 else {
1664 count /= 2;
1665 pos /= 2;
1666 if (copy_to_user(buf, src + pos, count))
1667 return -EFAULT;
1668 if (copy_to_user(buf + count, src + pos + es->dma_size/2, count))
1669 return -EFAULT;
1670 return 0;
1674 static int snd_es1968_capture_open(snd_pcm_substream_t *substream)
1676 snd_pcm_runtime_t *runtime = substream->runtime;
1677 es1968_t *chip = snd_pcm_substream_chip(substream);
1678 esschan_t *es;
1679 int apu1, apu2;
1681 apu1 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_CAPTURE);
1682 if (apu1 < 0)
1683 return apu1;
1684 apu2 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_RATECONV);
1685 if (apu2 < 0) {
1686 snd_es1968_free_apu_pair(chip, apu1);
1687 return apu2;
1690 es = snd_magic_kcalloc(esschan_t, 0, GFP_KERNEL);
1691 if (!es) {
1692 snd_es1968_free_apu_pair(chip, apu1);
1693 snd_es1968_free_apu_pair(chip, apu2);
1694 return -ENOMEM;
1697 es->apu[0] = apu1;
1698 es->apu[1] = apu1 + 1;
1699 es->apu[2] = apu2;
1700 es->apu[3] = apu2 + 1;
1701 es->apu_mode[0] = 0;
1702 es->apu_mode[1] = 0;
1703 es->apu_mode[2] = 0;
1704 es->apu_mode[3] = 0;
1705 es->running = 0;
1706 es->substream = substream;
1707 es->mode = ESM_MODE_CAPTURE;
1708 INIT_LIST_HEAD(&es->list);
1710 /* get mixbuffer */
1711 if ((es->mixbuf = snd_es1968_new_memory(chip, ESM_MIXBUF_SIZE)) == NULL) {
1712 snd_es1968_free_apu_pair(chip, apu1);
1713 snd_es1968_free_apu_pair(chip, apu2);
1714 snd_magic_kfree(es);
1715 return -ENOMEM;
1718 runtime->private_data = es;
1719 runtime->hw = snd_es1968_capture;
1720 runtime->hw.buffer_bytes_max = runtime->hw.period_bytes_max =
1721 calc_available_memory_size(chip) - 1024;
1723 return 0;
1726 static int snd_es1968_playback_close(snd_pcm_substream_t * substream)
1728 es1968_t *chip = snd_pcm_substream_chip(substream);
1729 esschan_t *es;
1730 if (substream->runtime->private_data == NULL)
1731 return 0;
1732 es = snd_magic_cast(esschan_t, substream->runtime->private_data, return -ENXIO);
1733 snd_es1968_free_apu_pair(chip, es->apu[0]);
1734 snd_magic_kfree(es);
1736 return 0;
1739 static int snd_es1968_capture_close(snd_pcm_substream_t * substream)
1741 es1968_t *chip = snd_pcm_substream_chip(substream);
1742 esschan_t *es;
1743 if (substream->runtime->private_data == NULL)
1744 return 0;
1745 es = snd_magic_cast(esschan_t, substream->runtime->private_data, return -ENXIO);
1746 snd_es1968_free_memory(chip, es->mixbuf);
1747 snd_es1968_free_apu_pair(chip, es->apu[0]);
1748 snd_es1968_free_apu_pair(chip, es->apu[2]);
1749 snd_magic_kfree(es);
1751 return 0;
1754 static snd_pcm_ops_t snd_es1968_playback_ops = {
1755 .open = snd_es1968_playback_open,
1756 .close = snd_es1968_playback_close,
1757 .ioctl = snd_pcm_lib_ioctl,
1758 .hw_params = snd_es1968_hw_params,
1759 .hw_free = snd_es1968_hw_free,
1760 .prepare = snd_es1968_pcm_prepare,
1761 .trigger = snd_es1968_pcm_trigger,
1762 .pointer = snd_es1968_pcm_pointer,
1765 static snd_pcm_ops_t snd_es1968_capture_ops = {
1766 .open = snd_es1968_capture_open,
1767 .close = snd_es1968_capture_close,
1768 .ioctl = snd_pcm_lib_ioctl,
1769 .hw_params = snd_es1968_hw_params,
1770 .hw_free = snd_es1968_hw_free,
1771 .prepare = snd_es1968_pcm_prepare,
1772 .trigger = snd_es1968_pcm_trigger,
1773 .pointer = snd_es1968_pcm_pointer,
1774 .copy = snd_es1968_capture_copy,
1779 * measure clock
1781 #define CLOCK_MEASURE_BUFSIZE 16768 /* enough large for a single shot */
1783 static void __devinit es1968_measure_clock(es1968_t *chip)
1785 int i, apu;
1786 unsigned int pa, offset, t;
1787 esm_memory_t *memory;
1788 unsigned long flags;
1789 struct timeval start_time, stop_time;
1791 if (chip->clock == 0)
1792 chip->clock = 48000; /* default clock value */
1794 /* search 2 APUs (although one apu is enough) */
1795 if ((apu = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_PLAY)) < 0) {
1796 snd_printk("Hmm, cannot find empty APU pair!?\n");
1797 return;
1799 if ((memory = snd_es1968_new_memory(chip, CLOCK_MEASURE_BUFSIZE)) == NULL) {
1800 snd_printk("cannot allocate dma buffer - using default clock %d\n", chip->clock);
1801 snd_es1968_free_apu_pair(chip, apu);
1802 return;
1805 memset(memory->buf, 0, CLOCK_MEASURE_BUFSIZE);
1807 wave_set_register(chip, apu << 3, (memory->addr - 0x10) & 0xfff8);
1809 pa = (unsigned int)((memory->addr - chip->dma_buf_addr) >> 1);
1810 pa |= 0x00400000; /* System RAM (Bit 22) */
1812 /* initialize apu */
1813 for (i = 0; i < 16; i++)
1814 apu_set_register(chip, apu, i, 0x0000);
1816 apu_set_register(chip, apu, 4, ((pa >> 16) & 0xff) << 8);
1817 apu_set_register(chip, apu, 5, pa & 0xffff);
1818 apu_set_register(chip, apu, 6, (pa + CLOCK_MEASURE_BUFSIZE/2) & 0xffff);
1819 apu_set_register(chip, apu, 7, CLOCK_MEASURE_BUFSIZE/2);
1820 apu_set_register(chip, apu, 8, 0x0000);
1821 apu_set_register(chip, apu, 9, 0xD000);
1822 apu_set_register(chip, apu, 10, 0x8F08);
1823 apu_set_register(chip, apu, 11, 0x0000);
1824 spin_lock_irqsave(&chip->reg_lock, flags);
1825 outw(1, chip->io_port + 0x04); /* clear WP interupts */
1826 outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ); /* enable WP ints */
1827 spin_unlock_irqrestore(&chip->reg_lock, flags);
1829 snd_es1968_apu_set_freq(chip, apu, ((unsigned int)48000 << 16) / chip->clock); /* 48000 Hz */
1831 spin_lock_irqsave(&chip->reg_lock, flags);
1832 __apu_set_register(chip, apu, 5, pa & 0xffff);
1833 snd_es1968_trigger_apu(chip, apu, 0x10); /* 16bit mono */
1834 do_gettimeofday(&start_time);
1835 spin_unlock_irqrestore(&chip->reg_lock, flags);
1836 #if 0
1837 set_current_state(TASK_UNINTERRUPTIBLE);
1838 schedule_timeout(HZ / 20); /* 50 msec */
1839 #else
1840 /* FIXME:
1841 * schedule() above may be too inaccurate and the pointer can
1842 * overlap the boundary..
1844 mdelay(50);
1845 #endif
1846 spin_lock_irqsave(&chip->reg_lock, flags);
1847 offset = __apu_get_register(chip, apu, 5);
1848 do_gettimeofday(&stop_time);
1849 snd_es1968_trigger_apu(chip, apu, 0); /* stop */
1850 spin_unlock_irqrestore(&chip->reg_lock, flags);
1852 /* check the current position */
1853 offset -= (pa & 0xffff);
1854 offset &= 0xfffe;
1856 t = stop_time.tv_sec - start_time.tv_sec;
1857 t *= 1000000;
1858 if (stop_time.tv_usec < start_time.tv_usec)
1859 t -= start_time.tv_usec - stop_time.tv_usec;
1860 else
1861 t += stop_time.tv_usec - start_time.tv_usec;
1862 if (t == 0) {
1863 snd_printk("?? calculation error..\n");
1864 } else {
1865 offset *= 1000;
1866 offset = (offset / t) * 1000 + ((offset % t) * 1000) / t;
1867 if (offset < 47500 || offset > 48500) {
1868 if (offset >= 40000 && offset <= 50000)
1869 chip->clock = (chip->clock * offset) / 48000;
1871 printk(KERN_INFO "es1968: clocking to %d\n", chip->clock);
1873 snd_es1968_free_memory(chip, memory);
1874 snd_es1968_free_apu_pair(chip, apu);
1881 static void snd_es1968_pcm_free(snd_pcm_t *pcm)
1883 es1968_t *esm = snd_magic_cast(es1968_t, pcm->private_data, return);
1884 snd_es1968_free_dmabuf(esm);
1885 esm->pcm = NULL;
1888 static int __devinit
1889 snd_es1968_pcm(es1968_t *chip, int device)
1891 snd_pcm_t *pcm;
1892 int err;
1894 /* get DMA buffer */
1895 if ((err = snd_es1968_init_dmabuf(chip)) < 0)
1896 return err;
1898 /* set PCMBAR */
1899 wave_set_register(chip, 0x01FC, chip->dma_buf_addr >> 12);
1900 wave_set_register(chip, 0x01FD, chip->dma_buf_addr >> 12);
1901 wave_set_register(chip, 0x01FE, chip->dma_buf_addr >> 12);
1902 wave_set_register(chip, 0x01FF, chip->dma_buf_addr >> 12);
1904 if ((err = snd_pcm_new(chip->card, "ESS Maestro", device,
1905 chip->playback_streams,
1906 chip->capture_streams, &pcm)) < 0)
1907 return err;
1909 pcm->private_data = chip;
1910 pcm->private_free = snd_es1968_pcm_free;
1912 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1968_playback_ops);
1913 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1968_capture_ops);
1915 pcm->info_flags = 0;
1917 strcpy(pcm->name, "ESS Maestro");
1919 chip->pcm = pcm;
1921 return 0;
1925 * update pointer
1927 static void snd_es1968_update_pcm(es1968_t *chip, esschan_t *es)
1929 unsigned int hwptr;
1930 unsigned int diff;
1931 snd_pcm_substream_t *subs = es->substream;
1933 if (subs == NULL || !es->running)
1934 return;
1936 hwptr = snd_es1968_get_dma_ptr(chip, es) << es->wav_shift;
1937 hwptr %= es->dma_size;
1939 diff = (es->dma_size + hwptr - es->hwptr) % es->dma_size;
1941 es->hwptr = hwptr;
1942 es->count += diff;
1944 if (es->count > es->frag_size) {
1945 spin_unlock(&chip->substream_lock);
1946 snd_pcm_period_elapsed(subs);
1947 spin_lock(&chip->substream_lock);
1948 es->count %= es->frag_size;
1954 static void es1968_update_hw_volume(unsigned long private_data)
1956 es1968_t *chip = snd_magic_cast(es1968_t, (void*)private_data, return);
1957 int x, val;
1959 /* Figure out which volume control button was pushed,
1960 based on differences from the default register
1961 values. */
1962 x = inb(chip->io_port + 0x1c);
1963 /* Reset the volume control registers. */
1964 outb(0x88, chip->io_port + 0x1c);
1965 outb(0x88, chip->io_port + 0x1d);
1966 outb(0x88, chip->io_port + 0x1e);
1967 outb(0x88, chip->io_port + 0x1f);
1969 if (! chip->master_switch || ! chip->master_volume)
1970 return;
1972 /* FIXME: more clean up is needed.. */
1973 val = chip->ac97->regs[AC97_MASTER];
1974 if (x & 1) {
1975 /* mute */
1976 snd_ac97_write_cache(chip->ac97, AC97_MASTER, val ^ 0x8000);
1977 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1978 &chip->master_switch->id);
1979 } else {
1980 val &= 0x7fff;
1981 if (((x>>1) & 7) > 4) {
1982 /* volume up */
1983 if ((val & 0xff) > 0)
1984 val--;
1985 if ((val & 0xff00) > 0)
1986 val -= 0x0100;
1987 } else {
1988 /* volume down */
1989 if ((val & 0xff) < 0x1f)
1990 val++;
1991 if ((val & 0xff00) < 0x1f00)
1992 val += 0x0100;
1994 snd_ac97_write_cache(chip->ac97, AC97_MASTER, val);
1995 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1996 &chip->master_volume->id);
2001 * interrupt handler
2003 static void snd_es1968_interrupt(int irq, void *dev_id, struct pt_regs *regs)
2005 es1968_t *chip = snd_magic_cast(es1968_t, dev_id, return);
2006 u32 event;
2008 if (!(event = inb(chip->io_port + 0x1A)))
2009 return;
2011 outw(inw(chip->io_port + 4) & 1, chip->io_port + 4);
2013 if (event & ESM_HWVOL_IRQ)
2014 tasklet_hi_schedule(&chip->hwvol_tq); /* we'll do this later */
2016 /* else ack 'em all, i imagine */
2017 outb(0xFF, chip->io_port + 0x1A);
2019 if ((event & ESM_MPU401_IRQ) && chip->rmidi) {
2020 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
2023 if (event & ESM_SOUND_IRQ) {
2024 struct list_head *p, *n;
2025 spin_lock(&chip->substream_lock);
2026 /* we need to use list_for_each_safe here since the substream
2027 * can be deleted in period_elapsed().
2029 list_for_each_safe(p, n, &chip->substream_list) {
2030 esschan_t *es = list_entry(p, esschan_t, list);
2031 snd_es1968_update_pcm(chip, es);
2033 spin_unlock(&chip->substream_lock);
2038 * Mixer stuff
2041 static int __devinit
2042 snd_es1968_mixer(es1968_t *chip)
2044 ac97_t ac97;
2045 snd_ctl_elem_id_t id;
2046 int err;
2048 memset(&ac97, 0, sizeof(ac97));
2049 ac97.write = snd_es1968_ac97_write;
2050 ac97.read = snd_es1968_ac97_read;
2051 ac97.private_data = chip;
2052 if ((err = snd_ac97_mixer(chip->card, &ac97, &chip->ac97)) < 0)
2053 return err;
2055 /* attach master switch / volumes for h/w volume control */
2056 memset(&id, 0, sizeof(id));
2057 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2058 strcpy(id.name, "Master Playback Switch");
2059 chip->master_switch = snd_ctl_find_id(chip->card, &id);
2060 memset(&id, 0, sizeof(id));
2061 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2062 strcpy(id.name, "Master Playback Volume");
2063 chip->master_volume = snd_ctl_find_id(chip->card, &id);
2065 return 0;
2069 * reset ac97 codec
2072 static void snd_es1968_ac97_reset(es1968_t *chip)
2074 unsigned long ioaddr = chip->io_port;
2076 unsigned short save_ringbus_a;
2077 unsigned short save_68;
2078 unsigned short w;
2079 unsigned int vend;
2081 /* save configuration */
2082 save_ringbus_a = inw(ioaddr + 0x36);
2084 //outw(inw(ioaddr + 0x38) & 0xfffc, ioaddr + 0x38); /* clear second codec id? */
2085 /* set command/status address i/o to 1st codec */
2086 outw(inw(ioaddr + 0x3a) & 0xfffc, ioaddr + 0x3a);
2087 outw(inw(ioaddr + 0x3c) & 0xfffc, ioaddr + 0x3c);
2089 /* disable ac link */
2090 outw(0x0000, ioaddr + 0x36);
2091 save_68 = inw(ioaddr + 0x68);
2092 pci_read_config_word(chip->pci, 0x58, &w); /* something magical with gpio and bus arb. */
2093 pci_read_config_dword(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, &vend);
2094 if (w & 1)
2095 save_68 |= 0x10;
2096 outw(0xfffe, ioaddr + 0x64); /* unmask gpio 0 */
2097 outw(0x0001, ioaddr + 0x68); /* gpio write */
2098 outw(0x0000, ioaddr + 0x60); /* write 0 to gpio 0 */
2099 udelay(20);
2100 outw(0x0001, ioaddr + 0x60); /* write 1 to gpio 1 */
2101 mdelay(20);
2103 outw(save_68 | 0x1, ioaddr + 0x68); /* now restore .. */
2104 outw((inw(ioaddr + 0x38) & 0xfffc) | 0x1, ioaddr + 0x38);
2105 outw((inw(ioaddr + 0x3a) & 0xfffc) | 0x1, ioaddr + 0x3a);
2106 outw((inw(ioaddr + 0x3c) & 0xfffc) | 0x1, ioaddr + 0x3c);
2108 /* now the second codec */
2109 /* disable ac link */
2110 outw(0x0000, ioaddr + 0x36);
2111 outw(0xfff7, ioaddr + 0x64); /* unmask gpio 3 */
2112 save_68 = inw(ioaddr + 0x68);
2113 outw(0x0009, ioaddr + 0x68); /* gpio write 0 & 3 ?? */
2114 outw(0x0001, ioaddr + 0x60); /* write 1 to gpio */
2115 udelay(20);
2116 outw(0x0009, ioaddr + 0x60); /* write 9 to gpio */
2117 mdelay(500); /* .. ouch.. */
2118 //outw(inw(ioaddr + 0x38) & 0xfffc, ioaddr + 0x38);
2119 outw(inw(ioaddr + 0x3a) & 0xfffc, ioaddr + 0x3a);
2120 outw(inw(ioaddr + 0x3c) & 0xfffc, ioaddr + 0x3c);
2122 #if 0 /* the loop here needs to be much better if we want it.. */
2123 snd_printk("trying software reset\n");
2124 /* try and do a software reset */
2125 outb(0x80 | 0x7c, ioaddr + 0x30);
2126 for (w = 0;; w++) {
2127 if ((inw(ioaddr + 0x30) & 1) == 0) {
2128 if (inb(ioaddr + 0x32) != 0)
2129 break;
2131 outb(0x80 | 0x7d, ioaddr + 0x30);
2132 if (((inw(ioaddr + 0x30) & 1) == 0)
2133 && (inb(ioaddr + 0x32) != 0))
2134 break;
2135 outb(0x80 | 0x7f, ioaddr + 0x30);
2136 if (((inw(ioaddr + 0x30) & 1) == 0)
2137 && (inb(ioaddr + 0x32) != 0))
2138 break;
2141 if (w > 10000) {
2142 outb(inb(ioaddr + 0x37) | 0x08, ioaddr + 0x37); /* do a software reset */
2143 mdelay(500); /* oh my.. */
2144 outb(inb(ioaddr + 0x37) & ~0x08,
2145 ioaddr + 0x37);
2146 udelay(1);
2147 outw(0x80, ioaddr + 0x30);
2148 for (w = 0; w < 10000; w++) {
2149 if ((inw(ioaddr + 0x30) & 1) == 0)
2150 break;
2154 #endif
2155 if (vend == NEC_VERSA_SUBID1 || vend == NEC_VERSA_SUBID2) {
2156 /* turn on external amp? */
2157 outw(0xf9ff, ioaddr + 0x64);
2158 outw(inw(ioaddr + 0x68) | 0x600, ioaddr + 0x68);
2159 outw(0x0209, ioaddr + 0x60);
2162 /* restore.. */
2163 outw(save_ringbus_a, ioaddr + 0x36);
2165 /* Turn on the 978 docking chip.
2166 First frob the "master output enable" bit,
2167 then set most of the playback volume control registers to max. */
2168 outb(inb(ioaddr+0xc0)|(1<<5), ioaddr+0xc0);
2169 outb(0xff, ioaddr+0xc3);
2170 outb(0xff, ioaddr+0xc4);
2171 outb(0xff, ioaddr+0xc6);
2172 outb(0xff, ioaddr+0xc8);
2173 outb(0x3f, ioaddr+0xcf);
2174 outb(0x3f, ioaddr+0xd0);
2177 static void snd_es1968_reset(es1968_t *chip)
2179 /* Reset */
2180 outw(ESM_RESET_MAESTRO | ESM_RESET_DIRECTSOUND,
2181 chip->io_port + ESM_PORT_HOST_IRQ);
2182 udelay(10);
2183 outw(0x0000, chip->io_port + ESM_PORT_HOST_IRQ);
2184 udelay(10);
2188 * power management
2190 static void snd_es1968_set_acpi(es1968_t *chip, int state)
2192 u16 active_mask = acpi_state_mask[state];
2194 pci_set_power_state(chip->pci, state);
2195 /* make sure the units we care about are on
2196 XXX we might want to do this before state flipping? */
2197 pci_write_config_word(chip->pci, 0x54, ~ active_mask);
2198 pci_write_config_word(chip->pci, 0x56, ~ active_mask);
2203 * initialize maestro chip
2205 static void snd_es1968_chip_init(es1968_t *chip)
2207 struct pci_dev *pci = chip->pci;
2208 int i;
2209 unsigned long iobase = chip->io_port;
2210 u16 w;
2211 u32 n;
2213 /* We used to muck around with pci config space that
2214 * we had no business messing with. We don't know enough
2215 * about the machine to know which DMA mode is appropriate,
2216 * etc. We were guessing wrong on some machines and making
2217 * them unhappy. We now trust in the BIOS to do things right,
2218 * which almost certainly means a new host of problems will
2219 * arise with broken BIOS implementations. screw 'em.
2220 * We're already intolerant of machines that don't assign
2221 * IRQs.
2224 /* do config work at full power */
2225 snd_es1968_set_acpi(chip, ACPI_D0);
2227 /* Config Reg A */
2228 pci_read_config_word(pci, ESM_CONFIG_A, &w);
2230 /* Use TDMA for now. TDMA works on all boards, so while its
2231 * not the most efficient its the simplest. */
2232 w &= ~DMA_CLEAR; /* Clear DMA bits */
2233 w |= DMA_TDMA; /* TDMA on */
2234 w &= ~(PIC_SNOOP1 | PIC_SNOOP2); /* Clear Pic Snoop Mode Bits */
2235 w &= ~SAFEGUARD; /* Safeguard off */
2236 w |= POST_WRITE; /* Posted write */
2237 w |= ISA_TIMING; /* ISA timing on */
2238 /* XXX huh? claims to be reserved.. */
2239 w &= ~SWAP_LR; /* swap left/right
2240 seems to only have effect on SB
2241 Emulation */
2242 w &= ~SUBTR_DECODE; /* Subtractive decode off */
2244 pci_write_config_word(pci, ESM_CONFIG_A, w);
2246 /* Config Reg B */
2248 pci_read_config_word(pci, ESM_CONFIG_B, &w);
2250 w &= ~(1 << 15); /* Turn off internal clock multiplier */
2251 /* XXX how do we know which to use? */
2252 w &= ~(1 << 14); /* External clock */
2254 w &= ~SPDIF_CONFB; /* disable S/PDIF output */
2255 w |= HWV_CONFB; /* HWV on */
2256 w |= DEBOUNCE; /* Debounce off: easier to push the HW buttons */
2257 w &= ~GPIO_CONFB; /* GPIO 4:5 */
2258 w |= CHI_CONFB; /* Disconnect from the CHI. Enabling this made a dell 7500 work. */
2259 w &= ~IDMA_CONFB; /* IDMA off (undocumented) */
2260 w &= ~MIDI_FIX; /* MIDI fix off (undoc) */
2261 w &= ~(1 << 1); /* reserved, always write 0 */
2262 w &= ~IRQ_TO_ISA; /* IRQ to ISA off (undoc) */
2264 pci_write_config_word(pci, ESM_CONFIG_B, w);
2266 /* DDMA off */
2268 pci_read_config_word(pci, ESM_DDMA, &w);
2269 w &= ~(1 << 0);
2270 pci_write_config_word(pci, ESM_DDMA, w);
2273 * Legacy mode
2276 pci_read_config_word(pci, ESM_LEGACY_AUDIO_CONTROL, &w);
2278 w &= ~ESS_ENABLE_AUDIO; /* Disable Legacy Audio */
2279 w &= ~ESS_ENABLE_SERIAL_IRQ; /* Disable SIRQ */
2280 w &= ~(0x1f); /* disable mpu irq/io, game port, fm, SB */
2282 pci_write_config_word(pci, ESM_LEGACY_AUDIO_CONTROL, w);
2284 /* Set up 978 docking control chip. */
2285 pci_read_config_word(pci, 0x58, &w);
2286 w|=1<<2; /* Enable 978. */
2287 w|=1<<3; /* Turn on 978 hardware volume control. */
2288 w&=~(1<<11); /* Turn on 978 mixer volume control. */
2289 pci_write_config_word(pci, 0x58, w);
2291 /* Sound Reset */
2293 snd_es1968_reset(chip);
2296 * Ring Bus Setup
2299 /* setup usual 0x34 stuff.. 0x36 may be chip specific */
2300 outw(0xC090, iobase + ESM_RING_BUS_DEST); /* direct sound, stereo */
2301 udelay(20);
2302 outw(0x3000, iobase + ESM_RING_BUS_CONTR_A); /* enable ringbus/serial */
2303 udelay(20);
2306 * Reset the CODEC
2309 snd_es1968_ac97_reset(chip);
2311 /* Ring Bus Control B */
2313 n = inl(iobase + ESM_RING_BUS_CONTR_B);
2314 n &= ~RINGB_EN_SPDIF; /* SPDIF off */
2315 //w |= RINGB_EN_2CODEC; /* enable 2nd codec */
2316 outl(n, iobase + ESM_RING_BUS_CONTR_B);
2318 /* Set hardware volume control registers to midpoints.
2319 We can tell which button was pushed based on how they change. */
2320 outb(0x88, iobase+0x1c);
2321 outb(0x88, iobase+0x1d);
2322 outb(0x88, iobase+0x1e);
2323 outb(0x88, iobase+0x1f);
2325 /* it appears some maestros (dell 7500) only work if these are set,
2326 regardless of wether we use the assp or not. */
2328 outb(0, iobase + ASSP_CONTROL_B);
2329 outb(3, iobase + ASSP_CONTROL_A); /* M: Reserved bits... */
2330 outb(0, iobase + ASSP_CONTROL_C); /* M: Disable ASSP, ASSP IRQ's and FM Port */
2332 /* Enable IRQ's */
2333 w = ESM_HIRQ_DSIE | ESM_HIRQ_MPU401 | ESM_HIRQ_HW_VOLUME;
2334 outw(w, iobase + ESM_PORT_HOST_IRQ);
2338 * set up wavecache
2340 for (i = 0; i < 16; i++) {
2341 /* Write 0 into the buffer area 0x1E0->1EF */
2342 outw(0x01E0 + i, iobase + WC_INDEX);
2343 outw(0x0000, iobase + WC_DATA);
2345 /* The 1.10 test program seem to write 0 into the buffer area
2346 * 0x1D0-0x1DF too.*/
2347 outw(0x01D0 + i, iobase + WC_INDEX);
2348 outw(0x0000, iobase + WC_DATA);
2350 wave_set_register(chip, IDR7_WAVE_ROMRAM,
2351 (wave_get_register(chip, IDR7_WAVE_ROMRAM) & 0xFF00));
2352 wave_set_register(chip, IDR7_WAVE_ROMRAM,
2353 wave_get_register(chip, IDR7_WAVE_ROMRAM) | 0x100);
2354 wave_set_register(chip, IDR7_WAVE_ROMRAM,
2355 wave_get_register(chip, IDR7_WAVE_ROMRAM) & ~0x200);
2356 wave_set_register(chip, IDR7_WAVE_ROMRAM,
2357 wave_get_register(chip, IDR7_WAVE_ROMRAM) | ~0x400);
2360 maestro_write(chip, IDR2_CRAM_DATA, 0x0000);
2361 /* Now back to the DirectSound stuff */
2362 /* audio serial configuration.. ? */
2363 maestro_write(chip, 0x08, 0xB004);
2364 maestro_write(chip, 0x09, 0x001B);
2365 maestro_write(chip, 0x0A, 0x8000);
2366 maestro_write(chip, 0x0B, 0x3F37);
2367 maestro_write(chip, 0x0C, 0x0098);
2369 /* parallel in, has something to do with recording :) */
2370 maestro_write(chip, 0x0C,
2371 (maestro_read(chip, 0x0C) & ~0xF000) | 0x8000);
2372 /* parallel out */
2373 maestro_write(chip, 0x0C,
2374 (maestro_read(chip, 0x0C) & ~0x0F00) | 0x0500);
2376 maestro_write(chip, 0x0D, 0x7632);
2378 /* Wave cache control on - test off, sg off,
2379 enable, enable extra chans 1Mb */
2381 w = inw(iobase + WC_CONTROL);
2383 w &= ~0xFA00; /* Seems to be reserved? I don't know */
2384 w |= 0xA000; /* reserved... I don't know */
2385 w &= ~0x0200; /* Channels 56,57,58,59 as Extra Play,Rec Channel enable
2386 Seems to crash the Computer if enabled... */
2387 w |= 0x0100; /* Wave Cache Operation Enabled */
2388 w |= 0x0080; /* Channels 60/61 as Placback/Record enabled */
2389 w &= ~0x0060; /* Clear Wavtable Size */
2390 w |= 0x0020; /* Wavetable Size : 1MB */
2391 /* Bit 4 is reserved */
2392 w &= ~0x000C; /* DMA Stuff? I don't understand what the datasheet means */
2393 /* Bit 1 is reserved */
2394 w &= ~0x0001; /* Test Mode off */
2396 outw(w, iobase + WC_CONTROL);
2398 /* Now clear the APU control ram */
2399 for (i = 0; i < NR_APUS; i++) {
2400 for (w = 0; w < NR_APU_REGS; w++)
2401 apu_set_register(chip, i, w, 0);
2406 #ifdef CONFIG_PM
2408 * PM support
2410 static void es1968_suspend(es1968_t *chip)
2412 snd_card_t *card = chip->card;
2414 snd_power_lock(card);
2415 if (card->power_state == SNDRV_CTL_POWER_D3hot)
2416 goto __skip;
2418 snd_pcm_suspend_all(chip->pcm);
2419 snd_es1968_bob_stop(chip);
2420 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2421 __skip:
2422 snd_power_unlock(card);
2425 static void es1968_resume(es1968_t *chip)
2427 snd_card_t *card = chip->card;
2429 snd_power_lock(card);
2430 if (card->power_state == SNDRV_CTL_POWER_D0)
2431 goto __skip;
2433 /* restore all our config */
2434 pci_enable_device(chip->pci);
2435 snd_es1968_chip_init(chip);
2437 /* need to restore the base pointers.. */
2438 if (chip->dma_buf_addr) {
2439 /* set PCMBAR */
2440 wave_set_register(chip, 0x01FC, chip->dma_buf_addr >> 12);
2443 /* restore ac97 state */
2444 snd_ac97_resume(chip->ac97);
2446 /* start timer again */
2447 if (atomic_read(&chip->bobclient))
2448 snd_es1968_bob_start(chip);
2449 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2450 __skip:
2451 snd_power_unlock(card);
2454 #ifndef PCI_OLD_SUSPEND
2455 static int snd_es1968_suspend(struct pci_dev *dev, u32 state)
2457 es1968_t *chip = snd_magic_cast(es1968_t, pci_get_drvdata(dev), return -ENXIO);
2458 es1968_suspend(chip);
2459 return 0;
2461 static int snd_es1968_resume(struct pci_dev *dev)
2463 es1968_t *chip = snd_magic_cast(es1968_t, pci_get_drvdata(dev), return -ENXIO);
2464 es1968_resume(chip);
2465 return 0;
2467 #else
2468 static void snd_es1968_suspend(struct pci_dev *dev)
2470 es1968_t *chip = snd_magic_cast(es1968_t, pci_get_drvdata(dev), return);
2471 es1968_suspend(chip);
2473 static void snd_es1968_resume(struct pci_dev *dev)
2475 es1968_t *chip = snd_magic_cast(es1968_t, pci_get_drvdata(dev), return);
2476 es1968_resume(chip);
2478 #endif
2480 /* callback */
2481 static int snd_es1968_set_power_state(snd_card_t *card, unsigned int power_state)
2483 es1968_t *chip = snd_magic_cast(es1968_t, card->power_state_private_data, return -ENXIO);
2484 switch (power_state) {
2485 case SNDRV_CTL_POWER_D0:
2486 case SNDRV_CTL_POWER_D1:
2487 case SNDRV_CTL_POWER_D2:
2488 es1968_resume(chip);
2489 break;
2490 case SNDRV_CTL_POWER_D3hot:
2491 case SNDRV_CTL_POWER_D3cold:
2492 es1968_suspend(chip);
2493 break;
2494 default:
2495 return -EINVAL;
2497 return 0;
2500 #endif /* CONFIG_PM */
2502 static int snd_es1968_free(es1968_t *chip)
2504 if (chip->res_io_port)
2505 snd_es1968_reset(chip);
2507 snd_es1968_set_acpi(chip, ACPI_D3);
2508 chip->master_switch = NULL;
2509 chip->master_volume = NULL;
2510 if (chip->res_io_port) {
2511 release_resource(chip->res_io_port);
2512 kfree_nocheck(chip->res_io_port);
2514 if (chip->irq >= 0)
2515 free_irq(chip->irq, (void *)chip);
2516 snd_magic_kfree(chip);
2517 return 0;
2520 static int snd_es1968_dev_free(snd_device_t *device)
2522 es1968_t *chip = snd_magic_cast(es1968_t, device->device_data, return -ENXIO);
2523 return snd_es1968_free(chip);
2526 static int __devinit snd_es1968_create(snd_card_t * card,
2527 struct pci_dev *pci,
2528 int total_bufsize,
2529 int play_streams,
2530 int capt_streams,
2531 es1968_t **chip_ret)
2533 static snd_device_ops_t ops = {
2534 .dev_free = snd_es1968_dev_free,
2536 es1968_t *chip;
2537 int i, err;
2539 *chip_ret = NULL;
2541 /* enable PCI device */
2542 if ((err = pci_enable_device(pci)) < 0)
2543 return err;
2544 /* check, if we can restrict PCI DMA transfers to 28 bits */
2545 if (!pci_dma_supported(pci, 0x0fffffff)) {
2546 snd_printk("architecture does not support 28bit PCI busmaster DMA\n");
2547 return -ENXIO;
2549 pci_set_dma_mask(pci, 0x0fffffff);
2551 chip = (es1968_t *) snd_magic_kcalloc(es1968_t, 0, GFP_KERNEL);
2552 if (! chip)
2553 return -ENOMEM;
2555 /* Set Vars */
2556 chip->type = (pci->vendor << 16) | pci->device;
2557 spin_lock_init(&chip->reg_lock);
2558 spin_lock_init(&chip->substream_lock);
2559 spin_lock_init(&chip->bob_lock);
2560 INIT_LIST_HEAD(&chip->buf_list);
2561 INIT_LIST_HEAD(&chip->substream_list);
2562 init_MUTEX(&chip->memory_mutex);
2563 tasklet_init(&chip->hwvol_tq, es1968_update_hw_volume, (unsigned long)chip);
2564 chip->card = card;
2565 chip->pci = pci;
2566 chip->irq = -1;
2567 chip->total_bufsize = total_bufsize; /* in bytes */
2568 chip->playback_streams = play_streams;
2569 chip->capture_streams = capt_streams;
2571 chip->io_port = pci_resource_start(pci, 0);
2572 if ((chip->res_io_port = request_region(chip->io_port, 0x100, "ESS Maestro")) == NULL) {
2573 snd_es1968_free(chip);
2574 snd_printk("unable to grab region 0x%lx-0x%lx\n", chip->io_port, chip->io_port + 0x100 - 1);
2575 return -EBUSY;
2577 if (request_irq(pci->irq, snd_es1968_interrupt, SA_INTERRUPT|SA_SHIRQ,
2578 "ESS Maestro", (void*)chip)) {
2579 snd_es1968_free(chip);
2580 snd_printk("unable to grab IRQ %d\n", pci->irq);
2581 return -EBUSY;
2583 chip->irq = pci->irq;
2585 /* Clear Maestro_map */
2586 for (i = 0; i < 32; i++)
2587 chip->maestro_map[i] = 0;
2589 /* Clear Apu Map */
2590 for (i = 0; i < NR_APUS; i++)
2591 chip->apu[i] = ESM_APU_FREE;
2593 atomic_set(&chip->bobclient, 0);
2595 /* just to be sure */
2596 pci_set_master(pci);
2598 snd_es1968_chip_init(chip);
2600 #ifdef CONFIG_PM
2601 card->set_power_state = snd_es1968_set_power_state;
2602 card->power_state_private_data = chip;
2603 #endif
2605 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2606 snd_es1968_free(chip);
2607 return err;
2610 *chip_ret = chip;
2612 return 0;
2617 * joystick
2620 static int snd_es1968_joystick_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
2622 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2623 uinfo->count = 1;
2624 uinfo->value.integer.min = 0;
2625 uinfo->value.integer.max = 1;
2626 return 0;
2629 static int snd_es1968_joystick_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
2631 es1968_t *chip = snd_kcontrol_chip(kcontrol);
2632 u16 val;
2634 pci_read_config_word(chip->pci, ESM_LEGACY_AUDIO_CONTROL, &val);
2635 ucontrol->value.integer.value[0] = (val & 0x04) ? 1 : 0;
2636 return 0;
2639 static int snd_es1968_joystick_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
2641 es1968_t *chip = snd_kcontrol_chip(kcontrol);
2642 u16 val, oval;
2644 pci_read_config_word(chip->pci, ESM_LEGACY_AUDIO_CONTROL, &oval);
2645 val = oval & ~0x04;
2646 if (ucontrol->value.integer.value[0])
2647 val |= 0x04;
2648 if (val != oval); {
2649 pci_write_config_word(chip->pci, ESM_LEGACY_AUDIO_CONTROL, val);
2650 return 1;
2652 return 0;
2655 #define num_controls(ary) (sizeof(ary) / sizeof(snd_kcontrol_new_t))
2657 static snd_kcontrol_new_t snd_es1968_control_switches[] __devinitdata = {
2659 .name = "Joystick",
2660 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
2661 .info = snd_es1968_joystick_info,
2662 .get = snd_es1968_joystick_get,
2663 .put = snd_es1968_joystick_put,
2669 static int __devinit snd_es1968_probe(struct pci_dev *pci,
2670 const struct pci_device_id *pci_id)
2672 static int dev;
2673 snd_card_t *card;
2674 es1968_t *chip;
2675 int i, err;
2677 if (dev >= SNDRV_CARDS)
2678 return -ENODEV;
2679 if (!enable[dev]) {
2680 dev++;
2681 return -ENOENT;
2684 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2685 if (!card)
2686 return -ENOMEM;
2688 if (total_bufsize[dev] < 128)
2689 total_bufsize[dev] = 128;
2690 if (total_bufsize[dev] > 4096)
2691 total_bufsize[dev] = 4096;
2692 if ((err = snd_es1968_create(card, pci,
2693 total_bufsize[dev] * 1024, /* in bytes */
2694 pcm_substreams_p[dev],
2695 pcm_substreams_c[dev],
2696 &chip)) < 0) {
2697 snd_card_free(card);
2698 return err;
2701 switch (chip->type) {
2702 case CARD_TYPE_ESS_ES1978:
2703 strcpy(card->driver, "ES1978");
2704 strcpy(card->shortname, "ESS ES1978 (Maestro 2E)");
2705 break;
2706 case CARD_TYPE_ESS_ES1968:
2707 strcpy(card->driver, "ES1968");
2708 strcpy(card->shortname, "ESS ES1968 (Maestro 2)");
2709 break;
2710 case CARD_TYPE_ESS_ESOLDM1:
2711 strcpy(card->driver, "ESM1");
2712 strcpy(card->shortname, "ESS Maestro 1");
2713 break;
2716 if ((err = snd_es1968_pcm(chip, 0)) < 0) {
2717 snd_card_free(card);
2718 return err;
2721 if ((err = snd_es1968_mixer(chip)) < 0) {
2722 snd_card_free(card);
2723 return err;
2726 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
2727 chip->io_port + ESM_MPU401_PORT, 1,
2728 chip->irq, 0, &chip->rmidi)) < 0) {
2729 printk(KERN_WARNING "es1968: skipping MPU-401 MIDI support..\n");
2732 /* card switches */
2733 for (i = 0; i < num_controls(snd_es1968_control_switches); i++) {
2734 err = snd_ctl_add(card, snd_ctl_new1(&snd_es1968_control_switches[i], chip));
2735 if (err < 0) {
2736 snd_card_free(card);
2737 return err;
2741 chip->clock = clock[dev];
2742 if (! chip->clock)
2743 es1968_measure_clock(chip);
2745 sprintf(card->longname, "%s at 0x%lx, irq %i",
2746 card->shortname, chip->io_port, chip->irq);
2748 if ((err = snd_card_register(card)) < 0) {
2749 snd_card_free(card);
2750 return err;
2752 pci_set_drvdata(pci, chip);
2753 dev++;
2754 return 0;
2757 static void __devexit snd_es1968_remove(struct pci_dev *pci)
2759 es1968_t *chip = snd_magic_cast(es1968_t, pci_get_drvdata(pci), return);
2760 if (chip)
2761 snd_card_free(chip->card);
2762 pci_set_drvdata(pci, NULL);
2765 static struct pci_driver driver = {
2766 .name = "ES1968 (ESS Maestro)",
2767 .id_table = snd_es1968_ids,
2768 .probe = snd_es1968_probe,
2769 .remove = __devexit_p(snd_es1968_remove),
2770 #ifdef CONFIG_PM
2771 .suspend = snd_es1968_suspend,
2772 .resume = snd_es1968_resume,
2773 #endif
2776 #if 0 // do we really need this?
2777 static int snd_es1968_notifier(struct notifier_block *nb, unsigned long event, void *buf)
2779 pci_unregister_driver(&driver);
2780 return NOTIFY_OK;
2783 static struct notifier_block snd_es1968_nb = {snd_es1968_notifier, NULL, 0};
2784 #endif
2786 static int __init alsa_card_es1968_init(void)
2788 int err;
2790 if ((err = pci_module_init(&driver)) < 0) {
2791 #ifdef MODULE
2792 printk(KERN_ERR "ESS Maestro soundcard not found or device busy\n");
2793 #endif
2794 return err;
2796 #if 0 // do we really need this?
2797 /* If this driver is not shutdown cleanly at reboot, it can
2798 leave the speaking emitting an annoying noise, so we catch
2799 shutdown events. */
2800 if (register_reboot_notifier(&snd_es1968_nb)) {
2801 printk(KERN_ERR "reboot notifier registration failed; may make noise at shutdown.\n");
2803 #endif
2804 return 0;
2807 static void __exit alsa_card_es1968_exit(void)
2809 #if 0 // do we really need this?
2810 unregister_reboot_notifier(&snd_es1968_nb);
2811 #endif
2812 pci_unregister_driver(&driver);
2815 module_init(alsa_card_es1968_init)
2816 module_exit(alsa_card_es1968_exit)
2818 #ifndef MODULE
2820 /* format is: snd-es1968=enable,index,id,
2821 total_bufsize,
2822 pcm_substreams_p,
2823 pcm_substreams_c,
2824 clock
2827 static int __init alsa_card_es1968_setup(char *str)
2829 static unsigned __initdata nr_dev = 0;
2831 if (nr_dev >= SNDRV_CARDS)
2832 return 0;
2833 (void)(get_option(&str,&enable[nr_dev]) == 2 &&
2834 get_option(&str,&index[nr_dev]) == 2 &&
2835 get_id(&str,&id[nr_dev]) == 2 &&
2836 get_option(&str,&total_bufsize[nr_dev]) == 2 &&
2837 get_option(&str,&pcm_substreams_p[nr_dev]) == 2 &&
2838 get_option(&str,&pcm_substreams_c[nr_dev]) == 2 &&
2839 get_option(&str,&clock[nr_dev]) == 2);
2840 nr_dev++;
2841 return 1;
2844 __setup("snd-es1968=", alsa_card_es1968_setup);
2846 #endif /* ifndef MODULE */