source: GPL/trunk/alsa-kernel/pci/es1968.c@ 77

Last change on this file since 77 was 77, checked in by vladest, 19 years ago

Provide sources to latest exist binary. See changelog for changes

File size: 88.3 KB
Line 
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.
9 *
10 * TODO:
11 * Perhaps Synth
12 *
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.
17 *
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.
22 *
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
26 *
27 *
28 * Notes from Zach Brown about the driver code
29 *
30 * Hardware Description
31 *
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.
46 *
47 * Driver Operation
48 *
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.
53 *
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.
60 *
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.
67 *
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.
82 *
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.
95 */
96
97#define __SND_OSS_COMPAT__
98#include <sound/driver.h>
99#include <sound/pcm.h>
100#include <sound/mpu401.h>
101#include <sound/ac97_codec.h>
102#define SNDRV_GET_ID
103#include <sound/initval.h>
104
105#define CARD_NAME "ESS Maestro1/2"
106#define DRIVER_NAME "ES1968"
107
108EXPORT_NO_SYMBOLS;
109MODULE_DESCRIPTION("ESS Maestro");
110MODULE_CLASSES("{sound}");
111MODULE_LICENSE("GPL");
112MODULE_DEVICES("{{ESS,Maestro 2e},"
113 "{ESS,Maestro 2},"
114 "{ESS,Maestro 1},"
115 "{TerraTec,DMX}}");
116
117static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 1-MAX */
118static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
119static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
120static int total_bufsize[SNDRV_CARDS] = { REPEAT_SNDRV(1024) };
121static int pcm_substreams_p[SNDRV_CARDS] = { REPEAT_SNDRV(4) };
122static int pcm_substreams_c[SNDRV_CARDS] = { REPEAT_SNDRV(1) };
123static int clock[SNDRV_CARDS] = { REPEAT_SNDRV(0) };
124static int use_pm[SNDRV_CARDS] = { REPEAT_SNDRV(2) };
125static int enable_mpu[SNDRV_CARDS] = { REPEAT_SNDRV(1) };
126
127MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
128MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
129MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
130MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
131MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
132MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
133MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
134MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
135MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
136MODULE_PARM(total_bufsize, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
137MODULE_PARM_DESC(total_bufsize, "Total buffer size in kB.");
138MODULE_PARM_SYNTAX(total_bufsize, SNDRV_ENABLED ",allows:{{1,4096}},skill:advanced");
139MODULE_PARM(pcm_substreams_p, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
140MODULE_PARM_DESC(pcm_substreams_p, "PCM Playback substreams for " CARD_NAME " soundcard.");
141MODULE_PARM_SYNTAX(pcm_substreams_p, SNDRV_ENABLED ",allows:{{1,8}}");
142MODULE_PARM(pcm_substreams_c, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
143MODULE_PARM_DESC(pcm_substreams_c, "PCM Capture substreams for " CARD_NAME " soundcard.");
144MODULE_PARM_SYNTAX(pcm_substreams_c, SNDRV_ENABLED ",allows:{{0,8}}");
145MODULE_PARM(clock, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
146MODULE_PARM_DESC(clock, "Clock on " CARD_NAME " soundcard. (0 = auto-detect)");
147MODULE_PARM_SYNTAX(clock, SNDRV_ENABLED);
148MODULE_PARM(use_pm, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
149MODULE_PARM_DESC(use_pm, "Toggle power-management. (0 = off, 1 = on, 2 = auto)");
150MODULE_PARM_SYNTAX(use_pm, SNDRV_ENABLED ",allows:{{0,1,2}},skill:advanced");
151MODULE_PARM(enable_mpu, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
152MODULE_PARM_DESC(enable_mpu, "Enable MPU401. (0 = off, 1 = on, 2 = auto)");
153MODULE_PARM_SYNTAX(enable_mpu, SNDRV_ENABLED "," SNDRV_BOOLEAN_TRUE_DESC);
154
155
156/* PCI Dev ID's */
157
158#ifndef PCI_VENDOR_ID_ESS
159#define PCI_VENDOR_ID_ESS 0x125D
160#endif
161
162#define PCI_VENDOR_ID_ESS_OLD 0x1285 /* Platform Tech, the people the ESS
163was bought form */
164
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
174
175#define NR_APUS 64
176#define NR_APU_REGS 16
177
178/* NEC Versas ? */
179#define NEC_VERSA_SUBID1 0x80581033
180#define NEC_VERSA_SUBID2 0x803c1033
181
182/* Mode Flags */
183#define ESS_FMT_STEREO 0x01
184#define ESS_FMT_16BIT 0x02
185
186#define DAC_RUNNING 1
187#define ADC_RUNNING 2
188
189/* Values for the ESM_LEGACY_AUDIO_CONTROL */
190
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
199
200/* Values for the ESM_CONFIG_A */
201
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
213
214/* Values for the ESM_CONFIG_B */
215
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 */
224
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
231
232/* ****Port Adresses**** */
233
234/* Write & Read */
235#define ESM_INDEX 0x02
236#define ESM_DATA 0x00
237
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
245
246/* WaveCache*/
247#define WC_INDEX 0x10
248#define WC_DATA 0x12
249#define WC_CONTROL 0x14
250
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
265
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
274
275#define WRITEABLE_MAP 0xEFFFFF
276#define READABLE_MAP 0x64003F
277
278/* PCI Register */
279
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
285
286/* Bob Bits */
287#define ESM_BOB_ENABLE 0x0001
288#define ESM_BOB_START 0x0001
289
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
300
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
307
308#define ESS_SYSCLK 50000000
309#define ESM_BOB_FREQ 200
310#define ESM_BOB_FREQ_MAX 800
311
312#define ESM_FREQ_ESM1 (49152000L / 1024L) /* default rate 48000 */
313#define ESM_FREQ_ESM2 (50000000L / 1024L)
314
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 */
334
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
341
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)
351
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)
360
361/* reg 0x03 */
362#define ESM_APU_STEP_SIZE_MASK 0x0fff
363
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)
369
370/* reg 0x05 - wave start offset */
371/* reg 0x06 - wave end offset */
372/* reg 0x07 - wave loop length */
373
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)
381
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)
386
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
394
395#define ESM_APU_FILTER_TUNING_SHIFT 8
396#define ESM_APU_FILTER_TUNING_MASK (0xff << 8)
397
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)
405
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)
412
413/* reg 0x0c */
414#define ESM_APU_RADIUS_SELECT (1 << 6)
415
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
423
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
429
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
435
436
437/* capture mixing buffer size */
438#define ESM_MEM_ALIGN 0x1000
439#define ESM_MIXBUF_SIZE 0x400
440
441#define ESM_MODE_PLAY 0
442#define ESM_MODE_CAPTURE 1
443
444/* acpi states */
445enum {
446 ACPI_D0=0,
447 ACPI_D1,
448 ACPI_D2,
449 ACPI_D3
450};
451
452/* bits in the acpi masks */
453#define ACPI_12MHZ ( 1 << 15)
454#define ACPI_24MHZ ( 1 << 14)
455#define ACPI_978 ( 1 << 13)
456#define ACPI_SPDIF ( 1 << 12)
457#define ACPI_GLUE ( 1 << 11)
458#define ACPI__10 ( 1 << 10) /* reserved */
459#define ACPI_PCIINT ( 1 << 9)
460#define ACPI_HV ( 1 << 8) /* hardware volume */
461#define ACPI_GPIO ( 1 << 7)
462#define ACPI_ASSP ( 1 << 6)
463#define ACPI_SB ( 1 << 5) /* sb emul */
464#define ACPI_FM ( 1 << 4) /* fm emul */
465#define ACPI_RB ( 1 << 3) /* ringbus / aclink */
466#define ACPI_MIDI ( 1 << 2)
467#define ACPI_GP ( 1 << 1) /* game port */
468#define ACPI_WP ( 1 << 0) /* wave processor */
469
470#define ACPI_ALL (0xffff)
471#define ACPI_SLEEP (~(ACPI_SPDIF|ACPI_ASSP|ACPI_SB|ACPI_FM| \
472 ACPI_MIDI|ACPI_GP|ACPI_WP))
473#define ACPI_NONE (ACPI__10)
474
475/* these masks indicate which units we care about at
476 which states */
477static u16 acpi_state_mask[] = {
478#ifdef TARGET_OS2
479 ACPI_ALL,
480 ACPI_SLEEP,
481 ACPI_SLEEP,
482 ACPI_NONE
483#else
484 [ACPI_D0] = ACPI_ALL,
485 [ACPI_D1] = ACPI_SLEEP,
486 [ACPI_D2] = ACPI_SLEEP,
487 [ACPI_D3] = ACPI_NONE
488#endif
489};
490
491
492typedef struct snd_es1968 es1968_t;
493typedef struct snd_esschan esschan_t;
494typedef struct snd_esm_memory esm_memory_t;
495
496/* APU use in the driver */
497enum snd_enum_apu_type {
498 ESM_APU_PCM_PLAY,
499 ESM_APU_PCM_CAPTURE,
500 ESM_APU_PCM_RATECONV,
501 ESM_APU_FREE
502};
503
504/* chip type */
505enum {
506 TYPE_MAESTRO, TYPE_MAESTRO2, TYPE_MAESTRO2E
507};
508
509/* DMA Hack! */
510struct snd_esm_memory {
511 struct snd_dma_buffer buf;
512 int empty; /* status */
513 struct list_head list;
514};
515
516/* Playback Channel */
517struct snd_esschan {
518 int running;
519
520 u8 apu[4];
521 u8 apu_mode[4];
522
523 /* playback/capture pcm buffer */
524 esm_memory_t *memory;
525 /* capture mixer buffer */
526 esm_memory_t *mixbuf;
527
528 unsigned int hwptr; /* current hw pointer in bytes */
529 unsigned int count; /* sample counter in bytes */
530 unsigned int dma_size; /* total buffer size in bytes */
531 unsigned int frag_size; /* period size in bytes */
532 unsigned int wav_shift;
533 u16 base[4]; /* offset for ptr */
534
535 /* stereo/16bit flag */
536 unsigned char fmt;
537 int mode; /* playback / capture */
538
539 int bob_freq; /* required timer frequency */
540
541 snd_pcm_substream_t *substream;
542
543 /* linked list */
544 struct list_head list;
545
546#ifdef CONFIG_PM
547 u16 wc_map[4];
548#endif
549};
550
551struct snd_es1968 {
552 /* Module Config */
553 int total_bufsize; /* in bytes */
554
555 int playback_streams, capture_streams;
556
557 unsigned int clock; /* clock */
558 /* for clock measurement */
559 unsigned int in_measurement: 1;
560 unsigned int measure_apu;
561 unsigned int measure_lastpos;
562 unsigned int measure_count;
563
564 /* buffer */
565 struct snd_dma_buffer dma;
566
567 /* Resources... */
568 int irq;
569 unsigned long io_port;
570 int type;
571 struct pci_dev *pci;
572 snd_card_t *card;
573 snd_pcm_t *pcm;
574 int do_pm; /* power-management enabled */
575
576 /* DMA memory block */
577 struct list_head buf_list;
578
579 /* ALSA Stuff */
580 ac97_t *ac97;
581 snd_kcontrol_t *master_switch; /* for h/w volume control */
582 snd_kcontrol_t *master_volume;
583
584 snd_rawmidi_t *rmidi;
585
586 spinlock_t reg_lock;
587 spinlock_t ac97_lock;
588 struct tasklet_struct hwvol_tq;
589 unsigned int in_suspend;
590
591 /* Maestro Stuff */
592 u16 maestro_map[32];
593 int bobclient; /* active timer instancs */
594 int bob_freq; /* timer frequency */
595 struct semaphore memory_mutex; /* memory lock */
596
597 /* APU states */
598 unsigned char apu[NR_APUS];
599
600 /* active substreams */
601 struct list_head substream_list;
602 spinlock_t substream_lock;
603
604#ifdef CONFIG_PM
605 u16 apu_map[NR_APUS][NR_APU_REGS];
606#endif
607};
608
609static irqreturn_t snd_es1968_interrupt(int irq, void *dev_id, struct pt_regs *regs);
610
611static struct pci_device_id snd_es1968_ids[] = {
612 /* Maestro 1 */
613 { 0x1285, 0x0100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, TYPE_MAESTRO },
614 /* Maestro 2 */
615 { 0x125d, 0x1968, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, TYPE_MAESTRO2 },
616 /* Maestro 2E */
617 { 0x125d, 0x1978, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, TYPE_MAESTRO2E },
618 { 0, }
619};
620
621MODULE_DEVICE_TABLE(pci, snd_es1968_ids);
622
623/* *********************
624 * Low Level Funcs! *
625 *********************/
626
627/* no spinlock */
628static void __maestro_write(es1968_t *chip, u16 reg, u16 data)
629{
630 outw(reg, chip->io_port + ESM_INDEX);
631 outw(data, chip->io_port + ESM_DATA);
632 chip->maestro_map[reg] = data;
633}
634
635inline static void maestro_write(es1968_t *chip, u16 reg, u16 data)
636{
637 unsigned long flags;
638 spin_lock_irqsave(&chip->reg_lock, flags);
639 __maestro_write(chip, reg, data);
640 spin_unlock_irqrestore(&chip->reg_lock, flags);
641}
642
643/* no spinlock */
644static u16 __maestro_read(es1968_t *chip, u16 reg)
645{
646 if (READABLE_MAP & (1 << reg)) {
647 outw(reg, chip->io_port + ESM_INDEX);
648 chip->maestro_map[reg] = inw(chip->io_port + ESM_DATA);
649 }
650 return chip->maestro_map[reg];
651}
652
653inline static u16 maestro_read(es1968_t *chip, u16 reg)
654{
655 unsigned long flags;
656 u16 result;
657 spin_lock_irqsave(&chip->reg_lock, flags);
658 result = __maestro_read(chip, reg);
659 spin_unlock_irqrestore(&chip->reg_lock, flags);
660 return result;
661}
662
663/* Wait for the codec bus to be free */
664static int snd_es1968_ac97_wait(es1968_t *chip)
665{
666 int timeout = 100000;
667
668 while (timeout-- > 0) {
669 if (!(inb(chip->io_port + ESM_AC97_INDEX) & 1))
670 return 0;
671 cond_resched();
672 }
673 snd_printd("es1968: ac97 timeout\n");
674 return 1; /* timeout */
675}
676
677static void snd_es1968_ac97_write(ac97_t *ac97, unsigned short reg, unsigned short val)
678{
679 es1968_t *chip = ac97->private_data;
680 unsigned long flags;
681
682 snd_es1968_ac97_wait(chip);
683
684 /* Write the bus */
685 spin_lock_irqsave(&chip->ac97_lock, flags);
686 outw(val, chip->io_port + ESM_AC97_DATA);
687 /*msleep(1);*/
688 outb(reg, chip->io_port + ESM_AC97_INDEX);
689 /*msleep(1);*/
690 spin_unlock_irqrestore(&chip->ac97_lock, flags);
691}
692
693static unsigned short snd_es1968_ac97_read(ac97_t *ac97, unsigned short reg)
694{
695 u16 data = 0;
696 es1968_t *chip = ac97->private_data;
697 unsigned long flags;
698
699 snd_es1968_ac97_wait(chip);
700
701 spin_lock_irqsave(&chip->ac97_lock, flags);
702 outb(reg | 0x80, chip->io_port + ESM_AC97_INDEX);
703 /*msleep(1);*/
704
705 if (! snd_es1968_ac97_wait(chip)) {
706 data = inw(chip->io_port + ESM_AC97_DATA);
707 /*msleep(1);*/
708 }
709 spin_unlock_irqrestore(&chip->ac97_lock, flags);
710
711 return data;
712}
713
714/* no spinlock */
715static void apu_index_set(es1968_t *chip, u16 index)
716{
717 int i;
718 __maestro_write(chip, IDR1_CRAM_POINTER, index);
719 for (i = 0; i < 1000; i++)
720 if (__maestro_read(chip, IDR1_CRAM_POINTER) == index)
721 return;
722 snd_printd("es1968: APU register select failed. (Timeout)\n");
723}
724
725/* no spinlock */
726static void apu_data_set(es1968_t *chip, u16 data)
727{
728 int i;
729 for (i = 0; i < 1000; i++) {
730 if (__maestro_read(chip, IDR0_DATA_PORT) == data)
731 return;
732 __maestro_write(chip, IDR0_DATA_PORT, data);
733 }
734 snd_printd("es1968: APU register set probably failed (Timeout)!\n");
735}
736
737/* no spinlock */
738static void __apu_set_register(es1968_t *chip, u16 channel, u8 reg, u16 data)
739{
740 snd_assert(channel < NR_APUS, return);
741#ifdef CONFIG_PM
742 chip->apu_map[channel][reg] = data;
743#endif
744 reg |= (channel << 4);
745 apu_index_set(chip, reg);
746 apu_data_set(chip, data);
747}
748
749inline static void apu_set_register(es1968_t *chip, u16 channel, u8 reg, u16 data)
750{
751 unsigned long flags;
752 spin_lock_irqsave(&chip->reg_lock, flags);
753 __apu_set_register(chip, channel, reg, data);
754 spin_unlock_irqrestore(&chip->reg_lock, flags);
755}
756
757static u16 __apu_get_register(es1968_t *chip, u16 channel, u8 reg)
758{
759 snd_assert(channel < NR_APUS, return 0);
760 reg |= (channel << 4);
761 apu_index_set(chip, reg);
762 return __maestro_read(chip, IDR0_DATA_PORT);
763}
764
765inline static u16 apu_get_register(es1968_t *chip, u16 channel, u8 reg)
766{
767 unsigned long flags;
768 u16 v;
769 spin_lock_irqsave(&chip->reg_lock, flags);
770 v = __apu_get_register(chip, channel, reg);
771 spin_unlock_irqrestore(&chip->reg_lock, flags);
772 return v;
773}
774
775#if 0 /* ASSP is not supported */
776
777static void assp_set_register(es1968_t *chip, u32 reg, u32 value)
778{
779 unsigned long flags;
780
781 spin_lock_irqsave(&chip->reg_lock, flags);
782 outl(reg, chip->io_port + ASSP_INDEX);
783 outl(value, chip->io_port + ASSP_DATA);
784 spin_unlock_irqrestore(&chip->reg_lock, flags);
785}
786
787static u32 assp_get_register(es1968_t *chip, u32 reg)
788{
789 unsigned long flags;
790 u32 value;
791
792 spin_lock_irqsave(&chip->reg_lock, flags);
793 outl(reg, chip->io_port + ASSP_INDEX);
794 value = inl(chip->io_port + ASSP_DATA);
795 spin_unlock_irqrestore(&chip->reg_lock, flags);
796
797 return value;
798}
799
800#endif
801
802static void wave_set_register(es1968_t *chip, u16 reg, u16 value)
803{
804 unsigned long flags;
805
806 spin_lock_irqsave(&chip->reg_lock, flags);
807 outw(reg, chip->io_port + WC_INDEX);
808 outw(value, chip->io_port + WC_DATA);
809 spin_unlock_irqrestore(&chip->reg_lock, flags);
810}
811
812static u16 wave_get_register(es1968_t *chip, u16 reg)
813{
814 unsigned long flags;
815 u16 value;
816
817 spin_lock_irqsave(&chip->reg_lock, flags);
818 outw(reg, chip->io_port + WC_INDEX);
819 value = inw(chip->io_port + WC_DATA);
820 spin_unlock_irqrestore(&chip->reg_lock, flags);
821
822 return value;
823}
824
825/* *******************
826 * Bob the Timer! *
827 *******************/
828
829static void snd_es1968_bob_stop(es1968_t *chip)
830{
831 u16 reg;
832
833 reg = __maestro_read(chip, 0x11);
834 reg &= ~ESM_BOB_ENABLE;
835 __maestro_write(chip, 0x11, reg);
836 reg = __maestro_read(chip, 0x17);
837 reg &= ~ESM_BOB_START;
838 __maestro_write(chip, 0x17, reg);
839}
840
841static void snd_es1968_bob_start(es1968_t *chip)
842{
843 int prescale;
844 int divide;
845
846 /* compute ideal interrupt frequency for buffer size & play rate */
847 /* first, find best prescaler value to match freq */
848 for (prescale = 5; prescale < 12; prescale++)
849 if (chip->bob_freq > (ESS_SYSCLK >> (prescale + 9)))
850 break;
851
852 /* next, back off prescaler whilst getting divider into optimum range */
853 divide = 1;
854 while ((prescale > 5) && (divide < 32)) {
855 prescale--;
856 divide <<= 1;
857 }
858 divide >>= 1;
859
860 /* now fine-tune the divider for best match */
861 for (; divide < 31; divide++)
862 if (chip->bob_freq >
863 ((ESS_SYSCLK >> (prescale + 9)) / (divide + 1))) break;
864
865 /* divide = 0 is illegal, but don't let prescale = 4! */
866 if (divide == 0) {
867 divide++;
868 if (prescale > 5)
869 prescale--;
870 } else if (divide > 1)
871 divide--;
872
873 __maestro_write(chip, 6, 0x9000 | (prescale << 5) | divide); /* set reg */
874
875 /* Now set IDR 11/17 */
876 __maestro_write(chip, 0x11, __maestro_read(chip, 0x11) | 1);
877 __maestro_write(chip, 0x17, __maestro_read(chip, 0x17) | 1);
878}
879
880/* call with substream spinlock */
881static void snd_es1968_bob_inc(es1968_t *chip, int freq)
882{
883 chip->bobclient++;
884 if (chip->bobclient == 1) {
885 chip->bob_freq = freq;
886 snd_es1968_bob_start(chip);
887 } else if (chip->bob_freq < freq) {
888 snd_es1968_bob_stop(chip);
889 chip->bob_freq = freq;
890 snd_es1968_bob_start(chip);
891 }
892}
893
894/* call with substream spinlock */
895static void snd_es1968_bob_dec(es1968_t *chip)
896{
897 chip->bobclient--;
898 if (chip->bobclient <= 0)
899 snd_es1968_bob_stop(chip);
900 else if (chip->bob_freq > ESM_BOB_FREQ) {
901 /* check reduction of timer frequency */
902 struct list_head *p;
903 int max_freq = ESM_BOB_FREQ;
904 list_for_each(p, &chip->substream_list) {
905 esschan_t *es = list_entry(p, esschan_t, list);
906 if (max_freq < es->bob_freq)
907 max_freq = es->bob_freq;
908 }
909 if (max_freq != chip->bob_freq) {
910 snd_es1968_bob_stop(chip);
911 chip->bob_freq = max_freq;
912 snd_es1968_bob_start(chip);
913 }
914 }
915}
916
917static int
918snd_es1968_calc_bob_rate(es1968_t *chip, esschan_t *es,
919 snd_pcm_runtime_t *runtime)
920{
921 /* we acquire 4 interrupts per period for precise control.. */
922 int freq = runtime->rate * 4;
923 if (es->fmt & ESS_FMT_STEREO)
924 freq <<= 1;
925 if (es->fmt & ESS_FMT_16BIT)
926 freq <<= 1;
927 freq /= es->frag_size;
928 if (freq < ESM_BOB_FREQ)
929 freq = ESM_BOB_FREQ;
930 else if (freq > ESM_BOB_FREQ_MAX)
931 freq = ESM_BOB_FREQ_MAX;
932 return freq;
933}
934
935
936/*************
937 * PCM Part *
938 *************/
939
940static u32 snd_es1968_compute_rate(es1968_t *chip, u32 freq)
941{
942 u32 rate = (freq << 16) / chip->clock;
943#if 0 /* XXX: do we need this? */
944 if (rate > 0x10000)
945 rate = 0x10000;
946#endif
947 return rate;
948}
949
950/* get current pointer */
951inline static unsigned int
952snd_es1968_get_dma_ptr(es1968_t *chip, esschan_t *es)
953{
954 unsigned int offset;
955
956 offset = apu_get_register(chip, es->apu[0], 5);
957
958 offset -= es->base[0];
959
960 return (offset & 0xFFFE); /* hardware is in words */
961}
962
963static void snd_es1968_apu_set_freq(es1968_t *chip, int apu, int freq)
964{
965 apu_set_register(chip, apu, 2,
966 (apu_get_register(chip, apu, 2) & 0x00FF) |
967 ((freq & 0xff) << 8) | 0x10);
968 apu_set_register(chip, apu, 3, freq >> 8);
969}
970
971/* spin lock held */
972inline static void snd_es1968_trigger_apu(es1968_t *esm, int apu, int mode)
973{
974 /* set the APU mode */
975 __apu_set_register(esm, apu, 0,
976 (__apu_get_register(esm, apu, 0) & 0xff0f) |
977 (mode << 4));
978}
979
980static void snd_es1968_pcm_start(es1968_t *chip, esschan_t *es)
981{
982 spin_lock(&chip->reg_lock);
983 __apu_set_register(chip, es->apu[0], 5, es->base[0]);
984 snd_es1968_trigger_apu(chip, es->apu[0], es->apu_mode[0]);
985 if (es->mode == ESM_MODE_CAPTURE) {
986 __apu_set_register(chip, es->apu[2], 5, es->base[2]);
987 snd_es1968_trigger_apu(chip, es->apu[2], es->apu_mode[2]);
988 }
989 if (es->fmt & ESS_FMT_STEREO) {
990 __apu_set_register(chip, es->apu[1], 5, es->base[1]);
991 snd_es1968_trigger_apu(chip, es->apu[1], es->apu_mode[1]);
992 if (es->mode == ESM_MODE_CAPTURE) {
993 __apu_set_register(chip, es->apu[3], 5, es->base[3]);
994 snd_es1968_trigger_apu(chip, es->apu[3], es->apu_mode[3]);
995 }
996 }
997 spin_unlock(&chip->reg_lock);
998}
999
1000static void snd_es1968_pcm_stop(es1968_t *chip, esschan_t *es)
1001{
1002 spin_lock(&chip->reg_lock);
1003 snd_es1968_trigger_apu(chip, es->apu[0], 0);
1004 snd_es1968_trigger_apu(chip, es->apu[1], 0);
1005 if (es->mode == ESM_MODE_CAPTURE) {
1006 snd_es1968_trigger_apu(chip, es->apu[2], 0);
1007 snd_es1968_trigger_apu(chip, es->apu[3], 0);
1008 }
1009 spin_unlock(&chip->reg_lock);
1010}
1011
1012/* set the wavecache control reg */
1013static void snd_es1968_program_wavecache(es1968_t *chip, esschan_t *es,
1014 int channel, u32 addr, int capture)
1015{
1016 u32 tmpval = (addr - 0x10) & 0xFFF8;
1017
1018 if (! capture) {
1019 if (!(es->fmt & ESS_FMT_16BIT))
1020 tmpval |= 4; /* 8bit */
1021 if (es->fmt & ESS_FMT_STEREO)
1022 tmpval |= 2; /* stereo */
1023 }
1024
1025 /* set the wavecache control reg */
1026 wave_set_register(chip, es->apu[channel] << 3, tmpval);
1027
1028#ifdef CONFIG_PM
1029 es->wc_map[channel] = tmpval;
1030#endif
1031}
1032
1033
1034static void snd_es1968_playback_setup(es1968_t *chip, esschan_t *es,
1035 snd_pcm_runtime_t *runtime)
1036{
1037 u32 pa;
1038 int high_apu = 0;
1039 int channel, apu;
1040 int i, size;
1041 unsigned long flags;
1042 u32 freq;
1043
1044 size = es->dma_size >> es->wav_shift;
1045
1046 if (es->fmt & ESS_FMT_STEREO)
1047 high_apu++;
1048
1049 for (channel = 0; channel <= high_apu; channel++) {
1050 apu = es->apu[channel];
1051
1052 snd_es1968_program_wavecache(chip, es, channel, es->memory->buf.addr, 0);
1053
1054 /* Offset to PCMBAR */
1055 pa = es->memory->buf.addr;
1056 pa -= chip->dma.addr;
1057 pa >>= 1; /* words */
1058
1059 pa |= 0x00400000; /* System RAM (Bit 22) */
1060
1061 if (es->fmt & ESS_FMT_STEREO) {
1062 /* Enable stereo */
1063 if (channel)
1064 pa |= 0x00800000; /* (Bit 23) */
1065 if (es->fmt & ESS_FMT_16BIT)
1066 pa >>= 1;
1067 }
1068
1069 /* base offset of dma calcs when reading the pointer
1070 on this left one */
1071 es->base[channel] = pa & 0xFFFF;
1072
1073 for (i = 0; i < 16; i++)
1074 apu_set_register(chip, apu, i, 0x0000);
1075
1076 /* Load the buffer into the wave engine */
1077 apu_set_register(chip, apu, 4, ((pa >> 16) & 0xFF) << 8);
1078 apu_set_register(chip, apu, 5, pa & 0xFFFF);
1079 apu_set_register(chip, apu, 6, (pa + size) & 0xFFFF);
1080 /* setting loop == sample len */
1081 apu_set_register(chip, apu, 7, size);
1082
1083 /* clear effects/env.. */
1084 apu_set_register(chip, apu, 8, 0x0000);
1085 /* set amp now to 0xd0 (?), low byte is 'amplitude dest'? */
1086 apu_set_register(chip, apu, 9, 0xD000);
1087
1088 /* clear routing stuff */
1089 apu_set_register(chip, apu, 11, 0x0000);
1090 /* dma on, no envelopes, filter to all 1s) */
1091 apu_set_register(chip, apu, 0, 0x400F);
1092
1093 if (es->fmt & ESS_FMT_16BIT)
1094 es->apu_mode[channel] = ESM_APU_16BITLINEAR;
1095 else
1096 es->apu_mode[channel] = ESM_APU_8BITLINEAR;
1097
1098 if (es->fmt & ESS_FMT_STEREO) {
1099 /* set panning: left or right */
1100 /* Check: different panning. On my Canyon 3D Chipset the
1101 Channels are swapped. I don't know, about the output
1102 to the SPDif Link. Perhaps you have to change this
1103 and not the APU Regs 4-5. */
1104 apu_set_register(chip, apu, 10,
1105 0x8F00 | (channel ? 0 : 0x10));
1106 es->apu_mode[channel] += 1; /* stereo */
1107 } else
1108 apu_set_register(chip, apu, 10, 0x8F08);
1109 }
1110
1111 spin_lock_irqsave(&chip->reg_lock, flags);
1112 /* clear WP interrupts */
1113 outw(1, chip->io_port + 0x04);
1114 /* enable WP ints */
1115 outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ);
1116 spin_unlock_irqrestore(&chip->reg_lock, flags);
1117
1118 freq = runtime->rate;
1119 /* set frequency */
1120 if (freq > 48000)
1121 freq = 48000;
1122 if (freq < 4000)
1123 freq = 4000;
1124
1125 /* hmmm.. */
1126 if (!(es->fmt & ESS_FMT_16BIT) && !(es->fmt & ESS_FMT_STEREO))
1127 freq >>= 1;
1128
1129 freq = snd_es1968_compute_rate(chip, freq);
1130
1131 /* Load the frequency, turn on 6dB */
1132 snd_es1968_apu_set_freq(chip, es->apu[0], freq);
1133 snd_es1968_apu_set_freq(chip, es->apu[1], freq);
1134}
1135
1136
1137static void init_capture_apu(es1968_t *chip, esschan_t *es, int channel,
1138 unsigned int pa, unsigned int bsize,
1139 int mode, int route)
1140{
1141 int i, apu = es->apu[channel];
1142
1143 es->apu_mode[channel] = mode;
1144
1145 /* set the wavecache control reg */
1146 snd_es1968_program_wavecache(chip, es, channel, pa, 1);
1147
1148 /* Offset to PCMBAR */
1149 pa -= chip->dma.addr;
1150 pa >>= 1; /* words */
1151
1152 /* base offset of dma calcs when reading the pointer
1153 on this left one */
1154 es->base[channel] = pa & 0xFFFF;
1155 pa |= 0x00400000; /* bit 22 -> System RAM */
1156
1157 /* Begin loading the APU */
1158 for (i = 0; i < 16; i++)
1159 apu_set_register(chip, apu, i, 0x0000);
1160
1161 /* need to enable subgroups.. and we should probably
1162 have different groups for different /dev/dsps.. */
1163 apu_set_register(chip, apu, 2, 0x8);
1164
1165 /* Load the buffer into the wave engine */
1166 apu_set_register(chip, apu, 4, ((pa >> 16) & 0xFF) << 8);
1167 apu_set_register(chip, apu, 5, pa & 0xFFFF);
1168 apu_set_register(chip, apu, 6, (pa + bsize) & 0xFFFF);
1169 apu_set_register(chip, apu, 7, bsize);
1170 /* clear effects/env.. */
1171 apu_set_register(chip, apu, 8, 0x00F0);
1172 /* amplitude now? sure. why not. */
1173 apu_set_register(chip, apu, 9, 0x0000);
1174 /* set filter tune, radius, polar pan */
1175 apu_set_register(chip, apu, 10, 0x8F08);
1176 /* route input */
1177 apu_set_register(chip, apu, 11, route);
1178 /* dma on, no envelopes, filter to all 1s) */
1179 apu_set_register(chip, apu, 0, 0x400F);
1180}
1181
1182
1183static void snd_es1968_capture_setup(es1968_t *chip, esschan_t *es,
1184 snd_pcm_runtime_t *runtime)
1185{
1186 int size;
1187 u32 freq;
1188 unsigned long flags;
1189
1190 size = es->dma_size >> es->wav_shift;
1191
1192 /* APU assignments:
1193 0 = mono/left SRC
1194 1 = right SRC
1195 2 = mono/left Input Mixer
1196 3 = right Input Mixer
1197 */
1198 /* data seems to flow from the codec, through an apu into
1199 the 'mixbuf' bit of page, then through the SRC apu
1200 and out to the real 'buffer'. ok. sure. */
1201
1202 /* input mixer (left/mono) */
1203 /* parallel in crap, see maestro reg 0xC [8-11] */
1204 init_capture_apu(chip, es, 2,
1205 es->mixbuf->buf.addr, ESM_MIXBUF_SIZE/4, /* in words */
1206 ESM_APU_INPUTMIXER, 0x14);
1207 /* SRC (left/mono); get input from inputing apu */
1208 init_capture_apu(chip, es, 0, es->memory->buf.addr, size,
1209 ESM_APU_SRCONVERTOR, es->apu[2]);
1210 if (es->fmt & ESS_FMT_STEREO) {
1211 /* input mixer (right) */
1212 init_capture_apu(chip, es, 3,
1213 es->mixbuf->buf.addr + ESM_MIXBUF_SIZE/2,
1214 ESM_MIXBUF_SIZE/4, /* in words */
1215 ESM_APU_INPUTMIXER, 0x15);
1216 /* SRC (right) */
1217 init_capture_apu(chip, es, 1,
1218 es->memory->buf.addr + size*2, size,
1219 ESM_APU_SRCONVERTOR, es->apu[3]);
1220 }
1221 freq = runtime->rate;
1222 /* Sample Rate conversion APUs don't like 0x10000 for their rate */
1223 if (freq > 47999)
1224 freq = 47999;
1225 if (freq < 4000)
1226 freq = 4000;
1227
1228 freq = snd_es1968_compute_rate(chip, freq);
1229
1230 /* Load the frequency, turn on 6dB */
1231 snd_es1968_apu_set_freq(chip, es->apu[0], freq);
1232 snd_es1968_apu_set_freq(chip, es->apu[1], freq);
1233
1234 /* fix mixer rate at 48khz. and its _must_ be 0x10000. */
1235 freq = 0x10000;
1236 snd_es1968_apu_set_freq(chip, es->apu[2], freq);
1237 snd_es1968_apu_set_freq(chip, es->apu[3], freq);
1238
1239 spin_lock_irqsave(&chip->reg_lock, flags);
1240 /* clear WP interrupts */
1241 outw(1, chip->io_port + 0x04);
1242 /* enable WP ints */
1243 outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ);
1244 spin_unlock_irqrestore(&chip->reg_lock, flags);
1245}
1246
1247/*******************
1248 * ALSA Interface *
1249 *******************/
1250
1251static int snd_es1968_pcm_prepare(snd_pcm_substream_t *substream)
1252{
1253 es1968_t *chip = snd_pcm_substream_chip(substream);
1254 snd_pcm_runtime_t *runtime = substream->runtime;
1255 esschan_t *es = runtime->private_data;
1256
1257 es->dma_size = snd_pcm_lib_buffer_bytes(substream);
1258 es->frag_size = snd_pcm_lib_period_bytes(substream);
1259
1260 es->wav_shift = 1; /* maestro handles always 16bit */
1261 es->fmt = 0;
1262 if (snd_pcm_format_width(runtime->format) == 16)
1263 es->fmt |= ESS_FMT_16BIT;
1264 if (runtime->channels > 1) {
1265 es->fmt |= ESS_FMT_STEREO;
1266 if (es->fmt & ESS_FMT_16BIT) /* 8bit is already word shifted */
1267 es->wav_shift++;
1268 }
1269 es->bob_freq = snd_es1968_calc_bob_rate(chip, es, runtime);
1270
1271 switch (es->mode) {
1272 case ESM_MODE_PLAY:
1273 snd_es1968_playback_setup(chip, es, runtime);
1274 break;
1275 case ESM_MODE_CAPTURE:
1276 snd_es1968_capture_setup(chip, es, runtime);
1277 break;
1278 }
1279
1280 return 0;
1281}
1282
1283static int snd_es1968_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
1284{
1285 es1968_t *chip = snd_pcm_substream_chip(substream);
1286 esschan_t *es = substream->runtime->private_data;
1287
1288 spin_lock(&chip->substream_lock);
1289 switch (cmd) {
1290 case SNDRV_PCM_TRIGGER_START:
1291 case SNDRV_PCM_TRIGGER_RESUME:
1292 if (es->running)
1293 break;
1294 snd_es1968_bob_inc(chip, es->bob_freq);
1295 es->count = 0;
1296 es->hwptr = 0;
1297 snd_es1968_pcm_start(chip, es);
1298 es->running = 1;
1299 break;
1300 case SNDRV_PCM_TRIGGER_STOP:
1301 case SNDRV_PCM_TRIGGER_SUSPEND:
1302 if (! es->running)
1303 break;
1304 snd_es1968_pcm_stop(chip, es);
1305 es->running = 0;
1306 snd_es1968_bob_dec(chip);
1307 break;
1308 }
1309 spin_unlock(&chip->substream_lock);
1310 return 0;
1311}
1312
1313static snd_pcm_uframes_t snd_es1968_pcm_pointer(snd_pcm_substream_t *substream)
1314{
1315 es1968_t *chip = snd_pcm_substream_chip(substream);
1316 esschan_t *es = substream->runtime->private_data;
1317 unsigned int ptr;
1318
1319 ptr = snd_es1968_get_dma_ptr(chip, es) << es->wav_shift;
1320
1321 return bytes_to_frames(substream->runtime, ptr % es->dma_size);
1322}
1323
1324static snd_pcm_hardware_t snd_es1968_playback = {
1325 /* info: */ (SNDRV_PCM_INFO_MMAP |
1326 SNDRV_PCM_INFO_MMAP_VALID |
1327 SNDRV_PCM_INFO_INTERLEAVED |
1328 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1329 /*SNDRV_PCM_INFO_PAUSE |*/
1330 SNDRV_PCM_INFO_RESUME),
1331 /* formats: */ SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1332 /* rates: */ SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1333 /* rate_min: */ 4000,
1334 /* rate_max: */ 48000,
1335 /* channels_min: */ 1,
1336 /* channels_max: */ 2,
1337 /* buffer_bytes_max: */ 65536,
1338 /* period_bytes_min: */ 256,
1339 /* period_bytes_max: */ 65536,
1340 /* periods_min: */ 1,
1341 /* periods_max: */ 1024,
1342 /* fifo_size: */ 0,
1343};
1344
1345static snd_pcm_hardware_t snd_es1968_capture = {
1346 /* info: */ (SNDRV_PCM_INFO_NONINTERLEAVED |
1347 SNDRV_PCM_INFO_MMAP |
1348 SNDRV_PCM_INFO_MMAP_VALID |
1349 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1350 /*SNDRV_PCM_INFO_PAUSE |*/
1351 SNDRV_PCM_INFO_RESUME),
1352 /* formats: */ /*SNDRV_PCM_FMTBIT_U8 |*/ SNDRV_PCM_FMTBIT_S16_LE,
1353 /* rates: */ SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1354 /* rate_min: */ 4000,
1355 /* rate_max: */ 48000,
1356 /* channels_min: */ 1,
1357 /* channels_max: */ 2,
1358 /* buffer_bytes_max: */ 65536,
1359 /* period_bytes_min: */ 256,
1360 /* period_bytes_max: */ 65536,
1361 /* periods_min: */ 1,
1362 /* periods_max: */ 1024,
1363 /* fifo_size: */ 0,
1364};
1365
1366/* *************************
1367 * DMA memory management *
1368 *************************/
1369
1370/* Because the Maestro can only take addresses relative to the PCM base address
1371 register :( */
1372
1373static int calc_available_memory_size(es1968_t *chip)
1374{
1375 struct list_head *p;
1376 int max_size = 0;
1377
1378 down(&chip->memory_mutex);
1379 list_for_each(p, &chip->buf_list) {
1380 esm_memory_t *buf = list_entry(p, esm_memory_t, list);
1381 if (buf->empty && buf->buf.bytes > max_size)
1382 max_size = buf->buf.bytes;
1383 }
1384 up(&chip->memory_mutex);
1385 if (max_size >= 128*1024)
1386 max_size = 127*1024;
1387 return max_size;
1388}
1389
1390/* allocate a new memory chunk with the specified size */
1391static esm_memory_t *snd_es1968_new_memory(es1968_t *chip, int size)
1392{
1393 esm_memory_t *buf;
1394 struct list_head *p;
1395
1396 size = ((size + ESM_MEM_ALIGN - 1) / ESM_MEM_ALIGN) * ESM_MEM_ALIGN;
1397 down(&chip->memory_mutex);
1398 list_for_each(p, &chip->buf_list) {
1399 buf = list_entry(p, esm_memory_t, list);
1400 if (buf->empty && buf->buf.bytes >= size)
1401 goto __found;
1402 }
1403 up(&chip->memory_mutex);
1404 return NULL;
1405
1406__found:
1407 if (buf->buf.bytes > size) {
1408 esm_memory_t *chunk = kmalloc(sizeof(*chunk), GFP_KERNEL);
1409 if (chunk == NULL) {
1410 up(&chip->memory_mutex);
1411 return NULL;
1412 }
1413 chunk->buf = buf->buf;
1414 chunk->buf.bytes -= size;
1415 chunk->buf.area += size;
1416 chunk->buf.addr += size;
1417 chunk->empty = 1;
1418 buf->buf.bytes = size;
1419 list_add(&chunk->list, &buf->list);
1420 }
1421 buf->empty = 0;
1422 up(&chip->memory_mutex);
1423 return buf;
1424}
1425
1426/* free a memory chunk */
1427static void snd_es1968_free_memory(es1968_t *chip, esm_memory_t *buf)
1428{
1429 esm_memory_t *chunk;
1430
1431 down(&chip->memory_mutex);
1432 buf->empty = 1;
1433 if (buf->list.prev != &chip->buf_list) {
1434 chunk = list_entry(buf->list.prev, esm_memory_t, list);
1435 if (chunk->empty) {
1436 chunk->buf.bytes += buf->buf.bytes;
1437 list_del(&buf->list);
1438 kfree(buf);
1439 buf = chunk;
1440 }
1441 }
1442 if (buf->list.next != &chip->buf_list) {
1443 chunk = list_entry(buf->list.next, esm_memory_t, list);
1444 if (chunk->empty) {
1445 buf->buf.bytes += chunk->buf.bytes;
1446 list_del(&chunk->list);
1447 kfree(chunk);
1448 }
1449 }
1450 up(&chip->memory_mutex);
1451}
1452
1453static void snd_es1968_free_dmabuf(es1968_t *chip)
1454{
1455 struct list_head *p;
1456
1457 if (! chip->dma.area)
1458 return;
1459 snd_dma_reserve_buf(&chip->dma, snd_dma_pci_buf_id(chip->pci));
1460 while ((p = chip->buf_list.next) != &chip->buf_list) {
1461 esm_memory_t *chunk = list_entry(p, esm_memory_t, list);
1462 list_del(p);
1463 kfree(chunk);
1464 }
1465}
1466
1467static int __devinit
1468snd_es1968_init_dmabuf(es1968_t *chip)
1469{
1470 int err;
1471 esm_memory_t *chunk;
1472
1473 chip->dma.dev.type = SNDRV_DMA_TYPE_DEV;
1474 chip->dma.dev.dev = snd_dma_pci_data(chip->pci);
1475 if (! snd_dma_get_reserved_buf(&chip->dma, snd_dma_pci_buf_id(chip->pci))) {
1476 err = snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV,
1477 snd_dma_pci_data(chip->pci),
1478 chip->total_bufsize, &chip->dma);
1479 if (err < 0 || ! chip->dma.area) {
1480 snd_printk("es1968: can't allocate dma pages for size %d\n",
1481 chip->total_bufsize);
1482 return -ENOMEM;
1483 }
1484 if ((chip->dma.addr + chip->dma.bytes - 1) & ~((1 << 28) - 1)) {
1485 snd_dma_free_pages(&chip->dma);
1486 snd_printk("es1968: DMA buffer beyond 256MB.\n");
1487 return -ENOMEM;
1488 }
1489 }
1490
1491 INIT_LIST_HEAD(&chip->buf_list);
1492 /* allocate an empty chunk */
1493 chunk = kmalloc(sizeof(*chunk), GFP_KERNEL);
1494 if (chunk == NULL) {
1495 snd_es1968_free_dmabuf(chip);
1496 return -ENOMEM;
1497 }
1498 memset(chip->dma.area, 0, ESM_MEM_ALIGN);
1499 chunk->buf = chip->dma;
1500 chunk->buf.area += ESM_MEM_ALIGN;
1501 chunk->buf.addr += ESM_MEM_ALIGN;
1502 chunk->buf.bytes -= ESM_MEM_ALIGN;
1503 chunk->empty = 1;
1504 list_add(&chunk->list, &chip->buf_list);
1505
1506 return 0;
1507}
1508
1509/* setup the dma_areas */
1510/* buffer is extracted from the pre-allocated memory chunk */
1511static int snd_es1968_hw_params(snd_pcm_substream_t *substream,
1512 snd_pcm_hw_params_t *hw_params)
1513{
1514 es1968_t *chip = snd_pcm_substream_chip(substream);
1515 snd_pcm_runtime_t *runtime = substream->runtime;
1516 esschan_t *chan = runtime->private_data;
1517 int size = params_buffer_bytes(hw_params);
1518
1519 if (chan->memory) {
1520 if (chan->memory->buf.bytes >= size) {
1521 runtime->dma_bytes = size;
1522 return 0;
1523 }
1524 snd_es1968_free_memory(chip, chan->memory);
1525 }
1526 chan->memory = snd_es1968_new_memory(chip, size);
1527 if (chan->memory == NULL) {
1528 // snd_printd("cannot allocate dma buffer: size = %d\n", size);
1529 return -ENOMEM;
1530 }
1531 snd_pcm_set_runtime_buffer(substream, &chan->memory->buf);
1532 return 1; /* area was changed */
1533}
1534
1535/* remove dma areas if allocated */
1536static int snd_es1968_hw_free(snd_pcm_substream_t * substream)
1537{
1538 es1968_t *chip = snd_pcm_substream_chip(substream);
1539 snd_pcm_runtime_t *runtime = substream->runtime;
1540 esschan_t *chan;
1541
1542 if (runtime->private_data == NULL)
1543 return 0;
1544 chan = runtime->private_data;
1545 if (chan->memory) {
1546 snd_es1968_free_memory(chip, chan->memory);
1547 chan->memory = NULL;
1548 }
1549 return 0;
1550}
1551
1552
1553/*
1554 * allocate APU pair
1555 */
1556static int snd_es1968_alloc_apu_pair(es1968_t *chip, int type)
1557{
1558 int apu;
1559
1560 for (apu = 0; apu < NR_APUS; apu += 2) {
1561 if (chip->apu[apu] == ESM_APU_FREE &&
1562 chip->apu[apu + 1] == ESM_APU_FREE) {
1563 chip->apu[apu] = chip->apu[apu + 1] = type;
1564 return apu;
1565 }
1566 }
1567 return -EBUSY;
1568}
1569
1570/*
1571 * release APU pair
1572 */
1573static void snd_es1968_free_apu_pair(es1968_t *chip, int apu)
1574{
1575 chip->apu[apu] = chip->apu[apu + 1] = ESM_APU_FREE;
1576}
1577
1578
1579/******************
1580 * PCM open/close *
1581 ******************/
1582
1583static int snd_es1968_playback_open(snd_pcm_substream_t *substream)
1584{
1585 es1968_t *chip = snd_pcm_substream_chip(substream);
1586 snd_pcm_runtime_t *runtime = substream->runtime;
1587 esschan_t *es;
1588 int apu1;
1589
1590 /* search 2 APUs */
1591 apu1 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_PLAY);
1592 if (apu1 < 0)
1593 return apu1;
1594
1595 es = kcalloc(1, sizeof(*es), GFP_KERNEL);
1596 if (!es) {
1597 snd_es1968_free_apu_pair(chip, apu1);
1598 return -ENOMEM;
1599 }
1600
1601 es->apu[0] = apu1;
1602 es->apu[1] = apu1 + 1;
1603 es->apu_mode[0] = 0;
1604 es->apu_mode[1] = 0;
1605 es->running = 0;
1606 es->substream = substream;
1607 es->mode = ESM_MODE_PLAY;
1608
1609 runtime->private_data = es;
1610 runtime->hw = snd_es1968_playback;
1611 runtime->hw.buffer_bytes_max = runtime->hw.period_bytes_max =
1612 calc_available_memory_size(chip);
1613#if 0
1614 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1615 1024);
1616#endif
1617 spin_lock_irq(&chip->substream_lock);
1618 list_add(&es->list, &chip->substream_list);
1619 spin_unlock_irq(&chip->substream_lock);
1620
1621 return 0;
1622}
1623
1624static int snd_es1968_capture_open(snd_pcm_substream_t *substream)
1625{
1626 snd_pcm_runtime_t *runtime = substream->runtime;
1627 es1968_t *chip = snd_pcm_substream_chip(substream);
1628 esschan_t *es;
1629 int apu1, apu2;
1630
1631 apu1 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_CAPTURE);
1632 if (apu1 < 0)
1633 return apu1;
1634
1635 apu2 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_RATECONV);
1636 if (apu2 < 0) {
1637 snd_es1968_free_apu_pair(chip, apu1);
1638 return apu2;
1639 }
1640
1641 es = kcalloc(1, sizeof(*es), GFP_KERNEL);
1642 if (!es) {
1643 snd_es1968_free_apu_pair(chip, apu1);
1644 snd_es1968_free_apu_pair(chip, apu2);
1645 return -ENOMEM;
1646 }
1647
1648 es->apu[0] = apu1;
1649 es->apu[1] = apu1 + 1;
1650 es->apu[2] = apu2;
1651 es->apu[3] = apu2 + 1;
1652 es->apu_mode[0] = 0;
1653 es->apu_mode[1] = 0;
1654 es->apu_mode[2] = 0;
1655 es->apu_mode[3] = 0;
1656 es->running = 0;
1657 es->substream = substream;
1658 es->mode = ESM_MODE_CAPTURE;
1659
1660 /* get mixbuffer */
1661 if ((es->mixbuf = snd_es1968_new_memory(chip, ESM_MIXBUF_SIZE)) == NULL) {
1662 snd_es1968_free_apu_pair(chip, apu1);
1663 snd_es1968_free_apu_pair(chip, apu2);
1664 kfree(es);
1665 return -ENOMEM;
1666 }
1667 memset(es->mixbuf->buf.area, 0, ESM_MIXBUF_SIZE);
1668
1669 runtime->private_data = es;
1670 runtime->hw = snd_es1968_capture;
1671 runtime->hw.buffer_bytes_max = runtime->hw.period_bytes_max =
1672 calc_available_memory_size(chip) - 1024; /* keep MIXBUF size */
1673#if 0
1674 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1675 1024);
1676#endif
1677 spin_lock_irq(&chip->substream_lock);
1678 list_add(&es->list, &chip->substream_list);
1679 spin_unlock_irq(&chip->substream_lock);
1680
1681 return 0;
1682}
1683
1684static int snd_es1968_playback_close(snd_pcm_substream_t * substream)
1685{
1686 es1968_t *chip = snd_pcm_substream_chip(substream);
1687 esschan_t *es;
1688
1689 if (substream->runtime->private_data == NULL)
1690 return 0;
1691 es = substream->runtime->private_data;
1692 spin_lock_irq(&chip->substream_lock);
1693 list_del(&es->list);
1694 spin_unlock_irq(&chip->substream_lock);
1695 snd_es1968_free_apu_pair(chip, es->apu[0]);
1696 kfree(es);
1697
1698 return 0;
1699}
1700
1701static int snd_es1968_capture_close(snd_pcm_substream_t * substream)
1702{
1703 es1968_t *chip = snd_pcm_substream_chip(substream);
1704 esschan_t *es;
1705
1706 if (substream->runtime->private_data == NULL)
1707 return 0;
1708 es = substream->runtime->private_data;
1709 spin_lock_irq(&chip->substream_lock);
1710 list_del(&es->list);
1711 spin_unlock_irq(&chip->substream_lock);
1712 snd_es1968_free_memory(chip, es->mixbuf);
1713 snd_es1968_free_apu_pair(chip, es->apu[0]);
1714 snd_es1968_free_apu_pair(chip, es->apu[2]);
1715 kfree(es);
1716
1717 return 0;
1718}
1719static snd_pcm_ops_t snd_es1968_playback_ops = {
1720 /* open: */ snd_es1968_playback_open,
1721 /* close: */ snd_es1968_playback_close,
1722 /* ioctl: */ snd_pcm_lib_ioctl,
1723 /* hw_params:*/ snd_es1968_hw_params,
1724 /* hw_free: */ snd_es1968_hw_free,
1725 /* prepare: */ snd_es1968_pcm_prepare,
1726 /* trigger: */ snd_es1968_pcm_trigger,
1727 /* pointer: */ snd_es1968_pcm_pointer,
1728 0,0
1729};
1730
1731static snd_pcm_ops_t snd_es1968_capture_ops = {
1732 /* open: */ snd_es1968_capture_open,
1733 /* close: */ snd_es1968_capture_close,
1734 /* ioctl: */ snd_pcm_lib_ioctl,
1735 /* hw_params:*/ snd_es1968_hw_params,
1736 /* hw_free: */ snd_es1968_hw_free,
1737 /* prepare: */ snd_es1968_pcm_prepare,
1738 /* trigger: */ snd_es1968_pcm_trigger,
1739 /* pointer: */ snd_es1968_pcm_pointer,
1740 /* copy: */ 0,
1741 0
1742};
1743
1744
1745/*
1746 * measure clock
1747 */
1748#define CLOCK_MEASURE_BUFSIZE 16768 /* enough large for a single shot */
1749
1750static void __devinit es1968_measure_clock(es1968_t *chip)
1751{
1752 int i, apu;
1753 unsigned int pa, offset, t;
1754 esm_memory_t *memory;
1755 struct timeval start_time, stop_time;
1756
1757 if (chip->clock == 0)
1758 chip->clock = 48000; /* default clock value */
1759
1760 /* search 2 APUs (although one apu is enough) */
1761 if ((apu = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_PLAY)) < 0) {
1762 snd_printk("Hmm, cannot find empty APU pair!?\n");
1763 return;
1764 }
1765 if ((memory = snd_es1968_new_memory(chip, CLOCK_MEASURE_BUFSIZE)) == NULL) {
1766 snd_printk("cannot allocate dma buffer - using default clock %d\n", chip->clock);
1767 snd_es1968_free_apu_pair(chip, apu);
1768 return;
1769 }
1770
1771 memset(memory->buf.area, 0, CLOCK_MEASURE_BUFSIZE);
1772
1773 wave_set_register(chip, apu << 3, (memory->buf.addr - 0x10) & 0xfff8);
1774
1775 pa = (unsigned int)((memory->buf.addr - chip->dma.addr) >> 1);
1776 pa |= 0x00400000; /* System RAM (Bit 22) */
1777
1778 /* initialize apu */
1779 for (i = 0; i < 16; i++)
1780 apu_set_register(chip, apu, i, 0x0000);
1781
1782 apu_set_register(chip, apu, 0, 0x400f);
1783 apu_set_register(chip, apu, 4, ((pa >> 16) & 0xff) << 8);
1784 apu_set_register(chip, apu, 5, pa & 0xffff);
1785 apu_set_register(chip, apu, 6, (pa + CLOCK_MEASURE_BUFSIZE/2) & 0xffff);
1786 apu_set_register(chip, apu, 7, CLOCK_MEASURE_BUFSIZE/2);
1787 apu_set_register(chip, apu, 8, 0x0000);
1788 apu_set_register(chip, apu, 9, 0xD000);
1789 apu_set_register(chip, apu, 10, 0x8F08);
1790 apu_set_register(chip, apu, 11, 0x0000);
1791 spin_lock_irq(&chip->reg_lock);
1792 outw(1, chip->io_port + 0x04); /* clear WP interrupts */
1793 outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ); /* enable WP ints */
1794 spin_unlock_irq(&chip->reg_lock);
1795
1796 snd_es1968_apu_set_freq(chip, apu, ((unsigned int)48000 << 16) / chip->clock); /* 48000 Hz */
1797
1798 chip->in_measurement = 1;
1799 chip->measure_apu = apu;
1800 spin_lock_irq(&chip->reg_lock);
1801 snd_es1968_bob_inc(chip, ESM_BOB_FREQ);
1802 __apu_set_register(chip, apu, 5, pa & 0xffff);
1803 snd_es1968_trigger_apu(chip, apu, ESM_APU_16BITLINEAR);
1804 do_gettimeofday(&start_time);
1805 spin_unlock_irq(&chip->reg_lock);
1806 msleep(50);
1807 spin_lock_irq(&chip->reg_lock);
1808 offset = __apu_get_register(chip, apu, 5);
1809 do_gettimeofday(&stop_time);
1810 snd_es1968_trigger_apu(chip, apu, 0); /* stop */
1811 snd_es1968_bob_dec(chip);
1812 chip->in_measurement = 0;
1813 spin_unlock_irq(&chip->reg_lock);
1814
1815 /* check the current position */
1816 offset -= (pa & 0xffff);
1817 offset &= 0xfffe;
1818 offset += chip->measure_count * (CLOCK_MEASURE_BUFSIZE/2);
1819#if 0 /* vladest patch baseg on r.ihle suggestion */
1820 t = stop_time.tv_sec - start_time.tv_sec;
1821 t *= 1000000;
1822 if (stop_time.tv_usec < start_time.tv_usec)
1823 t -= start_time.tv_usec - stop_time.tv_usec;
1824 else
1825 t += stop_time.tv_usec - start_time.tv_usec;
1826#else
1827 t = 50000;
1828#endif
1829 if (t == 0) {
1830 snd_printk("?? calculation error..\n");
1831 } else {
1832 offset *= 1000;
1833 offset = (offset / t) * 1000 + ((offset % t) * 1000) / t;
1834 if (offset < 47500 || offset > 48500) {
1835 if (offset >= 40000 && offset <= 50000)
1836 chip->clock = (chip->clock * offset) / 48000;
1837 }
1838 printk(KERN_INFO "es1968: clocking to %d\n", chip->clock);
1839 }
1840 snd_es1968_free_memory(chip, memory);
1841 snd_es1968_free_apu_pair(chip, apu);
1842}
1843
1844
1845/*
1846 */
1847static void snd_es1968_pcm_free(snd_pcm_t *pcm)
1848{
1849 es1968_t *esm = pcm->private_data;
1850 snd_es1968_free_dmabuf(esm);
1851 esm->pcm = NULL;
1852}
1853
1854static int __devinit
1855snd_es1968_pcm(es1968_t *chip, int device)
1856{
1857 snd_pcm_t *pcm;
1858 int err;
1859
1860 /* get DMA buffer */
1861 if ((err = snd_es1968_init_dmabuf(chip)) < 0)
1862 return err;
1863
1864 /* set PCMBAR */
1865 wave_set_register(chip, 0x01FC, chip->dma.addr >> 12);
1866 wave_set_register(chip, 0x01FD, chip->dma.addr >> 12);
1867 wave_set_register(chip, 0x01FE, chip->dma.addr >> 12);
1868 wave_set_register(chip, 0x01FF, chip->dma.addr >> 12);
1869
1870 if ((err = snd_pcm_new(chip->card, "ESS Maestro", device,
1871 chip->playback_streams,
1872 chip->capture_streams, &pcm)) < 0)
1873 return err;
1874
1875 pcm->private_data = chip;
1876 pcm->private_free = snd_es1968_pcm_free;
1877
1878 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1968_playback_ops);
1879 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1968_capture_ops);
1880
1881 pcm->info_flags = 0;
1882
1883 strcpy(pcm->name, "ESS Maestro");
1884
1885 chip->pcm = pcm;
1886
1887 return 0;
1888}
1889
1890/*
1891 * update pointer
1892 */
1893static void snd_es1968_update_pcm(es1968_t *chip, esschan_t *es)
1894{
1895 unsigned int hwptr;
1896 unsigned int diff;
1897 snd_pcm_substream_t *subs = es->substream;
1898
1899 if (subs == NULL || !es->running)
1900 return;
1901
1902 hwptr = snd_es1968_get_dma_ptr(chip, es) << es->wav_shift;
1903 hwptr %= es->dma_size;
1904
1905 diff = (es->dma_size + hwptr - es->hwptr) % es->dma_size;
1906
1907 es->hwptr = hwptr;
1908 es->count += diff;
1909
1910 if (es->count > es->frag_size) {
1911 spin_unlock(&chip->substream_lock);
1912 snd_pcm_period_elapsed(subs);
1913 spin_lock(&chip->substream_lock);
1914 es->count %= es->frag_size;
1915 }
1916}
1917
1918/*
1919 */
1920static void es1968_update_hw_volume(unsigned long private_data)
1921{
1922 es1968_t *chip = (es1968_t *) private_data;
1923 int x, val;
1924 unsigned long flags;
1925
1926 /* Figure out which volume control button was pushed,
1927 based on differences from the default register
1928 values. */
1929 x = inb(chip->io_port + 0x1c);
1930 /* Reset the volume control registers. */
1931 outb(0x88, chip->io_port + 0x1c);
1932 outb(0x88, chip->io_port + 0x1d);
1933 outb(0x88, chip->io_port + 0x1e);
1934 outb(0x88, chip->io_port + 0x1f);
1935
1936 if (chip->in_suspend)
1937 return;
1938
1939 if (! chip->master_switch || ! chip->master_volume)
1940 return;
1941
1942 /* FIXME: we can't call snd_ac97_* functions since here is in tasklet. */
1943 spin_lock_irqsave(&chip->ac97_lock, flags);
1944 val = chip->ac97->regs[AC97_MASTER];
1945 if (x & 1) {
1946 /* mute */
1947 val ^= 0x8000;
1948 chip->ac97->regs[AC97_MASTER] = val;
1949 outw(val, chip->io_port + ESM_AC97_DATA);
1950 outb(AC97_MASTER, chip->io_port + ESM_AC97_INDEX);
1951 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1952 &chip->master_switch->id);
1953 } else {
1954 val &= 0x7fff;
1955 if (((x>>1) & 7) > 4) {
1956 /* volume up */
1957 if ((val & 0xff) > 0)
1958 val--;
1959 if ((val & 0xff00) > 0)
1960 val -= 0x0100;
1961 } else {
1962 /* volume down */
1963 if ((val & 0xff) < 0x1f)
1964 val++;
1965 if ((val & 0xff00) < 0x1f00)
1966 val += 0x0100;
1967 }
1968 chip->ac97->regs[AC97_MASTER] = val;
1969 outw(val, chip->io_port + ESM_AC97_DATA);
1970 outb(AC97_MASTER, chip->io_port + ESM_AC97_INDEX);
1971 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1972 &chip->master_volume->id);
1973 }
1974 spin_unlock_irqrestore(&chip->ac97_lock, flags);
1975}
1976
1977/*
1978 * interrupt handler
1979 */
1980static irqreturn_t snd_es1968_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1981{
1982 es1968_t *chip = dev_id;
1983 u32 event;
1984#ifdef TARGET_OS2
1985 int fOurIrq = FALSE;
1986#endif
1987
1988 if (!(event = inb(chip->io_port + 0x1A)))
1989 return IRQ_NONE;
1990
1991#ifdef TARGET_OS2
1992 fOurIrq = TRUE;
1993#endif
1994 outw(inw(chip->io_port + 4) & 1, chip->io_port + 4);
1995
1996 if (event & ESM_HWVOL_IRQ)
1997 tasklet_hi_schedule(&chip->hwvol_tq); /* we'll do this later */
1998
1999 /* else ack 'em all, i imagine */
2000 outb(0xFF, chip->io_port + 0x1A);
2001
2002 if ((event & ESM_MPU401_IRQ) && chip->rmidi) {
2003 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
2004 }
2005
2006 if (event & ESM_SOUND_IRQ) {
2007 struct list_head *p;
2008 spin_lock(&chip->substream_lock);
2009 list_for_each(p, &chip->substream_list) {
2010 esschan_t *es = list_entry(p, esschan_t, list);
2011 if (es->running)
2012 snd_es1968_update_pcm(chip, es);
2013 }
2014 spin_unlock(&chip->substream_lock);
2015 if (chip->in_measurement) {
2016 unsigned int curp = __apu_get_register(chip, chip->measure_apu, 5);
2017 if (curp < chip->measure_lastpos)
2018 chip->measure_count++;
2019 chip->measure_lastpos = curp;
2020 }
2021 }
2022
2023#ifdef TARGET_OS2
2024 if (fOurIrq) {
2025 eoi_irq(irq);
2026 }
2027#endif //TARGET_OS2
2028 return IRQ_HANDLED;
2029}
2030
2031/*
2032 * Mixer stuff
2033 */
2034
2035static int __devinit
2036snd_es1968_mixer(es1968_t *chip)
2037{
2038 ac97_bus_t *pbus;
2039 ac97_template_t ac97;
2040 snd_ctl_elem_id_t id;
2041 int err;
2042 static ac97_bus_ops_t ops = {
2043 0, snd_es1968_ac97_write,
2044 snd_es1968_ac97_read,0,0
2045 };
2046
2047 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
2048 return err;
2049 pbus->no_vra = 1; /* ES1968 doesn't need VRA */
2050
2051 memset(&ac97, 0, sizeof(ac97));
2052 ac97.private_data = chip;
2053 if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97)) < 0)
2054 return err;
2055
2056 /* attach master switch / volumes for h/w volume control */
2057 memset(&id, 0, sizeof(id));
2058 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2059 strcpy(id.name, "Master Playback Switch");
2060 chip->master_switch = snd_ctl_find_id(chip->card, &id);
2061 memset(&id, 0, sizeof(id));
2062 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2063 strcpy(id.name, "Master Playback Volume");
2064 chip->master_volume = snd_ctl_find_id(chip->card, &id);
2065
2066 return 0;
2067}
2068
2069/*
2070 * reset ac97 codec
2071 */
2072
2073static void snd_es1968_ac97_reset(es1968_t *chip)
2074{
2075 unsigned long ioaddr = chip->io_port;
2076
2077 unsigned short save_ringbus_a;
2078 unsigned short save_68;
2079 unsigned short w;
2080 unsigned int vend;
2081
2082 /* save configuration */
2083 save_ringbus_a = inw(ioaddr + 0x36);
2084
2085 //outw(inw(ioaddr + 0x38) & 0xfffc, ioaddr + 0x38); /* clear second codec id? */
2086 /* set command/status address i/o to 1st codec */
2087 outw(inw(ioaddr + 0x3a) & 0xfffc, ioaddr + 0x3a);
2088 outw(inw(ioaddr + 0x3c) & 0xfffc, ioaddr + 0x3c);
2089
2090 /* disable ac link */
2091 outw(0x0000, ioaddr + 0x36);
2092 save_68 = inw(ioaddr + 0x68);
2093 pci_read_config_word(chip->pci, 0x58, &w); /* something magical with gpio and bus arb. */
2094 pci_read_config_dword(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, &vend);
2095 if (w & 1)
2096 save_68 |= 0x10;
2097 outw(0xfffe, ioaddr + 0x64); /* unmask gpio 0 */
2098 outw(0x0001, ioaddr + 0x68); /* gpio write */
2099 outw(0x0000, ioaddr + 0x60); /* write 0 to gpio 0 */
2100 udelay(20);
2101 outw(0x0001, ioaddr + 0x60); /* write 1 to gpio 1 */
2102 msleep(20);
2103
2104 outw(save_68 | 0x1, ioaddr + 0x68); /* now restore .. */
2105 outw((inw(ioaddr + 0x38) & 0xfffc) | 0x1, ioaddr + 0x38);
2106 outw((inw(ioaddr + 0x3a) & 0xfffc) | 0x1, ioaddr + 0x3a);
2107 outw((inw(ioaddr + 0x3c) & 0xfffc) | 0x1, ioaddr + 0x3c);
2108
2109 /* now the second codec */
2110 /* disable ac link */
2111 outw(0x0000, ioaddr + 0x36);
2112 outw(0xfff7, ioaddr + 0x64); /* unmask gpio 3 */
2113 save_68 = inw(ioaddr + 0x68);
2114 outw(0x0009, ioaddr + 0x68); /* gpio write 0 & 3 ?? */
2115 outw(0x0001, ioaddr + 0x60); /* write 1 to gpio */
2116 udelay(20);
2117 outw(0x0009, ioaddr + 0x60); /* write 9 to gpio */
2118 msleep(500);
2119 //outw(inw(ioaddr + 0x38) & 0xfffc, ioaddr + 0x38);
2120 outw(inw(ioaddr + 0x3a) & 0xfffc, ioaddr + 0x3a);
2121 outw(inw(ioaddr + 0x3c) & 0xfffc, ioaddr + 0x3c);
2122
2123#if 0 /* the loop here needs to be much better if we want it.. */
2124 snd_printk("trying software reset\n");
2125 /* try and do a software reset */
2126 outb(0x80 | 0x7c, ioaddr + 0x30);
2127 for (w = 0;; w++) {
2128 if ((inw(ioaddr + 0x30) & 1) == 0) {
2129 if (inb(ioaddr + 0x32) != 0)
2130 break;
2131
2132 outb(0x80 | 0x7d, ioaddr + 0x30);
2133 if (((inw(ioaddr + 0x30) & 1) == 0)
2134 && (inb(ioaddr + 0x32) != 0))
2135 break;
2136 outb(0x80 | 0x7f, ioaddr + 0x30);
2137 if (((inw(ioaddr + 0x30) & 1) == 0)
2138 && (inb(ioaddr + 0x32) != 0))
2139 break;
2140 }
2141
2142 if (w > 10000) {
2143 outb(inb(ioaddr + 0x37) | 0x08, ioaddr + 0x37); /* do a software reset */
2144 msleep(500); /* oh my.. */
2145 outb(inb(ioaddr + 0x37) & ~0x08,
2146 ioaddr + 0x37);
2147 udelay(1);
2148 outw(0x80, ioaddr + 0x30);
2149 for (w = 0; w < 10000; w++) {
2150 if ((inw(ioaddr + 0x30) & 1) == 0)
2151 break;
2152 }
2153 }
2154 }
2155#endif
2156 if (vend == NEC_VERSA_SUBID1 || vend == NEC_VERSA_SUBID2) {
2157 /* turn on external amp? */
2158 outw(0xf9ff, ioaddr + 0x64);
2159 outw(inw(ioaddr + 0x68) | 0x600, ioaddr + 0x68);
2160 outw(0x0209, ioaddr + 0x60);
2161 }
2162
2163 /* restore.. */
2164 outw(save_ringbus_a, ioaddr + 0x36);
2165
2166 /* Turn on the 978 docking chip.
2167 First frob the "master output enable" bit,
2168 then set most of the playback volume control registers to max. */
2169 outb(inb(ioaddr+0xc0)|(1<<5), ioaddr+0xc0);
2170 outb(0xff, ioaddr+0xc3);
2171 outb(0xff, ioaddr+0xc4);
2172 outb(0xff, ioaddr+0xc6);
2173 outb(0xff, ioaddr+0xc8);
2174 outb(0x3f, ioaddr+0xcf);
2175 outb(0x3f, ioaddr+0xd0);
2176}
2177
2178static void snd_es1968_reset(es1968_t *chip)
2179{
2180 /* Reset */
2181 outw(ESM_RESET_MAESTRO | ESM_RESET_DIRECTSOUND,
2182 chip->io_port + ESM_PORT_HOST_IRQ);
2183 udelay(10);
2184 outw(0x0000, chip->io_port + ESM_PORT_HOST_IRQ);
2185 udelay(10);
2186}
2187
2188/*
2189 * power management
2190 */
2191static void snd_es1968_set_acpi(es1968_t *chip, int state)
2192{
2193 u16 active_mask = acpi_state_mask[state];
2194
2195 pci_set_power_state(chip->pci, state);
2196 /* make sure the units we care about are on
2197 XXX we might want to do this before state flipping? */
2198 pci_write_config_word(chip->pci, 0x54, ~ active_mask);
2199 pci_write_config_word(chip->pci, 0x56, ~ active_mask);
2200}
2201
2202
2203/*
2204 * initialize maestro chip
2205 */
2206static void snd_es1968_chip_init(es1968_t *chip)
2207{
2208 struct pci_dev *pci = chip->pci;
2209 int i;
2210 unsigned long iobase = chip->io_port;
2211 u16 w;
2212 u32 n;
2213
2214 /* We used to muck around with pci config space that
2215 * we had no business messing with. We don't know enough
2216 * about the machine to know which DMA mode is appropriate,
2217 * etc. We were guessing wrong on some machines and making
2218 * them unhappy. We now trust in the BIOS to do things right,
2219 * which almost certainly means a new host of problems will
2220 * arise with broken BIOS implementations. screw 'em.
2221 * We're already intolerant of machines that don't assign
2222 * IRQs.
2223 */
2224
2225 /* do config work at full power */
2226 snd_es1968_set_acpi(chip, ACPI_D0);
2227
2228 /* Config Reg A */
2229 pci_read_config_word(pci, ESM_CONFIG_A, &w);
2230
2231 /* Use TDMA for now. TDMA works on all boards, so while its
2232 * not the most efficient its the simplest. */
2233 w &= ~DMA_CLEAR; /* Clear DMA bits */
2234 w |= DMA_TDMA; /* TDMA on */
2235 w &= ~(PIC_SNOOP1 | PIC_SNOOP2); /* Clear Pic Snoop Mode Bits */
2236 w &= ~SAFEGUARD; /* Safeguard off */
2237 w |= POST_WRITE; /* Posted write */
2238 w |= ISA_TIMING; /* ISA timing on */
2239 /* XXX huh? claims to be reserved.. */
2240 w &= ~SWAP_LR; /* swap left/right
2241 seems to only have effect on SB
2242 Emulation */
2243 w &= ~SUBTR_DECODE; /* Subtractive decode off */
2244
2245 pci_write_config_word(pci, ESM_CONFIG_A, w);
2246
2247 /* Config Reg B */
2248
2249 pci_read_config_word(pci, ESM_CONFIG_B, &w);
2250
2251 w &= ~(1 << 15); /* Turn off internal clock multiplier */
2252 /* XXX how do we know which to use? */
2253 w &= ~(1 << 14); /* External clock */
2254
2255 w &= ~SPDIF_CONFB; /* disable S/PDIF output */
2256 w |= HWV_CONFB; /* HWV on */
2257 w |= DEBOUNCE; /* Debounce off: easier to push the HW buttons */
2258 w &= ~GPIO_CONFB; /* GPIO 4:5 */
2259 w |= CHI_CONFB; /* Disconnect from the CHI. Enabling this made a dell 7500 work. */
2260 w &= ~IDMA_CONFB; /* IDMA off (undocumented) */
2261 w &= ~MIDI_FIX; /* MIDI fix off (undoc) */
2262 w &= ~(1 << 1); /* reserved, always write 0 */
2263 w &= ~IRQ_TO_ISA; /* IRQ to ISA off (undoc) */
2264
2265 pci_write_config_word(pci, ESM_CONFIG_B, w);
2266
2267 /* DDMA off */
2268
2269 pci_read_config_word(pci, ESM_DDMA, &w);
2270 w &= ~(1 << 0);
2271 pci_write_config_word(pci, ESM_DDMA, w);
2272
2273 /*
2274 * Legacy mode
2275 */
2276
2277 pci_read_config_word(pci, ESM_LEGACY_AUDIO_CONTROL, &w);
2278
2279 w &= ~ESS_ENABLE_AUDIO; /* Disable Legacy Audio */
2280 w &= ~ESS_ENABLE_SERIAL_IRQ; /* Disable SIRQ */
2281 w &= ~(0x1f); /* disable mpu irq/io, game port, fm, SB */
2282
2283 pci_write_config_word(pci, ESM_LEGACY_AUDIO_CONTROL, w);
2284
2285 /* Set up 978 docking control chip. */
2286 pci_read_config_word(pci, 0x58, &w);
2287 w|=1<<2; /* Enable 978. */
2288 w|=1<<3; /* Turn on 978 hardware volume control. */
2289 w&=~(1<<11); /* Turn on 978 mixer volume control. */
2290 pci_write_config_word(pci, 0x58, w);
2291
2292 /* Sound Reset */
2293
2294 snd_es1968_reset(chip);
2295
2296 /*
2297 * Ring Bus Setup
2298 */
2299
2300 /* setup usual 0x34 stuff.. 0x36 may be chip specific */
2301 outw(0xC090, iobase + ESM_RING_BUS_DEST); /* direct sound, stereo */
2302 udelay(20);
2303 outw(0x3000, iobase + ESM_RING_BUS_CONTR_A); /* enable ringbus/serial */
2304 udelay(20);
2305
2306 /*
2307 * Reset the CODEC
2308 */
2309
2310 snd_es1968_ac97_reset(chip);
2311
2312 /* Ring Bus Control B */
2313
2314 n = inl(iobase + ESM_RING_BUS_CONTR_B);
2315 n &= ~RINGB_EN_SPDIF; /* SPDIF off */
2316 //w |= RINGB_EN_2CODEC; /* enable 2nd codec */
2317 outl(n, iobase + ESM_RING_BUS_CONTR_B);
2318
2319 /* Set hardware volume control registers to midpoints.
2320 We can tell which button was pushed based on how they change. */
2321 outb(0x88, iobase+0x1c);
2322 outb(0x88, iobase+0x1d);
2323 outb(0x88, iobase+0x1e);
2324 outb(0x88, iobase+0x1f);
2325
2326 /* it appears some maestros (dell 7500) only work if these are set,
2327 regardless of wether we use the assp or not. */
2328
2329 outb(0, iobase + ASSP_CONTROL_B);
2330 outb(3, iobase + ASSP_CONTROL_A); /* M: Reserved bits... */
2331 outb(0, iobase + ASSP_CONTROL_C); /* M: Disable ASSP, ASSP IRQ's and FM Port */
2332
2333 /*
2334 * set up wavecache
2335 */
2336 for (i = 0; i < 16; i++) {
2337 /* Write 0 into the buffer area 0x1E0->1EF */
2338 outw(0x01E0 + i, iobase + WC_INDEX);
2339 outw(0x0000, iobase + WC_DATA);
2340
2341 /* The 1.10 test program seem to write 0 into the buffer area
2342 * 0x1D0-0x1DF too.*/
2343 outw(0x01D0 + i, iobase + WC_INDEX);
2344 outw(0x0000, iobase + WC_DATA);
2345 }
2346 wave_set_register(chip, IDR7_WAVE_ROMRAM,
2347 (wave_get_register(chip, IDR7_WAVE_ROMRAM) & 0xFF00));
2348 wave_set_register(chip, IDR7_WAVE_ROMRAM,
2349 wave_get_register(chip, IDR7_WAVE_ROMRAM) | 0x100);
2350 wave_set_register(chip, IDR7_WAVE_ROMRAM,
2351 wave_get_register(chip, IDR7_WAVE_ROMRAM) & ~0x200);
2352 wave_set_register(chip, IDR7_WAVE_ROMRAM,
2353 wave_get_register(chip, IDR7_WAVE_ROMRAM) | ~0x400);
2354
2355
2356 maestro_write(chip, IDR2_CRAM_DATA, 0x0000);
2357 /* Now back to the DirectSound stuff */
2358 /* audio serial configuration.. ? */
2359 maestro_write(chip, 0x08, 0xB004);
2360 maestro_write(chip, 0x09, 0x001B);
2361 maestro_write(chip, 0x0A, 0x8000);
2362 maestro_write(chip, 0x0B, 0x3F37);
2363 maestro_write(chip, 0x0C, 0x0098);
2364
2365 /* parallel in, has something to do with recording :) */
2366 maestro_write(chip, 0x0C,
2367 (maestro_read(chip, 0x0C) & ~0xF000) | 0x8000);
2368 /* parallel out */
2369 maestro_write(chip, 0x0C,
2370 (maestro_read(chip, 0x0C) & ~0x0F00) | 0x0500);
2371
2372 maestro_write(chip, 0x0D, 0x7632);
2373
2374 /* Wave cache control on - test off, sg off,
2375 enable, enable extra chans 1Mb */
2376
2377 w = inw(iobase + WC_CONTROL);
2378
2379 w &= ~0xFA00; /* Seems to be reserved? I don't know */
2380 w |= 0xA000; /* reserved... I don't know */
2381 w &= ~0x0200; /* Channels 56,57,58,59 as Extra Play,Rec Channel enable
2382 Seems to crash the Computer if enabled... */
2383 w |= 0x0100; /* Wave Cache Operation Enabled */
2384 w |= 0x0080; /* Channels 60/61 as Placback/Record enabled */
2385 w &= ~0x0060; /* Clear Wavtable Size */
2386 w |= 0x0020; /* Wavetable Size : 1MB */
2387 /* Bit 4 is reserved */
2388 w &= ~0x000C; /* DMA Stuff? I don't understand what the datasheet means */
2389 /* Bit 1 is reserved */
2390 w &= ~0x0001; /* Test Mode off */
2391
2392 outw(w, iobase + WC_CONTROL);
2393
2394 /* Now clear the APU control ram */
2395 for (i = 0; i < NR_APUS; i++) {
2396 for (w = 0; w < NR_APU_REGS; w++)
2397 apu_set_register(chip, i, w, 0);
2398
2399 }
2400}
2401
2402/* Enable IRQ's */
2403static void snd_es1968_start_irq(es1968_t *chip)
2404{
2405 unsigned short w;
2406 w = ESM_HIRQ_DSIE | ESM_HIRQ_HW_VOLUME;
2407 if (chip->rmidi)
2408 w |= ESM_HIRQ_MPU401;
2409 outw(w, chip->io_port + ESM_PORT_HOST_IRQ);
2410}
2411
2412#ifdef CONFIG_PM
2413/*
2414 * PM support
2415 */
2416static int es1968_suspend(snd_card_t *card, unsigned int state)
2417{
2418 es1968_t *chip = card->pm_private_data;
2419
2420 if (! chip->do_pm)
2421 return 0;
2422
2423 chip->in_suspend = 1;
2424 snd_pcm_suspend_all(chip->pcm);
2425 snd_ac97_suspend(chip->ac97);
2426 snd_es1968_bob_stop(chip);
2427 snd_es1968_set_acpi(chip, ACPI_D3);
2428 return 0;
2429}
2430
2431static int es1968_resume(snd_card_t *card, unsigned int state)
2432{
2433 es1968_t *chip = card->pm_private_data;
2434 struct list_head *p;
2435
2436 if (! chip->do_pm)
2437 return 0;
2438
2439 /* restore all our config */
2440 pci_enable_device(chip->pci);
2441 pci_set_master(chip->pci);
2442 snd_es1968_chip_init(chip);
2443
2444 /* need to restore the base pointers.. */
2445 if (chip->dma.addr) {
2446 /* set PCMBAR */
2447 wave_set_register(chip, 0x01FC, chip->dma.addr >> 12);
2448 // by vladest
2449 // wave_set_register(chip, 0x01FD, chip->dma.addr >> 12);
2450 // wave_set_register(chip, 0x01FE, chip->dma.addr >> 12);
2451 // wave_set_register(chip, 0x01FF, chip->dma.addr >> 12);
2452 // !by vladest
2453
2454 }
2455
2456 snd_es1968_start_irq(chip);
2457
2458 // by vladest 06.10.2003 16:22
2459 // es1968_measure_clock(chip);
2460 //! by vladest
2461
2462 /* restore ac97 state */
2463 snd_ac97_resume(chip->ac97);
2464
2465 list_for_each(p, &chip->substream_list) {
2466 esschan_t *es = list_entry(p, esschan_t, list);
2467 switch (es->mode) {
2468 case ESM_MODE_PLAY:
2469 snd_es1968_playback_setup(chip, es, es->substream->runtime);
2470 break;
2471 case ESM_MODE_CAPTURE:
2472 snd_es1968_capture_setup(chip, es, es->substream->runtime);
2473 break;
2474 }
2475 }
2476 /* start timer again */
2477 if (chip->bobclient)
2478 snd_es1968_bob_start(chip);
2479
2480 chip->in_suspend = 0;
2481 return 0;
2482}
2483
2484#endif /* CONFIG_PM */
2485
2486static int snd_es1968_free(es1968_t *chip)
2487{
2488 if (chip->io_port) {
2489 synchronize_irq(chip->irq);
2490 outw(1, chip->io_port + 0x04); /* clear WP interrupts */
2491 outw(0, chip->io_port + ESM_PORT_HOST_IRQ); /* disable IRQ */
2492 }
2493 if (chip->irq >= 0)
2494 free_irq(chip->irq, (void *)chip);
2495 snd_es1968_set_acpi(chip, ACPI_D3);
2496 chip->master_switch = NULL;
2497 chip->master_volume = NULL;
2498 pci_release_regions(chip->pci);
2499 kfree(chip);
2500 return 0;
2501}
2502
2503static int snd_es1968_dev_free(snd_device_t *device)
2504{
2505 es1968_t *chip = device->device_data;
2506 return snd_es1968_free(chip);
2507}
2508
2509struct ess_device_list {
2510 unsigned short type; /* chip type */
2511 unsigned short vendor; /* subsystem vendor id */
2512};
2513
2514static struct ess_device_list pm_whitelist[] __devinitdata = {
2515 { TYPE_MAESTRO2E, 0x0e11 }, /* Compaq Armada */
2516 { TYPE_MAESTRO2E, 0x1028 },
2517 { TYPE_MAESTRO2E, 0x103c },
2518 { TYPE_MAESTRO2E, 0x1179 },
2519 { TYPE_MAESTRO2E, 0x14c0 }, /* HP omnibook 4150 */
2520 { TYPE_MAESTRO2E, 0x1558 },
2521};
2522
2523static struct ess_device_list mpu_blacklist[] __devinitdata = {
2524 { TYPE_MAESTRO2, 0x125d },
2525};
2526
2527static int __devinit snd_es1968_create(snd_card_t * card,
2528 struct pci_dev *pci,
2529 int total_bufsize,
2530 int play_streams,
2531 int capt_streams,
2532 int chip_type,
2533 int do_pm,
2534 es1968_t **chip_ret)
2535{
2536#ifdef TARGET_OS2
2537 static snd_device_ops_t ops = {
2538 snd_es1968_dev_free,0,0,0
2539 };
2540#else
2541 static snd_device_ops_t ops = {
2542 dev_free: snd_es1968_dev_free,
2543 };
2544#endif
2545 es1968_t *chip;
2546 int i, err;
2547
2548 *chip_ret = NULL;
2549
2550 /* enable PCI device */
2551 if ((err = pci_enable_device(pci)) < 0)
2552 return err;
2553 /* check, if we can restrict PCI DMA transfers to 28 bits */
2554 if (!pci_dma_supported(pci, 0x0fffffff)) {
2555 snd_printk("architecture does not support 28bit PCI busmaster DMA\n");
2556 return -ENXIO;
2557 }
2558 pci_set_consistent_dma_mask(pci, 0x0fffffff);
2559
2560 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
2561 if (! chip)
2562 return -ENOMEM;
2563
2564 /* Set Vars */
2565 chip->type = chip_type;
2566 spin_lock_init(&chip->reg_lock);
2567 spin_lock_init(&chip->substream_lock);
2568 INIT_LIST_HEAD(&chip->buf_list);
2569 INIT_LIST_HEAD(&chip->substream_list);
2570 spin_lock_init(&chip->ac97_lock);
2571 init_MUTEX(&chip->memory_mutex);
2572 tasklet_init(&chip->hwvol_tq, es1968_update_hw_volume, (unsigned long)chip);
2573 chip->card = card;
2574 chip->pci = pci;
2575 chip->irq = -1;
2576 chip->total_bufsize = total_bufsize; /* in bytes */
2577 chip->playback_streams = play_streams;
2578 chip->capture_streams = capt_streams;
2579
2580 if ((err = pci_request_regions(pci, "ESS Maestro")) < 0) {
2581 kfree(chip);
2582 return err;
2583 }
2584 chip->io_port = pci_resource_start(pci, 0);
2585 if (request_irq(pci->irq, snd_es1968_interrupt, SA_INTERRUPT|SA_SHIRQ,
2586 "ESS Maestro", (void*)chip)) {
2587 snd_printk("unable to grab IRQ %d\n", pci->irq);
2588 snd_es1968_free(chip);
2589 return -EBUSY;
2590 }
2591 chip->irq = pci->irq;
2592
2593 /* Clear Maestro_map */
2594 for (i = 0; i < 32; i++)
2595 chip->maestro_map[i] = 0;
2596
2597 /* Clear Apu Map */
2598 for (i = 0; i < NR_APUS; i++)
2599 chip->apu[i] = ESM_APU_FREE;
2600
2601 /* just to be sure */
2602 pci_set_master(pci);
2603
2604 if (do_pm > 1) {
2605 /* disable power-management if not on the whitelist */
2606 unsigned short vend;
2607 pci_read_config_word(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, &vend);
2608 for (i = 0; i < (int)ARRAY_SIZE(pm_whitelist); i++) {
2609 if (chip->type == pm_whitelist[i].type &&
2610 vend == pm_whitelist[i].vendor) {
2611 do_pm = 1;
2612 break;
2613 }
2614 }
2615 if (do_pm > 1) {
2616 /* not matched; disabling pm */
2617 printk(KERN_INFO "es1968: not attempting power management.\n");
2618 do_pm = 0;
2619 }
2620 }
2621 chip->do_pm = do_pm;
2622
2623 snd_es1968_chip_init(chip);
2624
2625 if (chip->do_pm)
2626 snd_card_set_pm_callback(card, es1968_suspend, es1968_resume, chip);
2627
2628 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2629 snd_es1968_free(chip);
2630 return err;
2631 }
2632
2633 *chip_ret = chip;
2634
2635 return 0;
2636}
2637
2638
2639/*
2640 * joystick
2641 */
2642
2643static int snd_es1968_joystick_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
2644{
2645 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2646 uinfo->count = 1;
2647 uinfo->value.integer.min = 0;
2648 uinfo->value.integer.max = 1;
2649 return 0;
2650}
2651
2652static int snd_es1968_joystick_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
2653{
2654 es1968_t *chip = snd_kcontrol_chip(kcontrol);
2655 u16 val;
2656
2657 pci_read_config_word(chip->pci, ESM_LEGACY_AUDIO_CONTROL, &val);
2658 ucontrol->value.integer.value[0] = (val & 0x04) ? 1 : 0;
2659 return 0;
2660}
2661
2662static int snd_es1968_joystick_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
2663{
2664 es1968_t *chip = snd_kcontrol_chip(kcontrol);
2665 u16 val, oval;
2666
2667 pci_read_config_word(chip->pci, ESM_LEGACY_AUDIO_CONTROL, &oval);
2668 val = oval & ~0x04;
2669 if (ucontrol->value.integer.value[0])
2670 val |= 0x04;
2671 if (val != oval) {
2672 pci_write_config_word(chip->pci, ESM_LEGACY_AUDIO_CONTROL, val);
2673 return 1;
2674 }
2675 return 0;
2676}
2677
2678#define num_controls(ary) (sizeof(ary) / sizeof(snd_kcontrol_new_t))
2679
2680static snd_kcontrol_new_t snd_es1968_control_switches[] __devinitdata = {
2681 {
2682 /* .iface = */SNDRV_CTL_ELEM_IFACE_CARD,0,0,
2683 /* .name = */"Joystick",0,0,0,
2684 /* .info = */snd_es1968_joystick_info,
2685 /* .get = */snd_es1968_joystick_get,
2686 /* .put = */snd_es1968_joystick_put,
2687 }
2688};
2689
2690/*
2691 */
2692static int __devinit snd_es1968_probe(struct pci_dev *pci,
2693 const struct pci_device_id *pci_id)
2694{
2695 static int dev;
2696 snd_card_t *card;
2697 es1968_t *chip;
2698 unsigned int i;
2699 int err;
2700
2701 printk("snd_es1968_probe cp1\n");
2702 if (dev >= SNDRV_CARDS)
2703 return -ENODEV;
2704 if (!enable[dev]) {
2705 dev++;
2706 return -ENOENT;
2707 }
2708
2709 printk("snd_es1968_probe cp2\n");
2710 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2711 if (!card)
2712 return -ENOMEM;
2713
2714 printk("snd_es1968_probe cp3\n");
2715
2716 if (total_bufsize[dev] < 128)
2717 total_bufsize[dev] = 128;
2718 if (total_bufsize[dev] > 4096)
2719 total_bufsize[dev] = 4096;
2720 if ((err = snd_es1968_create(card, pci,
2721 total_bufsize[dev] * 1024, /* in bytes */
2722 pcm_substreams_p[dev],
2723 pcm_substreams_c[dev],
2724 pci_id->driver_data,
2725 use_pm[dev],
2726 &chip)) < 0) {
2727 snd_card_free(card);
2728 return err;
2729 }
2730
2731 printk("snd_es1968_probe cp4n");
2732
2733 switch (chip->type) {
2734 case TYPE_MAESTRO2E:
2735 strcpy(card->driver, "ES1978");
2736 strcpy(card->shortname, "ESS ES1978 (Maestro 2E)");
2737 break;
2738 case TYPE_MAESTRO2:
2739 strcpy(card->driver, "ES1968");
2740 strcpy(card->shortname, "ESS ES1968 (Maestro 2)");
2741 break;
2742 case TYPE_MAESTRO:
2743 strcpy(card->driver, "ESM1");
2744 strcpy(card->shortname, "ESS Maestro 1");
2745 break;
2746 }
2747
2748 if ((err = snd_es1968_pcm(chip, 0)) < 0) {
2749 snd_card_free(card);
2750 return err;
2751 }
2752 printk("snd_es1968_probe cp5\n");
2753
2754
2755 if ((err = snd_es1968_mixer(chip)) < 0) {
2756 snd_card_free(card);
2757 return err;
2758 }
2759
2760 printk("snd_es1968_probe cp6\n");
2761
2762 if (enable_mpu[dev] == 2) {
2763 /* check the black list */
2764 unsigned short vend;
2765 pci_read_config_word(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, &vend);
2766 for (i = 0; i < ARRAY_SIZE(mpu_blacklist); i++) {
2767 if (chip->type == mpu_blacklist[i].type &&
2768 vend == mpu_blacklist[i].vendor) {
2769 enable_mpu[dev] = 0;
2770 break;
2771 }
2772 }
2773 }
2774 printk("snd_es1968_probe cp7\n");
2775
2776 if (enable_mpu[dev]) {
2777 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
2778 chip->io_port + ESM_MPU401_PORT,
2779 MPU401_INFO_INTEGRATED,
2780 chip->irq, 0, &chip->rmidi)) < 0) {
2781 printk(KERN_WARNING "es1968: skipping MPU-401 MIDI support..\n");
2782 }
2783 }
2784
2785 printk("snd_es1968_probe cp8\n");
2786
2787 /* card switches */
2788 for (i = 0; i < num_controls(snd_es1968_control_switches); i++) {
2789 err = snd_ctl_add(card, snd_ctl_new1(&snd_es1968_control_switches[i], chip));
2790 if (err < 0) {
2791 snd_card_free(card);
2792 return err;
2793 }
2794 }
2795 printk("snd_es1968_probe cp9\n");
2796
2797
2798 snd_es1968_start_irq(chip);
2799
2800 chip->clock = clock[dev];
2801 if (! chip->clock)
2802 es1968_measure_clock(chip);
2803
2804 printk("snd_es1968_probe cp10\n");
2805
2806 sprintf(card->longname, "%s at 0x%lx, irq %i",
2807 card->shortname, chip->io_port, chip->irq);
2808
2809 if ((err = snd_card_register(card)) < 0) {
2810 snd_card_free(card);
2811 return err;
2812 }
2813 printk("snd_es1968_probe cp11\n");
2814
2815 pci_set_drvdata(pci, card);
2816 printk("snd_es1968_probe cp12\n");
2817
2818 dev++;
2819 return 0;
2820}
2821
2822static void __devexit snd_es1968_remove(struct pci_dev *pci)
2823{
2824 snd_card_free(pci_get_drvdata(pci));
2825 pci_set_drvdata(pci, NULL);
2826}
2827
2828static struct pci_driver driver = {
2829 0, 0, 0,
2830 "ES1968 (ESS Maestro)",
2831 snd_es1968_ids,
2832 snd_es1968_probe,
2833 snd_es1968_remove,
2834 SND_PCI_PM_CALLBACKS
2835};
2836
2837static int __init alsa_card_es1968_init(void)
2838{
2839 int err;
2840
2841 if ((err = pci_module_init(&driver)) < 0) {
2842#ifdef MODULE
2843 // snd_printk("ESS Maestro soundcard not found or device busy\n");
2844#endif
2845 return err;
2846 }
2847 return 0;
2848}
2849
2850static void __exit alsa_card_es1968_exit(void)
2851{
2852#if 0 // do we really need this?
2853 unregister_reboot_notifier(&snd_es1968_nb);
2854#endif
2855 pci_unregister_driver(&driver);
2856}
2857
2858module_init(alsa_card_es1968_init)
2859module_exit(alsa_card_es1968_exit)
2860
2861#ifndef MODULE
2862
2863/* format is: snd-es1968=enable,index,id,
2864 total_bufsize,
2865 pcm_substreams_p,
2866 pcm_substreams_c,
2867 clock,
2868 use_pm,
2869 enable_mpu
2870 */
2871
2872static int __init alsa_card_es1968_setup(char *str)
2873{
2874 static unsigned __initdata nr_dev = 0;
2875
2876 if (nr_dev >= SNDRV_CARDS)
2877 return 0;
2878 (void)(get_option(&str,&enable[nr_dev]) == 2 &&
2879 get_option(&str,&index[nr_dev]) == 2 &&
2880 get_id(&str,&id[nr_dev]) == 2 &&
2881 get_option(&str,&total_bufsize[nr_dev]) == 2 &&
2882 get_option(&str,&pcm_substreams_p[nr_dev]) == 2 &&
2883 get_option(&str,&pcm_substreams_c[nr_dev]) == 2 &&
2884 get_option(&str,&clock[nr_dev]) == 2 &&
2885 get_option(&str,&use_pm[nr_dev]) == 2 &&
2886 get_option(&str,&enable_mpu[nr_dev]) == 2);
2887 nr_dev++;
2888 return 1;
2889}
2890
2891__setup("snd-es1968=", alsa_card_es1968_setup);
2892
2893#endif /* ifndef MODULE */
Note: See TracBrowser for help on using the repository browser.