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

Last change on this file since 426 was 426, checked in by Paul Smedley, 16 years ago

Update alsa-kernel to ALSA 1.0.20 level

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