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

Last change on this file since 695 was 695, checked in by David Azarewicz, 4 years ago

Merge changes from next branch.

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