Blame view

sound/pci/es1938.c 55.5 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /*
   *  Driver for ESS Solo-1 (ES1938, ES1946, ES1969) soundcard
   *  Copyright (c) by Jaromir Koutek <miri@punknet.cz>,
c1017a4cd   Jaroslav Kysela   [ALSA] Changed Ja...
4
   *                   Jaroslav Kysela <perex@perex.cz>,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
   *                   Thomas Sailer <sailer@ife.ee.ethz.ch>,
   *                   Abramo Bagnara <abramo@alsa-project.org>,
   *                   Markus Gruber <gruber@eikon.tum.de>
   * 
   * Rewritten from sonicvibes.c source.
   *
   *  TODO:
   *    Rewrite better spinlocks
   *
   *
   *   This program is free software; you can redistribute it and/or modify
   *   it under the terms of the GNU General Public License as published by
   *   the Free Software Foundation; either version 2 of the License, or
   *   (at your option) any later version.
   *
   *   This program is distributed in the hope that it will be useful,
   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *   GNU General Public License for more details.
   *
   *   You should have received a copy of the GNU General Public License
   *   along with this program; if not, write to the Free Software
   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   *
   */
  
  /*
    NOTES:
    - Capture data is written unaligned starting from dma_base + 1 so I need to
      disable mmap and to add a copy callback.
    - After several cycle of the following:
      while : ; do arecord -d1 -f cd -t raw | aplay -f cd ; done
      a "playback write error (DMA or IRQ trouble?)" may happen.
      This is due to playback interrupts not generated.
      I suspect a timing issue.
    - Sometimes the interrupt handler is invoked wrongly during playback.
      This generates some harmless "Unexpected hw_pointer: wrong interrupt
      acknowledge".
      I've seen that using small period sizes.
      Reproducible with:
      mpg123 test.mp3 &
      hdparm -t -T /dev/hda
  */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
49
50
51
52
  #include <linux/init.h>
  #include <linux/interrupt.h>
  #include <linux/pci.h>
  #include <linux/slab.h>
  #include <linux/gameport.h>
65a772172   Paul Gortmaker   sound: fix driver...
53
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
  #include <linux/delay.h>
910638ae7   Matthias Gehre   [PATCH] Replace 0...
55
  #include <linux/dma-mapping.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
58
59
60
61
  #include <sound/core.h>
  #include <sound/control.h>
  #include <sound/pcm.h>
  #include <sound/opl3.h>
  #include <sound/mpu401.h>
  #include <sound/initval.h>
0b5939726   Takashi Iwai   [ALSA] Add dB inf...
62
  #include <sound/tlv.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  
  #include <asm/io.h>
  
  MODULE_AUTHOR("Jaromir Koutek <miri@punknet.cz>");
  MODULE_DESCRIPTION("ESS Solo-1");
  MODULE_LICENSE("GPL");
  MODULE_SUPPORTED_DEVICE("{{ESS,ES1938},"
                  "{ESS,ES1946},"
                  "{ESS,ES1969},"
  		"{TerraTec,128i PCI}}");
  
  #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  #define SUPPORT_JOYSTICK 1
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
  static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
  static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
a67ff6a54   Rusty Russell   ALSA: module_para...
79
  static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  
  module_param_array(index, int, NULL, 0444);
  MODULE_PARM_DESC(index, "Index value for ESS Solo-1 soundcard.");
  module_param_array(id, charp, NULL, 0444);
  MODULE_PARM_DESC(id, "ID string for ESS Solo-1 soundcard.");
  module_param_array(enable, bool, NULL, 0444);
  MODULE_PARM_DESC(enable, "Enable ESS Solo-1 soundcard.");
  
  #define SLIO_REG(chip, x) ((chip)->io_port + ESSIO_REG_##x)
  
  #define SLDM_REG(chip, x) ((chip)->ddma_port + ESSDM_REG_##x)
  
  #define SLSB_REG(chip, x) ((chip)->sb_port + ESSSB_REG_##x)
  
  #define SL_PCI_LEGACYCONTROL		0x40
  #define SL_PCI_CONFIG			0x50
  #define SL_PCI_DDMACONTROL		0x60
  
  #define ESSIO_REG_AUDIO2DMAADDR		0
  #define ESSIO_REG_AUDIO2DMACOUNT	4
  #define ESSIO_REG_AUDIO2MODE		6
  #define ESSIO_REG_IRQCONTROL		7
  
  #define ESSDM_REG_DMAADDR		0x00
  #define ESSDM_REG_DMACOUNT		0x04
  #define ESSDM_REG_DMACOMMAND		0x08
  #define ESSDM_REG_DMASTATUS		0x08
  #define ESSDM_REG_DMAMODE		0x0b
  #define ESSDM_REG_DMACLEAR		0x0d
  #define ESSDM_REG_DMAMASK		0x0f
  
  #define ESSSB_REG_FMLOWADDR		0x00
  #define ESSSB_REG_FMHIGHADDR		0x02
  #define ESSSB_REG_MIXERADDR		0x04
  #define ESSSB_REG_MIXERDATA		0x05
  
  #define ESSSB_IREG_AUDIO1		0x14
  #define ESSSB_IREG_MICMIX		0x1a
  #define ESSSB_IREG_RECSRC		0x1c
  #define ESSSB_IREG_MASTER		0x32
  #define ESSSB_IREG_FM			0x36
  #define ESSSB_IREG_AUXACD		0x38
  #define ESSSB_IREG_AUXB			0x3a
  #define ESSSB_IREG_PCSPEAKER		0x3c
  #define ESSSB_IREG_LINE			0x3e
  #define ESSSB_IREG_SPATCONTROL		0x50
  #define ESSSB_IREG_SPATLEVEL		0x52
  #define ESSSB_IREG_MASTER_LEFT		0x60
  #define ESSSB_IREG_MASTER_RIGHT		0x62
  #define ESSSB_IREG_MPU401CONTROL	0x64
  #define ESSSB_IREG_MICMIXRECORD		0x68
  #define ESSSB_IREG_AUDIO2RECORD		0x69
  #define ESSSB_IREG_AUXACDRECORD		0x6a
  #define ESSSB_IREG_FMRECORD		0x6b
  #define ESSSB_IREG_AUXBRECORD		0x6c
  #define ESSSB_IREG_MONO			0x6d
  #define ESSSB_IREG_LINERECORD		0x6e
  #define ESSSB_IREG_MONORECORD		0x6f
  #define ESSSB_IREG_AUDIO2SAMPLE		0x70
  #define ESSSB_IREG_AUDIO2MODE		0x71
  #define ESSSB_IREG_AUDIO2FILTER		0x72
  #define ESSSB_IREG_AUDIO2TCOUNTL	0x74
  #define ESSSB_IREG_AUDIO2TCOUNTH	0x76
  #define ESSSB_IREG_AUDIO2CONTROL1	0x78
  #define ESSSB_IREG_AUDIO2CONTROL2	0x7a
  #define ESSSB_IREG_AUDIO2		0x7c
  
  #define ESSSB_REG_RESET			0x06
  
  #define ESSSB_REG_READDATA		0x0a
  #define ESSSB_REG_WRITEDATA		0x0c
  #define ESSSB_REG_READSTATUS		0x0c
  
  #define ESSSB_REG_STATUS		0x0e
  
  #define ESS_CMD_EXTSAMPLERATE		0xa1
  #define ESS_CMD_FILTERDIV		0xa2
  #define ESS_CMD_DMACNTRELOADL		0xa4
  #define ESS_CMD_DMACNTRELOADH		0xa5
  #define ESS_CMD_ANALOGCONTROL		0xa8
  #define ESS_CMD_IRQCONTROL		0xb1
  #define ESS_CMD_DRQCONTROL		0xb2
  #define ESS_CMD_RECLEVEL		0xb4
  #define ESS_CMD_SETFORMAT		0xb6
  #define ESS_CMD_SETFORMAT2		0xb7
  #define ESS_CMD_DMACONTROL		0xb8
  #define ESS_CMD_DMATYPE			0xb9
  #define ESS_CMD_OFFSETLEFT		0xba	
  #define ESS_CMD_OFFSETRIGHT		0xbb
  #define ESS_CMD_READREG			0xc0
  #define ESS_CMD_ENABLEEXT		0xc6
  #define ESS_CMD_PAUSEDMA		0xd0
  #define ESS_CMD_ENABLEAUDIO1		0xd1
  #define ESS_CMD_STOPAUDIO1		0xd3
  #define ESS_CMD_AUDIO1STATUS		0xd8
  #define ESS_CMD_CONTDMA			0xd4
  #define ESS_CMD_TESTIRQ			0xf2
  
  #define ESS_RECSRC_MIC		0
  #define ESS_RECSRC_AUXACD	2
  #define ESS_RECSRC_AUXB		5
  #define ESS_RECSRC_LINE		6
  #define ESS_RECSRC_NONE		7
  
  #define DAC1 0x01
  #define ADC1 0x02
  #define DAC2 0x04
  
  /*
  
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191
  #define SAVED_REG_SIZE	32 /* max. number of registers to save */
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
192
  struct es1938 {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
193
194
195
196
197
198
199
200
201
202
203
  	int irq;
  
  	unsigned long io_port;
  	unsigned long sb_port;
  	unsigned long vc_port;
  	unsigned long mpu_port;
  	unsigned long game_port;
  	unsigned long ddma_port;
  
  	unsigned char irqmask;
  	unsigned char revision;
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
204
205
206
207
  	struct snd_kcontrol *hw_volume;
  	struct snd_kcontrol *hw_switch;
  	struct snd_kcontrol *master_volume;
  	struct snd_kcontrol *master_switch;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
  
  	struct pci_dev *pci;
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
210
211
212
213
214
215
  	struct snd_card *card;
  	struct snd_pcm *pcm;
  	struct snd_pcm_substream *capture_substream;
  	struct snd_pcm_substream *playback1_substream;
  	struct snd_pcm_substream *playback2_substream;
  	struct snd_rawmidi *rmidi;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216
217
218
219
220
221
222
  
  	unsigned int dma1_size;
  	unsigned int dma2_size;
  	unsigned int dma1_start;
  	unsigned int dma2_start;
  	unsigned int dma1_shift;
  	unsigned int dma2_shift;
19e2e3c30   Hermann Lauer   [ALSA] es1938 - i...
223
  	unsigned int last_capture_dmaaddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
224
225
226
227
  	unsigned int active;
  
  	spinlock_t reg_lock;
  	spinlock_t mixer_lock;
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
228
          struct snd_info_entry *proc_entry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
230
231
232
233
234
235
236
  
  #ifdef SUPPORT_JOYSTICK
  	struct gameport *gameport;
  #endif
  #ifdef CONFIG_PM
  	unsigned char saved_regs[SAVED_REG_SIZE];
  #endif
  };
7d12e780e   David Howells   IRQ: Maintain reg...
237
  static irqreturn_t snd_es1938_interrupt(int irq, void *dev_id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238

cebe41d4b   Alexey Dobriyan   sound: use DEFINE...
239
  static DEFINE_PCI_DEVICE_TABLE(snd_es1938_ids) = {
28d27aae9   Joe Perches   sound: Use PCI_VD...
240
  	{ PCI_VDEVICE(ESS, 0x1969), 0, },   /* Solo-1 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
242
243
244
245
246
247
248
249
250
251
252
253
  	{ 0, }
  };
  
  MODULE_DEVICE_TABLE(pci, snd_es1938_ids);
  
  #define RESET_LOOP_TIMEOUT	0x10000
  #define WRITE_LOOP_TIMEOUT	0x10000
  #define GET_LOOP_TIMEOUT	0x01000
  
  #undef REG_DEBUG
  /* -----------------------------------------------------------------
   * Write to a mixer register
   * -----------------------------------------------------------------*/
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
254
  static void snd_es1938_mixer_write(struct es1938 *chip, unsigned char reg, unsigned char val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
256
257
258
259
260
261
  {
  	unsigned long flags;
  	spin_lock_irqsave(&chip->mixer_lock, flags);
  	outb(reg, SLSB_REG(chip, MIXERADDR));
  	outb(val, SLSB_REG(chip, MIXERDATA));
  	spin_unlock_irqrestore(&chip->mixer_lock, flags);
  #ifdef REG_DEBUG
99b359ba1   Takashi Iwai   [ALSA] Add missin...
262
263
  	snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x
  ", reg, val);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
265
266
267
268
269
  #endif
  }
  
  /* -----------------------------------------------------------------
   * Read from a mixer register
   * -----------------------------------------------------------------*/
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
270
  static int snd_es1938_mixer_read(struct es1938 *chip, unsigned char reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
271
272
273
274
275
276
277
278
  {
  	int data;
  	unsigned long flags;
  	spin_lock_irqsave(&chip->mixer_lock, flags);
  	outb(reg, SLSB_REG(chip, MIXERADDR));
  	data = inb(SLSB_REG(chip, MIXERDATA));
  	spin_unlock_irqrestore(&chip->mixer_lock, flags);
  #ifdef REG_DEBUG
99b359ba1   Takashi Iwai   [ALSA] Add missin...
279
280
  	snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x
  ", reg, data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
282
283
284
285
286
287
  #endif
  	return data;
  }
  
  /* -----------------------------------------------------------------
   * Write to some bits of a mixer register (return old value)
   * -----------------------------------------------------------------*/
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
288
289
  static int snd_es1938_mixer_bits(struct es1938 *chip, unsigned char reg,
  				 unsigned char mask, unsigned char val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
290
291
292
293
294
295
296
297
298
299
300
  {
  	unsigned long flags;
  	unsigned char old, new, oval;
  	spin_lock_irqsave(&chip->mixer_lock, flags);
  	outb(reg, SLSB_REG(chip, MIXERADDR));
  	old = inb(SLSB_REG(chip, MIXERDATA));
  	oval = old & mask;
  	if (val != oval) {
  		new = (old & ~mask) | (val & mask);
  		outb(new, SLSB_REG(chip, MIXERDATA));
  #ifdef REG_DEBUG
99b359ba1   Takashi Iwai   [ALSA] Add missin...
301
302
303
  		snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x
  ",
  			   reg, old, new);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
304
305
306
307
308
309
310
311
312
  #endif
  	}
  	spin_unlock_irqrestore(&chip->mixer_lock, flags);
  	return oval;
  }
  
  /* -----------------------------------------------------------------
   * Write command to Controller Registers
   * -----------------------------------------------------------------*/
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
313
  static void snd_es1938_write_cmd(struct es1938 *chip, unsigned char cmd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314
315
316
317
318
319
320
321
322
  {
  	int i;
  	unsigned char v;
  	for (i = 0; i < WRITE_LOOP_TIMEOUT; i++) {
  		if (!(v = inb(SLSB_REG(chip, READSTATUS)) & 0x80)) {
  			outb(cmd, SLSB_REG(chip, WRITEDATA));
  			return;
  		}
  	}
99b359ba1   Takashi Iwai   [ALSA] Add missin...
323
324
  	printk(KERN_ERR "snd_es1938_write_cmd timeout (0x02%x/0x02%x)
  ", cmd, v);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
325
326
327
328
329
  }
  
  /* -----------------------------------------------------------------
   * Read the Read Data Buffer
   * -----------------------------------------------------------------*/
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
330
  static int snd_es1938_get_byte(struct es1938 *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
331
332
333
334
335
336
  {
  	int i;
  	unsigned char v;
  	for (i = GET_LOOP_TIMEOUT; i; i--)
  		if ((v = inb(SLSB_REG(chip, STATUS))) & 0x80)
  			return inb(SLSB_REG(chip, READDATA));
99b359ba1   Takashi Iwai   [ALSA] Add missin...
337
338
  	snd_printk(KERN_ERR "get_byte timeout: status 0x02%x
  ", v);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
339
340
341
342
343
344
  	return -ENODEV;
  }
  
  /* -----------------------------------------------------------------
   * Write value cmd register
   * -----------------------------------------------------------------*/
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
345
  static void snd_es1938_write(struct es1938 *chip, unsigned char reg, unsigned char val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346
347
348
349
350
351
352
  {
  	unsigned long flags;
  	spin_lock_irqsave(&chip->reg_lock, flags);
  	snd_es1938_write_cmd(chip, reg);
  	snd_es1938_write_cmd(chip, val);
  	spin_unlock_irqrestore(&chip->reg_lock, flags);
  #ifdef REG_DEBUG
99b359ba1   Takashi Iwai   [ALSA] Add missin...
353
354
  	snd_printk(KERN_DEBUG "Reg %02x set to %02x
  ", reg, val);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
355
356
357
358
359
360
  #endif
  }
  
  /* -----------------------------------------------------------------
   * Read data from cmd register and return it
   * -----------------------------------------------------------------*/
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
361
  static unsigned char snd_es1938_read(struct es1938 *chip, unsigned char reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
362
363
364
365
366
367
368
369
370
  {
  	unsigned char val;
  	unsigned long flags;
  	spin_lock_irqsave(&chip->reg_lock, flags);
  	snd_es1938_write_cmd(chip, ESS_CMD_READREG);
  	snd_es1938_write_cmd(chip, reg);
  	val = snd_es1938_get_byte(chip);
  	spin_unlock_irqrestore(&chip->reg_lock, flags);
  #ifdef REG_DEBUG
99b359ba1   Takashi Iwai   [ALSA] Add missin...
371
372
  	snd_printk(KERN_DEBUG "Reg %02x now is %02x
  ", reg, val);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
373
374
375
376
377
378
379
  #endif
  	return val;
  }
  
  /* -----------------------------------------------------------------
   * Write data to cmd register and return old value
   * -----------------------------------------------------------------*/
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
380
381
  static int snd_es1938_bits(struct es1938 *chip, unsigned char reg, unsigned char mask,
  			   unsigned char val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
382
383
384
385
386
387
388
389
390
391
392
393
394
  {
  	unsigned long flags;
  	unsigned char old, new, oval;
  	spin_lock_irqsave(&chip->reg_lock, flags);
  	snd_es1938_write_cmd(chip, ESS_CMD_READREG);
  	snd_es1938_write_cmd(chip, reg);
  	old = snd_es1938_get_byte(chip);
  	oval = old & mask;
  	if (val != oval) {
  		snd_es1938_write_cmd(chip, reg);
  		new = (old & ~mask) | (val & mask);
  		snd_es1938_write_cmd(chip, new);
  #ifdef REG_DEBUG
99b359ba1   Takashi Iwai   [ALSA] Add missin...
395
396
397
  		snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x
  ",
  			   reg, old, new);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
398
399
400
401
402
403
404
405
406
  #endif
  	}
  	spin_unlock_irqrestore(&chip->reg_lock, flags);
  	return oval;
  }
  
  /* --------------------------------------------------------------------
   * Reset the chip
   * --------------------------------------------------------------------*/
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
407
  static void snd_es1938_reset(struct es1938 *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
408
409
410
411
412
413
414
415
416
417
418
419
  {
  	int i;
  
  	outb(3, SLSB_REG(chip, RESET));
  	inb(SLSB_REG(chip, RESET));
  	outb(0, SLSB_REG(chip, RESET));
  	for (i = 0; i < RESET_LOOP_TIMEOUT; i++) {
  		if (inb(SLSB_REG(chip, STATUS)) & 0x80) {
  			if (inb(SLSB_REG(chip, READDATA)) == 0xaa)
  				goto __next;
  		}
  	}
99b359ba1   Takashi Iwai   [ALSA] Add missin...
420
421
  	snd_printk(KERN_ERR "ESS Solo-1 reset failed
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
  
       __next:
  	snd_es1938_write_cmd(chip, ESS_CMD_ENABLEEXT);
  
  	/* Demand transfer DMA: 4 bytes per DMA request */
  	snd_es1938_write(chip, ESS_CMD_DMATYPE, 2);
  
  	/* Change behaviour of register A1
  	   4x oversampling
  	   2nd channel DAC asynchronous */                                                      
  	snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2MODE, 0x32);
  	/* enable/select DMA channel and IRQ channel */
  	snd_es1938_bits(chip, ESS_CMD_IRQCONTROL, 0xf0, 0x50);
  	snd_es1938_bits(chip, ESS_CMD_DRQCONTROL, 0xf0, 0x50);
  	snd_es1938_write_cmd(chip, ESS_CMD_ENABLEAUDIO1);
  	/* Set spatializer parameters to recommended values */
  	snd_es1938_mixer_write(chip, 0x54, 0x8f);
  	snd_es1938_mixer_write(chip, 0x56, 0x95);
  	snd_es1938_mixer_write(chip, 0x58, 0x94);
  	snd_es1938_mixer_write(chip, 0x5a, 0x80);
  }
  
  /* --------------------------------------------------------------------
   * Reset the FIFOs
   * --------------------------------------------------------------------*/
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
447
  static void snd_es1938_reset_fifo(struct es1938 *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
448
449
450
451
  {
  	outb(2, SLSB_REG(chip, RESET));
  	outb(0, SLSB_REG(chip, RESET));
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
452
  static struct snd_ratnum clocks[2] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
453
454
455
456
457
458
459
460
461
462
463
464
465
  	{
  		.num = 793800,
  		.den_min = 1,
  		.den_max = 128,
  		.den_step = 1,
  	},
  	{
  		.num = 768000,
  		.den_min = 1,
  		.den_max = 128,
  		.den_step = 1,
  	}
  };
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
466
  static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
467
468
469
  	.nrats = 2,
  	.rats = clocks,
  };
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
470
471
  static void snd_es1938_rate_set(struct es1938 *chip, 
  				struct snd_pcm_substream *substream,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
472
473
474
  				int mode)
  {
  	unsigned int bits, div0;
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
475
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
  	if (runtime->rate_num == clocks[0].num)
  		bits = 128 - runtime->rate_den;
  	else
  		bits = 256 - runtime->rate_den;
  
  	/* set filter register */
  	div0 = 256 - 7160000*20/(8*82*runtime->rate);
  		
  	if (mode == DAC2) {
  		snd_es1938_mixer_write(chip, 0x70, bits);
  		snd_es1938_mixer_write(chip, 0x72, div0);
  	} else {
  		snd_es1938_write(chip, 0xA1, bits);
  		snd_es1938_write(chip, 0xA2, div0);
  	}
  }
  
  /* --------------------------------------------------------------------
   * Configure Solo1 builtin DMA Controller
   * --------------------------------------------------------------------*/
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
496
  static void snd_es1938_playback1_setdma(struct es1938 *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497
498
499
500
501
502
  {
  	outb(0x00, SLIO_REG(chip, AUDIO2MODE));
  	outl(chip->dma2_start, SLIO_REG(chip, AUDIO2DMAADDR));
  	outw(0, SLIO_REG(chip, AUDIO2DMACOUNT));
  	outw(chip->dma2_size, SLIO_REG(chip, AUDIO2DMACOUNT));
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
503
  static void snd_es1938_playback2_setdma(struct es1938 *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
504
505
506
507
508
509
510
511
512
513
514
515
516
  {
  	/* Enable DMA controller */
  	outb(0xc4, SLDM_REG(chip, DMACOMMAND));
  	/* 1. Master reset */
  	outb(0, SLDM_REG(chip, DMACLEAR));
  	/* 2. Mask DMA */
  	outb(1, SLDM_REG(chip, DMAMASK));
  	outb(0x18, SLDM_REG(chip, DMAMODE));
  	outl(chip->dma1_start, SLDM_REG(chip, DMAADDR));
  	outw(chip->dma1_size - 1, SLDM_REG(chip, DMACOUNT));
  	/* 3. Unmask DMA */
  	outb(0, SLDM_REG(chip, DMAMASK));
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
517
  static void snd_es1938_capture_setdma(struct es1938 *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
518
519
520
521
522
523
524
525
526
  {
  	/* Enable DMA controller */
  	outb(0xc4, SLDM_REG(chip, DMACOMMAND));
  	/* 1. Master reset */
  	outb(0, SLDM_REG(chip, DMACLEAR));
  	/* 2. Mask DMA */
  	outb(1, SLDM_REG(chip, DMAMASK));
  	outb(0x14, SLDM_REG(chip, DMAMODE));
  	outl(chip->dma1_start, SLDM_REG(chip, DMAADDR));
19e2e3c30   Hermann Lauer   [ALSA] es1938 - i...
527
  	chip->last_capture_dmaaddr = chip->dma1_start;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
528
529
530
531
532
533
534
535
536
  	outw(chip->dma1_size - 1, SLDM_REG(chip, DMACOUNT));
  	/* 3. Unmask DMA */
  	outb(0, SLDM_REG(chip, DMAMASK));
  }
  
  /* ----------------------------------------------------------------------
   *
   *                           *** PCM part ***
   */
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
537
  static int snd_es1938_capture_trigger(struct snd_pcm_substream *substream,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
538
539
  				      int cmd)
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
540
  	struct es1938 *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
541
542
543
  	int val;
  	switch (cmd) {
  	case SNDRV_PCM_TRIGGER_START:
93b9f4263   Takashi Iwai   [ALSA] es1938 - C...
544
  	case SNDRV_PCM_TRIGGER_RESUME:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
545
546
547
548
  		val = 0x0f;
  		chip->active |= ADC1;
  		break;
  	case SNDRV_PCM_TRIGGER_STOP:
93b9f4263   Takashi Iwai   [ALSA] es1938 - C...
549
  	case SNDRV_PCM_TRIGGER_SUSPEND:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
550
551
552
553
554
555
556
557
558
  		val = 0x00;
  		chip->active &= ~ADC1;
  		break;
  	default:
  		return -EINVAL;
  	}
  	snd_es1938_write(chip, ESS_CMD_DMACONTROL, val);
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
559
  static int snd_es1938_playback1_trigger(struct snd_pcm_substream *substream,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
560
561
  					int cmd)
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
562
  	struct es1938 *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
563
564
  	switch (cmd) {
  	case SNDRV_PCM_TRIGGER_START:
93b9f4263   Takashi Iwai   [ALSA] es1938 - C...
565
  	case SNDRV_PCM_TRIGGER_RESUME:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
566
567
568
569
570
571
572
573
574
575
576
577
  		/* According to the documentation this should be:
  		   0x13 but that value may randomly swap stereo channels */
                  snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2CONTROL1, 0x92);
                  udelay(10);
  		snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2CONTROL1, 0x93);
                  /* This two stage init gives the FIFO -> DAC connection time to
                   * settle before first data from DMA flows in.  This should ensure
                   * no swapping of stereo channels.  Report a bug if otherwise :-) */
  		outb(0x0a, SLIO_REG(chip, AUDIO2MODE));
  		chip->active |= DAC2;
  		break;
  	case SNDRV_PCM_TRIGGER_STOP:
93b9f4263   Takashi Iwai   [ALSA] es1938 - C...
578
  	case SNDRV_PCM_TRIGGER_SUSPEND:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
579
580
581
582
583
584
585
586
587
  		outb(0, SLIO_REG(chip, AUDIO2MODE));
  		snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2CONTROL1, 0);
  		chip->active &= ~DAC2;
  		break;
  	default:
  		return -EINVAL;
  	}
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
588
  static int snd_es1938_playback2_trigger(struct snd_pcm_substream *substream,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
589
590
  					int cmd)
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
591
  	struct es1938 *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
592
593
594
  	int val;
  	switch (cmd) {
  	case SNDRV_PCM_TRIGGER_START:
93b9f4263   Takashi Iwai   [ALSA] es1938 - C...
595
  	case SNDRV_PCM_TRIGGER_RESUME:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
596
597
598
599
  		val = 5;
  		chip->active |= DAC1;
  		break;
  	case SNDRV_PCM_TRIGGER_STOP:
93b9f4263   Takashi Iwai   [ALSA] es1938 - C...
600
  	case SNDRV_PCM_TRIGGER_SUSPEND:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
601
602
603
604
605
606
607
608
609
  		val = 0;
  		chip->active &= ~DAC1;
  		break;
  	default:
  		return -EINVAL;
  	}
  	snd_es1938_write(chip, ESS_CMD_DMACONTROL, val);
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
610
  static int snd_es1938_playback_trigger(struct snd_pcm_substream *substream,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
  				       int cmd)
  {
  	switch (substream->number) {
  	case 0:
  		return snd_es1938_playback1_trigger(substream, cmd);
  	case 1:
  		return snd_es1938_playback2_trigger(substream, cmd);
  	}
  	snd_BUG();
  	return -EINVAL;
  }
  
  /* --------------------------------------------------------------------
   * First channel for Extended Mode Audio 1 ADC Operation
   * --------------------------------------------------------------------*/
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
626
  static int snd_es1938_capture_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
627
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
628
629
  	struct es1938 *chip = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
  	int u, is8, mono;
  	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  	unsigned int count = snd_pcm_lib_period_bytes(substream);
  
  	chip->dma1_size = size;
  	chip->dma1_start = runtime->dma_addr;
  
  	mono = (runtime->channels > 1) ? 0 : 1;
  	is8 = snd_pcm_format_width(runtime->format) == 16 ? 0 : 1;
  	u = snd_pcm_format_unsigned(runtime->format);
  
  	chip->dma1_shift = 2 - mono - is8;
  
  	snd_es1938_reset_fifo(chip);
  	
  	/* program type */
  	snd_es1938_bits(chip, ESS_CMD_ANALOGCONTROL, 0x03, (mono ? 2 : 1));
  
  	/* set clock and counters */
          snd_es1938_rate_set(chip, substream, ADC1);
  
  	count = 0x10000 - count;
  	snd_es1938_write(chip, ESS_CMD_DMACNTRELOADL, count & 0xff);
  	snd_es1938_write(chip, ESS_CMD_DMACNTRELOADH, count >> 8);
  
  	/* initialize and configure ADC */
  	snd_es1938_write(chip, ESS_CMD_SETFORMAT2, u ? 0x51 : 0x71);
  	snd_es1938_write(chip, ESS_CMD_SETFORMAT2, 0x90 | 
  		       (u ? 0x00 : 0x20) | 
  		       (is8 ? 0x00 : 0x04) | 
  		       (mono ? 0x40 : 0x08));
  
  	//	snd_es1938_reset_fifo(chip);	
  
  	/* 11. configure system interrupt controller and DMA controller */
  	snd_es1938_capture_setdma(chip);
  
  	return 0;
  }
  
  
  /* ------------------------------------------------------------------------------
   * Second Audio channel DAC Operation
   * ------------------------------------------------------------------------------*/
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
674
  static int snd_es1938_playback1_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
675
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
676
677
  	struct es1938 *chip = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
  	int u, is8, mono;
  	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  	unsigned int count = snd_pcm_lib_period_bytes(substream);
  
  	chip->dma2_size = size;
  	chip->dma2_start = runtime->dma_addr;
  
  	mono = (runtime->channels > 1) ? 0 : 1;
  	is8 = snd_pcm_format_width(runtime->format) == 16 ? 0 : 1;
  	u = snd_pcm_format_unsigned(runtime->format);
  
  	chip->dma2_shift = 2 - mono - is8;
  
          snd_es1938_reset_fifo(chip);
  
  	/* set clock and counters */
          snd_es1938_rate_set(chip, substream, DAC2);
  
  	count >>= 1;
  	count = 0x10000 - count;
  	snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2TCOUNTL, count & 0xff);
  	snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2TCOUNTH, count >> 8);
  
  	/* initialize and configure Audio 2 DAC */
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
702
703
  	snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2CONTROL2, 0x40 | (u ? 0 : 4) |
  			       (mono ? 0 : 2) | (is8 ? 0 : 1));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
704
705
706
707
708
709
  
  	/* program DMA */
  	snd_es1938_playback1_setdma(chip);
  	
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
710
  static int snd_es1938_playback2_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
711
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
712
713
  	struct es1938 *chip = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
  	int u, is8, mono;
  	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  	unsigned int count = snd_pcm_lib_period_bytes(substream);
  
  	chip->dma1_size = size;
  	chip->dma1_start = runtime->dma_addr;
  
  	mono = (runtime->channels > 1) ? 0 : 1;
  	is8 = snd_pcm_format_width(runtime->format) == 16 ? 0 : 1;
  	u = snd_pcm_format_unsigned(runtime->format);
  
  	chip->dma1_shift = 2 - mono - is8;
  
  	count = 0x10000 - count;
   
  	/* reset */
  	snd_es1938_reset_fifo(chip);
  	
  	snd_es1938_bits(chip, ESS_CMD_ANALOGCONTROL, 0x03, (mono ? 2 : 1));
  
  	/* set clock and counters */
          snd_es1938_rate_set(chip, substream, DAC1);
  	snd_es1938_write(chip, ESS_CMD_DMACNTRELOADL, count & 0xff);
  	snd_es1938_write(chip, ESS_CMD_DMACNTRELOADH, count >> 8);
  
  	/* initialized and configure DAC */
          snd_es1938_write(chip, ESS_CMD_SETFORMAT, u ? 0x80 : 0x00);
          snd_es1938_write(chip, ESS_CMD_SETFORMAT, u ? 0x51 : 0x71);
          snd_es1938_write(chip, ESS_CMD_SETFORMAT2, 
  			 0x90 | (mono ? 0x40 : 0x08) |
  			 (is8 ? 0x00 : 0x04) | (u ? 0x00 : 0x20));
  
  	/* program DMA */
  	snd_es1938_playback2_setdma(chip);
  	
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
751
  static int snd_es1938_playback_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
752
753
754
755
756
757
758
759
760
761
  {
  	switch (substream->number) {
  	case 0:
  		return snd_es1938_playback1_prepare(substream);
  	case 1:
  		return snd_es1938_playback2_prepare(substream);
  	}
  	snd_BUG();
  	return -EINVAL;
  }
19e2e3c30   Hermann Lauer   [ALSA] es1938 - i...
762
763
764
765
766
767
768
769
  /* during the incrementing of dma counters the DMA register reads sometimes
     returns garbage. To ensure a valid hw pointer, the following checks which
     should be very unlikely to fail are used:
     - is the current DMA address in the valid DMA range ?
     - is the sum of DMA address and DMA counter pointing to the last DMA byte ?
     One can argue this could differ by one byte depending on which register is
     updated first, so the implementation below allows for that.
  */
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
770
  static snd_pcm_uframes_t snd_es1938_capture_pointer(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
771
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
772
  	struct es1938 *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
773
  	size_t ptr;
19e2e3c30   Hermann Lauer   [ALSA] es1938 - i...
774
  #if 0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
775
  	size_t old, new;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
776
777
778
779
780
781
  	/* This stuff is *needed*, don't ask why - AB */
  	old = inw(SLDM_REG(chip, DMACOUNT));
  	while ((new = inw(SLDM_REG(chip, DMACOUNT))) != old)
  		old = new;
  	ptr = chip->dma1_size - 1 - new;
  #else
19e2e3c30   Hermann Lauer   [ALSA] es1938 - i...
782
783
784
785
786
787
788
789
790
791
792
793
794
795
  	size_t count;
  	unsigned int diff;
  
  	ptr = inl(SLDM_REG(chip, DMAADDR));
  	count = inw(SLDM_REG(chip, DMACOUNT));
  	diff = chip->dma1_start + chip->dma1_size - ptr - count;
  
  	if (diff > 3 || ptr < chip->dma1_start
  	      || ptr >= chip->dma1_start+chip->dma1_size)
  	  ptr = chip->last_capture_dmaaddr;            /* bad, use last saved */
  	else
  	  chip->last_capture_dmaaddr = ptr;            /* good, remember it */
  
  	ptr -= chip->dma1_start;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
796
797
798
  #endif
  	return ptr >> chip->dma1_shift;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
799
  static snd_pcm_uframes_t snd_es1938_playback1_pointer(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
800
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
801
  	struct es1938 *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
802
803
804
805
806
807
808
809
  	size_t ptr;
  #if 1
  	ptr = chip->dma2_size - inw(SLIO_REG(chip, AUDIO2DMACOUNT));
  #else
  	ptr = inl(SLIO_REG(chip, AUDIO2DMAADDR)) - chip->dma2_start;
  #endif
  	return ptr >> chip->dma2_shift;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
810
  static snd_pcm_uframes_t snd_es1938_playback2_pointer(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
811
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
812
  	struct es1938 *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
813
814
815
816
817
818
819
820
821
822
823
824
825
  	size_t ptr;
  	size_t old, new;
  #if 1
  	/* This stuff is *needed*, don't ask why - AB */
  	old = inw(SLDM_REG(chip, DMACOUNT));
  	while ((new = inw(SLDM_REG(chip, DMACOUNT))) != old)
  		old = new;
  	ptr = chip->dma1_size - 1 - new;
  #else
  	ptr = inl(SLDM_REG(chip, DMAADDR)) - chip->dma1_start;
  #endif
  	return ptr >> chip->dma1_shift;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
826
  static snd_pcm_uframes_t snd_es1938_playback_pointer(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
827
828
829
830
831
832
833
834
835
836
  {
  	switch (substream->number) {
  	case 0:
  		return snd_es1938_playback1_pointer(substream);
  	case 1:
  		return snd_es1938_playback2_pointer(substream);
  	}
  	snd_BUG();
  	return -EINVAL;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
837
  static int snd_es1938_capture_copy(struct snd_pcm_substream *substream,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
838
839
840
841
842
  				   int channel,
  				   snd_pcm_uframes_t pos,
  				   void __user *dst,
  				   snd_pcm_uframes_t count)
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
843
844
  	struct snd_pcm_runtime *runtime = substream->runtime;
  	struct es1938 *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
845
846
  	pos <<= chip->dma1_shift;
  	count <<= chip->dma1_shift;
da3cec35d   Takashi Iwai   ALSA: Kill snd_as...
847
848
  	if (snd_BUG_ON(pos + count > chip->dma1_size))
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
  	if (pos + count < chip->dma1_size) {
  		if (copy_to_user(dst, runtime->dma_area + pos + 1, count))
  			return -EFAULT;
  	} else {
  		if (copy_to_user(dst, runtime->dma_area + pos + 1, count - 1))
  			return -EFAULT;
  		if (put_user(runtime->dma_area[0], ((unsigned char __user *)dst) + count - 1))
  			return -EFAULT;
  	}
  	return 0;
  }
  
  /*
   * buffer management
   */
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
864
865
  static int snd_es1938_pcm_hw_params(struct snd_pcm_substream *substream,
  				    struct snd_pcm_hw_params *hw_params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
866
867
868
869
870
871
872
873
  
  {
  	int err;
  
  	if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  		return err;
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
874
  static int snd_es1938_pcm_hw_free(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
875
876
877
878
879
880
881
  {
  	return snd_pcm_lib_free_pages(substream);
  }
  
  /* ----------------------------------------------------------------------
   * Audio1 Capture (ADC)
   * ----------------------------------------------------------------------*/
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
882
  static struct snd_pcm_hardware snd_es1938_capture =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
883
884
885
  {
  	.info =			(SNDRV_PCM_INFO_INTERLEAVED |
  				SNDRV_PCM_INFO_BLOCK_TRANSFER),
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
886
887
  	.formats =		(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  				 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
  	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  	.rate_min =		6000,
  	.rate_max =		48000,
  	.channels_min =		1,
  	.channels_max =		2,
          .buffer_bytes_max =	0x8000,       /* DMA controller screws on higher values */
  	.period_bytes_min =	64,
  	.period_bytes_max =	0x8000,
  	.periods_min =		1,
  	.periods_max =		1024,
  	.fifo_size =		256,
  };
  
  /* -----------------------------------------------------------------------
   * Audio2 Playback (DAC)
   * -----------------------------------------------------------------------*/
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
904
  static struct snd_pcm_hardware snd_es1938_playback =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
905
906
907
908
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
  				 SNDRV_PCM_INFO_MMAP_VALID),
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
909
910
  	.formats =		(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  				 SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
911
912
913
914
915
916
917
918
919
920
921
922
  	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  	.rate_min =		6000,
  	.rate_max =		48000,
  	.channels_min =		1,
  	.channels_max =		2,
          .buffer_bytes_max =	0x8000,       /* DMA controller screws on higher values */
  	.period_bytes_min =	64,
  	.period_bytes_max =	0x8000,
  	.periods_min =		1,
  	.periods_max =		1024,
  	.fifo_size =		256,
  };
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
923
  static int snd_es1938_capture_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
924
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
925
926
  	struct es1938 *chip = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
927
928
929
930
931
932
933
934
935
936
  
  	if (chip->playback2_substream)
  		return -EAGAIN;
  	chip->capture_substream = substream;
  	runtime->hw = snd_es1938_capture;
  	snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  				      &hw_constraints_clocks);
  	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, 0xff00);
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
937
  static int snd_es1938_playback_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
938
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
939
940
  	struct es1938 *chip = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
  
  	switch (substream->number) {
  	case 0:
  		chip->playback1_substream = substream;
  		break;
  	case 1:
  		if (chip->capture_substream)
  			return -EAGAIN;
  		chip->playback2_substream = substream;
  		break;
  	default:
  		snd_BUG();
  		return -EINVAL;
  	}
  	runtime->hw = snd_es1938_playback;
  	snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  				      &hw_constraints_clocks);
  	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, 0xff00);
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
961
  static int snd_es1938_capture_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
962
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
963
  	struct es1938 *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
964
965
966
967
  
  	chip->capture_substream = NULL;
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
968
  static int snd_es1938_playback_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
969
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
970
  	struct es1938 *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
971
972
973
974
975
976
977
978
979
980
981
982
983
984
  
  	switch (substream->number) {
  	case 0:
  		chip->playback1_substream = NULL;
  		break;
  	case 1:
  		chip->playback2_substream = NULL;
  		break;
  	default:
  		snd_BUG();
  		return -EINVAL;
  	}
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
985
  static struct snd_pcm_ops snd_es1938_playback_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
986
987
988
989
990
991
992
993
994
  	.open =		snd_es1938_playback_open,
  	.close =	snd_es1938_playback_close,
  	.ioctl =	snd_pcm_lib_ioctl,
  	.hw_params =	snd_es1938_pcm_hw_params,
  	.hw_free =	snd_es1938_pcm_hw_free,
  	.prepare =	snd_es1938_playback_prepare,
  	.trigger =	snd_es1938_playback_trigger,
  	.pointer =	snd_es1938_playback_pointer,
  };
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
995
  static struct snd_pcm_ops snd_es1938_capture_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
996
997
998
999
1000
1001
1002
1003
1004
1005
  	.open =		snd_es1938_capture_open,
  	.close =	snd_es1938_capture_close,
  	.ioctl =	snd_pcm_lib_ioctl,
  	.hw_params =	snd_es1938_pcm_hw_params,
  	.hw_free =	snd_es1938_pcm_hw_free,
  	.prepare =	snd_es1938_capture_prepare,
  	.trigger =	snd_es1938_capture_trigger,
  	.pointer =	snd_es1938_capture_pointer,
  	.copy =		snd_es1938_capture_copy,
  };
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1006
  static int __devinit snd_es1938_new_pcm(struct es1938 *chip, int device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1007
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1008
  	struct snd_pcm *pcm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1009
1010
1011
1012
1013
1014
1015
1016
  	int err;
  
  	if ((err = snd_pcm_new(chip->card, "es-1938-1946", device, 2, 1, &pcm)) < 0)
  		return err;
  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1938_playback_ops);
  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1938_capture_ops);
  	
  	pcm->private_data = chip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
  	pcm->info_flags = 0;
  	strcpy(pcm->name, "ESS Solo-1");
  
  	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  					      snd_dma_pci_data(chip->pci), 64*1024, 64*1024);
  
  	chip->pcm = pcm;
  	return 0;
  }
  
  /* -------------------------------------------------------------------
   * 
   *                       *** Mixer part ***
   */
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1031
1032
  static int snd_es1938_info_mux(struct snd_kcontrol *kcontrol,
  			       struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
  {
  	static char *texts[8] = {
  		"Mic", "Mic Master", "CD", "AOUT",
  		"Mic1", "Mix", "Line", "Master"
  	};
  
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  	uinfo->count = 1;
  	uinfo->value.enumerated.items = 8;
  	if (uinfo->value.enumerated.item > 7)
  		uinfo->value.enumerated.item = 7;
  	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1047
1048
  static int snd_es1938_get_mux(struct snd_kcontrol *kcontrol,
  			      struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1049
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1050
  	struct es1938 *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1051
1052
1053
  	ucontrol->value.enumerated.item[0] = snd_es1938_mixer_read(chip, 0x1c) & 0x07;
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1054
1055
  static int snd_es1938_put_mux(struct snd_kcontrol *kcontrol,
  			      struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1056
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1057
  	struct es1938 *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1058
1059
1060
1061
1062
1063
  	unsigned char val = ucontrol->value.enumerated.item[0];
  	
  	if (val > 7)
  		return -EINVAL;
  	return snd_es1938_mixer_bits(chip, 0x1c, 0x07, val) != val;
  }
a5ce88909   Takashi Iwai   [ALSA] Clean up w...
1064
  #define snd_es1938_info_spatializer_enable	snd_ctl_boolean_mono_info
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1065

e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1066
1067
  static int snd_es1938_get_spatializer_enable(struct snd_kcontrol *kcontrol,
  					     struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1068
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1069
  	struct es1938 *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1070
1071
1072
1073
  	unsigned char val = snd_es1938_mixer_read(chip, 0x50);
  	ucontrol->value.integer.value[0] = !!(val & 8);
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1074
1075
  static int snd_es1938_put_spatializer_enable(struct snd_kcontrol *kcontrol,
  					     struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1076
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1077
  	struct es1938 *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
  	unsigned char oval, nval;
  	int change;
  	nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
  	oval = snd_es1938_mixer_read(chip, 0x50) & 0x0c;
  	change = nval != oval;
  	if (change) {
  		snd_es1938_mixer_write(chip, 0x50, nval & ~0x04);
  		snd_es1938_mixer_write(chip, 0x50, nval);
  	}
  	return change;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1089
1090
  static int snd_es1938_info_hw_volume(struct snd_kcontrol *kcontrol,
  				     struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1091
1092
1093
1094
1095
1096
1097
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 2;
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = 63;
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1098
1099
  static int snd_es1938_get_hw_volume(struct snd_kcontrol *kcontrol,
  				    struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1100
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1101
  	struct es1938 *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1102
1103
1104
1105
  	ucontrol->value.integer.value[0] = snd_es1938_mixer_read(chip, 0x61) & 0x3f;
  	ucontrol->value.integer.value[1] = snd_es1938_mixer_read(chip, 0x63) & 0x3f;
  	return 0;
  }
a5ce88909   Takashi Iwai   [ALSA] Clean up w...
1106
  #define snd_es1938_info_hw_switch		snd_ctl_boolean_stereo_info
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1107

e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1108
1109
  static int snd_es1938_get_hw_switch(struct snd_kcontrol *kcontrol,
  				    struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1110
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1111
  	struct es1938 *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1112
1113
1114
1115
  	ucontrol->value.integer.value[0] = !(snd_es1938_mixer_read(chip, 0x61) & 0x40);
  	ucontrol->value.integer.value[1] = !(snd_es1938_mixer_read(chip, 0x63) & 0x40);
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1116
  static void snd_es1938_hwv_free(struct snd_kcontrol *kcontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1117
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1118
  	struct es1938 *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1119
1120
1121
1122
1123
  	chip->master_volume = NULL;
  	chip->master_switch = NULL;
  	chip->hw_volume = NULL;
  	chip->hw_switch = NULL;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1124
  static int snd_es1938_reg_bits(struct es1938 *chip, unsigned char reg,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1125
1126
1127
1128
1129
1130
1131
  			       unsigned char mask, unsigned char val)
  {
  	if (reg < 0xa0)
  		return snd_es1938_mixer_bits(chip, reg, mask, val);
  	else
  		return snd_es1938_bits(chip, reg, mask, val);
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1132
  static int snd_es1938_reg_read(struct es1938 *chip, unsigned char reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1133
1134
1135
1136
1137
1138
  {
  	if (reg < 0xa0)
  		return snd_es1938_mixer_read(chip, reg);
  	else
  		return snd_es1938_read(chip, reg);
  }
0b5939726   Takashi Iwai   [ALSA] Add dB inf...
1139
1140
1141
1142
1143
1144
1145
1146
  #define ES1938_SINGLE_TLV(xname, xindex, reg, shift, mask, invert, xtlv)    \
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
    .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,\
    .name = xname, .index = xindex, \
    .info = snd_es1938_info_single, \
    .get = snd_es1938_get_single, .put = snd_es1938_put_single, \
    .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24), \
    .tlv = { .p = xtlv } }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1147
1148
1149
1150
1151
  #define ES1938_SINGLE(xname, xindex, reg, shift, mask, invert) \
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
    .info = snd_es1938_info_single, \
    .get = snd_es1938_get_single, .put = snd_es1938_put_single, \
    .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1152
1153
  static int snd_es1938_info_single(struct snd_kcontrol *kcontrol,
  				  struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1154
1155
1156
1157
1158
1159
1160
1161
1162
  {
  	int mask = (kcontrol->private_value >> 16) & 0xff;
  
  	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 1;
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = mask;
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1163
1164
  static int snd_es1938_get_single(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1165
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1166
  	struct es1938 *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
  	int reg = kcontrol->private_value & 0xff;
  	int shift = (kcontrol->private_value >> 8) & 0xff;
  	int mask = (kcontrol->private_value >> 16) & 0xff;
  	int invert = (kcontrol->private_value >> 24) & 0xff;
  	int val;
  	
  	val = snd_es1938_reg_read(chip, reg);
  	ucontrol->value.integer.value[0] = (val >> shift) & mask;
  	if (invert)
  		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1179
1180
  static int snd_es1938_put_single(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1181
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1182
  	struct es1938 *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
  	int reg = kcontrol->private_value & 0xff;
  	int shift = (kcontrol->private_value >> 8) & 0xff;
  	int mask = (kcontrol->private_value >> 16) & 0xff;
  	int invert = (kcontrol->private_value >> 24) & 0xff;
  	unsigned char val;
  	
  	val = (ucontrol->value.integer.value[0] & mask);
  	if (invert)
  		val = mask - val;
  	mask <<= shift;
  	val <<= shift;
  	return snd_es1938_reg_bits(chip, reg, mask, val) != val;
  }
0b5939726   Takashi Iwai   [ALSA] Add dB inf...
1196
1197
1198
1199
1200
1201
1202
1203
  #define ES1938_DOUBLE_TLV(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert, xtlv) \
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
    .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,\
    .name = xname, .index = xindex, \
    .info = snd_es1938_info_double, \
    .get = snd_es1938_get_double, .put = snd_es1938_put_double, \
    .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22), \
    .tlv = { .p = xtlv } }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1204
1205
1206
1207
1208
  #define ES1938_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
    .info = snd_es1938_info_double, \
    .get = snd_es1938_get_double, .put = snd_es1938_put_double, \
    .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1209
1210
  static int snd_es1938_info_double(struct snd_kcontrol *kcontrol,
  				  struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1211
1212
1213
1214
1215
1216
1217
1218
1219
  {
  	int mask = (kcontrol->private_value >> 24) & 0xff;
  
  	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 2;
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = mask;
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1220
1221
  static int snd_es1938_get_double(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1222
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1223
  	struct es1938 *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
  	int left_reg = kcontrol->private_value & 0xff;
  	int right_reg = (kcontrol->private_value >> 8) & 0xff;
  	int shift_left = (kcontrol->private_value >> 16) & 0x07;
  	int shift_right = (kcontrol->private_value >> 19) & 0x07;
  	int mask = (kcontrol->private_value >> 24) & 0xff;
  	int invert = (kcontrol->private_value >> 22) & 1;
  	unsigned char left, right;
  	
  	left = snd_es1938_reg_read(chip, left_reg);
  	if (left_reg != right_reg)
  		right = snd_es1938_reg_read(chip, right_reg);
  	else
  		right = left;
  	ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
  	ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
  	if (invert) {
  		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  		ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  	}
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1245
1246
  static int snd_es1938_put_double(struct snd_kcontrol *kcontrol,
  				 struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1247
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1248
  	struct es1938 *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
  	int left_reg = kcontrol->private_value & 0xff;
  	int right_reg = (kcontrol->private_value >> 8) & 0xff;
  	int shift_left = (kcontrol->private_value >> 16) & 0x07;
  	int shift_right = (kcontrol->private_value >> 19) & 0x07;
  	int mask = (kcontrol->private_value >> 24) & 0xff;
  	int invert = (kcontrol->private_value >> 22) & 1;
  	int change;
  	unsigned char val1, val2, mask1, mask2;
  	
  	val1 = ucontrol->value.integer.value[0] & mask;
  	val2 = ucontrol->value.integer.value[1] & mask;
  	if (invert) {
  		val1 = mask - val1;
  		val2 = mask - val2;
  	}
  	val1 <<= shift_left;
  	val2 <<= shift_right;
  	mask1 = mask << shift_left;
  	mask2 = mask << shift_right;
  	if (left_reg != right_reg) {
  		change = 0;
  		if (snd_es1938_reg_bits(chip, left_reg, mask1, val1) != val1)
  			change = 1;
  		if (snd_es1938_reg_bits(chip, right_reg, mask2, val2) != val2)
  			change = 1;
  	} else {
  		change = (snd_es1938_reg_bits(chip, left_reg, mask1 | mask2, 
  					      val1 | val2) != (val1 | val2));
  	}
  	return change;
  }
0b5939726   Takashi Iwai   [ALSA] Add dB inf...
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
  static unsigned int db_scale_master[] = {
  	TLV_DB_RANGE_HEAD(2),
  	0, 54, TLV_DB_SCALE_ITEM(-3600, 50, 1),
  	54, 63, TLV_DB_SCALE_ITEM(-900, 100, 0),
  };
  
  static unsigned int db_scale_audio1[] = {
  	TLV_DB_RANGE_HEAD(2),
  	0, 8, TLV_DB_SCALE_ITEM(-3300, 300, 1),
  	8, 15, TLV_DB_SCALE_ITEM(-900, 150, 0),
  };
  
  static unsigned int db_scale_audio2[] = {
  	TLV_DB_RANGE_HEAD(2),
  	0, 8, TLV_DB_SCALE_ITEM(-3450, 300, 1),
  	8, 15, TLV_DB_SCALE_ITEM(-1050, 150, 0),
  };
  
  static unsigned int db_scale_mic[] = {
  	TLV_DB_RANGE_HEAD(2),
  	0, 8, TLV_DB_SCALE_ITEM(-2400, 300, 1),
  	8, 15, TLV_DB_SCALE_ITEM(0, 150, 0),
  };
  
  static unsigned int db_scale_line[] = {
  	TLV_DB_RANGE_HEAD(2),
  	0, 8, TLV_DB_SCALE_ITEM(-3150, 300, 1),
  	8, 15, TLV_DB_SCALE_ITEM(-750, 150, 0),
  };
0cb29ea0d   Takashi Iwai   [ALSA] Add even m...
1309
  static const DECLARE_TLV_DB_SCALE(db_scale_capture, 0, 150, 0);
0b5939726   Takashi Iwai   [ALSA] Add dB inf...
1310

e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1311
  static struct snd_kcontrol_new snd_es1938_controls[] = {
0b5939726   Takashi Iwai   [ALSA] Add dB inf...
1312
1313
  ES1938_DOUBLE_TLV("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0,
  		  db_scale_master),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
  ES1938_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
  {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.name = "Hardware Master Playback Volume",
  	.access = SNDRV_CTL_ELEM_ACCESS_READ,
  	.info = snd_es1938_info_hw_volume,
  	.get = snd_es1938_get_hw_volume,
  },
  {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
dbf91dd47   Clemens Ladisch   [ALSA] ES1938: re...
1324
  	.access = (SNDRV_CTL_ELEM_ACCESS_READ |
0b5939726   Takashi Iwai   [ALSA] Add dB inf...
1325
  		   SNDRV_CTL_ELEM_ACCESS_TLV_READ),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1326
  	.name = "Hardware Master Playback Switch",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1327
1328
  	.info = snd_es1938_info_hw_switch,
  	.get = snd_es1938_get_hw_switch,
0b5939726   Takashi Iwai   [ALSA] Add dB inf...
1329
  	.tlv = { .p = db_scale_master },
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1330
1331
  },
  ES1938_SINGLE("Hardware Volume Split", 0, 0x64, 7, 1, 0),
0b5939726   Takashi Iwai   [ALSA] Add dB inf...
1332
1333
  ES1938_DOUBLE_TLV("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0,
  		  db_scale_line),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1334
  ES1938_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
0b5939726   Takashi Iwai   [ALSA] Add dB inf...
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
  ES1938_DOUBLE_TLV("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0,
  		  db_scale_mic),
  ES1938_DOUBLE_TLV("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0,
  		  db_scale_line),
  ES1938_DOUBLE_TLV("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0,
  		  db_scale_mic),
  ES1938_DOUBLE_TLV("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0,
  		  db_scale_line),
  ES1938_DOUBLE_TLV("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0,
  		  db_scale_capture),
d355c82a0   Jaroslav Kysela   ALSA: rename "PC ...
1345
  ES1938_SINGLE("Beep Volume", 0, 0x3c, 0, 7, 0),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1346
1347
1348
1349
1350
1351
1352
1353
1354
  ES1938_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
  ES1938_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
  {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.name = "Capture Source",
  	.info = snd_es1938_info_mux,
  	.get = snd_es1938_get_mux,
  	.put = snd_es1938_put_mux,
  },
0b5939726   Takashi Iwai   [ALSA] Add dB inf...
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
  ES1938_DOUBLE_TLV("Mono Input Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0,
  		  db_scale_line),
  ES1938_DOUBLE_TLV("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0,
  		  db_scale_audio2),
  ES1938_DOUBLE_TLV("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0,
  		  db_scale_mic),
  ES1938_DOUBLE_TLV("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0,
  		  db_scale_line),
  ES1938_DOUBLE_TLV("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0,
  		  db_scale_mic),
  ES1938_DOUBLE_TLV("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0,
  		  db_scale_line),
  ES1938_DOUBLE_TLV("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0,
  		  db_scale_line),
  ES1938_DOUBLE_TLV("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0,
  		  db_scale_line),
  ES1938_DOUBLE_TLV("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0,
  		  db_scale_audio2),
  ES1938_DOUBLE_TLV("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0,
  		  db_scale_audio1),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
  ES1938_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
  {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.name = "3D Control - Switch",
  	.info = snd_es1938_info_spatializer_enable,
  	.get = snd_es1938_get_spatializer_enable,
  	.put = snd_es1938_put_spatializer_enable,
  },
  ES1938_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0)
  };
  
  
  /* ---------------------------------------------------------------------------- */
  /* ---------------------------------------------------------------------------- */
  
  /*
   * initialize the chip - used by resume callback, too
   */
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1393
  static void snd_es1938_chip_init(struct es1938 *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
  {
  	/* reset chip */
  	snd_es1938_reset(chip);
  
  	/* configure native mode */
  
  	/* enable bus master */
  	pci_set_master(chip->pci);
  
  	/* disable legacy audio */
  	pci_write_config_word(chip->pci, SL_PCI_LEGACYCONTROL, 0x805f);
  
  	/* set DDMA base */
  	pci_write_config_word(chip->pci, SL_PCI_DDMACONTROL, chip->ddma_port | 1);
  
  	/* set DMA/IRQ policy */
  	pci_write_config_dword(chip->pci, SL_PCI_CONFIG, 0);
  
  	/* enable Audio 1, Audio 2, MPU401 IRQ and HW volume IRQ*/
  	outb(0xf0, SLIO_REG(chip, IRQCONTROL));
  
  	/* reset DMA */
  	outb(0, SLDM_REG(chip, DMACLEAR));
  }
  
  #ifdef CONFIG_PM
  /*
   * PM support
   */
  
  static unsigned char saved_regs[SAVED_REG_SIZE+1] = {
  	0x14, 0x1a, 0x1c, 0x3a, 0x3c, 0x3e, 0x36, 0x38,
  	0x50, 0x52, 0x60, 0x61, 0x62, 0x63, 0x64, 0x68,
  	0x69, 0x6a, 0x6b, 0x6d, 0x6e, 0x6f, 0x7c, 0x7d,
  	0xa8, 0xb4,
  };
b34a580ec   Takashi Iwai   [ALSA] es1938 - F...
1430
  static int es1938_suspend(struct pci_dev *pci, pm_message_t state)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1431
  {
b34a580ec   Takashi Iwai   [ALSA] es1938 - F...
1432
1433
  	struct snd_card *card = pci_get_drvdata(pci);
  	struct es1938 *chip = card->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1434
  	unsigned char *s, *d;
b34a580ec   Takashi Iwai   [ALSA] es1938 - F...
1435
  	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1436
1437
1438
1439
1440
1441
1442
  	snd_pcm_suspend_all(chip->pcm);
  
  	/* save mixer-related registers */
  	for (s = saved_regs, d = chip->saved_regs; *s; s++, d++)
  		*d = snd_es1938_reg_read(chip, *s);
  
  	outb(0x00, SLIO_REG(chip, IRQCONTROL)); /* disable irqs */
30b35399c   Takashi Iwai   [ALSA] Various fi...
1443
  	if (chip->irq >= 0) {
b34a580ec   Takashi Iwai   [ALSA] es1938 - F...
1444
  		free_irq(chip->irq, chip);
30b35399c   Takashi Iwai   [ALSA] Various fi...
1445
1446
  		chip->irq = -1;
  	}
b34a580ec   Takashi Iwai   [ALSA] es1938 - F...
1447
1448
  	pci_disable_device(pci);
  	pci_save_state(pci);
30b35399c   Takashi Iwai   [ALSA] Various fi...
1449
  	pci_set_power_state(pci, pci_choose_state(pci, state));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1450
1451
  	return 0;
  }
b34a580ec   Takashi Iwai   [ALSA] es1938 - F...
1452
  static int es1938_resume(struct pci_dev *pci)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1453
  {
b34a580ec   Takashi Iwai   [ALSA] es1938 - F...
1454
1455
  	struct snd_card *card = pci_get_drvdata(pci);
  	struct es1938 *chip = card->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1456
  	unsigned char *s, *d;
30b35399c   Takashi Iwai   [ALSA] Various fi...
1457
  	pci_set_power_state(pci, PCI_D0);
b34a580ec   Takashi Iwai   [ALSA] es1938 - F...
1458
  	pci_restore_state(pci);
30b35399c   Takashi Iwai   [ALSA] Various fi...
1459
1460
1461
1462
1463
1464
1465
1466
1467
  	if (pci_enable_device(pci) < 0) {
  		printk(KERN_ERR "es1938: pci_enable_device failed, "
  		       "disabling device
  ");
  		snd_card_disconnect(card);
  		return -EIO;
  	}
  
  	if (request_irq(pci->irq, snd_es1938_interrupt,
934c2b6d0   Takashi Iwai   ALSA: use KBUILD_...
1468
  			IRQF_SHARED, KBUILD_MODNAME, chip)) {
30b35399c   Takashi Iwai   [ALSA] Various fi...
1469
1470
1471
1472
1473
1474
  		printk(KERN_ERR "es1938: unable to grab IRQ %d, "
  		       "disabling device
  ", pci->irq);
  		snd_card_disconnect(card);
  		return -EIO;
  	}
b34a580ec   Takashi Iwai   [ALSA] es1938 - F...
1475
  	chip->irq = pci->irq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1476
1477
1478
1479
1480
1481
1482
1483
1484
  	snd_es1938_chip_init(chip);
  
  	/* restore mixer-related registers */
  	for (s = saved_regs, d = chip->saved_regs; *s; s++, d++) {
  		if (*s < 0xa0)
  			snd_es1938_mixer_write(chip, *s, *d);
  		else
  			snd_es1938_write(chip, *s, *d);
  	}
b34a580ec   Takashi Iwai   [ALSA] es1938 - F...
1485
  	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1486
1487
1488
1489
1490
  	return 0;
  }
  #endif /* CONFIG_PM */
  
  #ifdef SUPPORT_JOYSTICK
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1491
  static int __devinit snd_es1938_create_gameport(struct es1938 *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
  {
  	struct gameport *gp;
  
  	chip->gameport = gp = gameport_allocate_port();
  	if (!gp) {
  		printk(KERN_ERR "es1938: cannot allocate memory for gameport
  ");
  		return -ENOMEM;
  	}
  
  	gameport_set_name(gp, "ES1938");
  	gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  	gameport_set_dev_parent(gp, &chip->pci->dev);
  	gp->io = chip->game_port;
  
  	gameport_register_port(gp);
  
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1511
  static void snd_es1938_free_gameport(struct es1938 *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1512
1513
1514
1515
1516
1517
1518
  {
  	if (chip->gameport) {
  		gameport_unregister_port(chip->gameport);
  		chip->gameport = NULL;
  	}
  }
  #else
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1519
1520
  static inline int snd_es1938_create_gameport(struct es1938 *chip) { return -ENOSYS; }
  static inline void snd_es1938_free_gameport(struct es1938 *chip) { }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1521
  #endif /* SUPPORT_JOYSTICK */
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1522
  static int snd_es1938_free(struct es1938 *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1523
1524
1525
1526
1527
1528
1529
  {
  	/* disable irqs */
  	outb(0x00, SLIO_REG(chip, IRQCONTROL));
  	if (chip->rmidi)
  		snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0);
  
  	snd_es1938_free_gameport(chip);
f000fd809   Jeff Garzik   [ALSA] Fix synchr...
1530
  	if (chip->irq >= 0)
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1531
  		free_irq(chip->irq, chip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1532
1533
1534
1535
1536
  	pci_release_regions(chip->pci);
  	pci_disable_device(chip->pci);
  	kfree(chip);
  	return 0;
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1537
  static int snd_es1938_dev_free(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1538
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1539
  	struct es1938 *chip = device->device_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1540
1541
  	return snd_es1938_free(chip);
  }
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1542
  static int __devinit snd_es1938_create(struct snd_card *card,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1543
  				    struct pci_dev * pci,
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1544
  				    struct es1938 ** rchip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1545
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1546
  	struct es1938 *chip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1547
  	int err;
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1548
  	static struct snd_device_ops ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1549
1550
1551
1552
1553
1554
1555
1556
1557
  		.dev_free =	snd_es1938_dev_free,
  	};
  
  	*rchip = NULL;
  
  	/* enable PCI device */
  	if ((err = pci_enable_device(pci)) < 0)
  		return err;
          /* check, if we can restrict PCI DMA transfers to 24 bits */
2f4f27d42   Yang Hongyang   dma-mapping: repl...
1558
1559
  	if (pci_set_dma_mask(pci, DMA_BIT_MASK(24)) < 0 ||
  	    pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(24)) < 0) {
99b359ba1   Takashi Iwai   [ALSA] Add missin...
1560
1561
  		snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1562
1563
1564
  		pci_disable_device(pci);
                  return -ENXIO;
          }
e560d8d83   Takashi Iwai   [ALSA] Replace wi...
1565
  	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1566
1567
1568
1569
1570
1571
1572
1573
  	if (chip == NULL) {
  		pci_disable_device(pci);
  		return -ENOMEM;
  	}
  	spin_lock_init(&chip->reg_lock);
  	spin_lock_init(&chip->mixer_lock);
  	chip->card = card;
  	chip->pci = pci;
30b35399c   Takashi Iwai   [ALSA] Various fi...
1574
  	chip->irq = -1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
  	if ((err = pci_request_regions(pci, "ESS Solo-1")) < 0) {
  		kfree(chip);
  		pci_disable_device(pci);
  		return err;
  	}
  	chip->io_port = pci_resource_start(pci, 0);
  	chip->sb_port = pci_resource_start(pci, 1);
  	chip->vc_port = pci_resource_start(pci, 2);
  	chip->mpu_port = pci_resource_start(pci, 3);
  	chip->game_port = pci_resource_start(pci, 4);
437a5a460   Takashi Iwai   [ALSA] Remove IRQ...
1585
  	if (request_irq(pci->irq, snd_es1938_interrupt, IRQF_SHARED,
934c2b6d0   Takashi Iwai   ALSA: use KBUILD_...
1586
  			KBUILD_MODNAME, chip)) {
99b359ba1   Takashi Iwai   [ALSA] Add missin...
1587
1588
  		snd_printk(KERN_ERR "unable to grab IRQ %d
  ", pci->irq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1589
1590
1591
1592
1593
  		snd_es1938_free(chip);
  		return -EBUSY;
  	}
  	chip->irq = pci->irq;
  #ifdef ES1938_DDEBUG
99b359ba1   Takashi Iwai   [ALSA] Add missin...
1594
1595
  	snd_printk(KERN_DEBUG "create: io: 0x%lx, sb: 0x%lx, vc: 0x%lx, mpu: 0x%lx, game: 0x%lx
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1596
1597
1598
1599
1600
1601
  		   chip->io_port, chip->sb_port, chip->vc_port, chip->mpu_port, chip->game_port);
  #endif
  
  	chip->ddma_port = chip->vc_port + 0x00;		/* fix from Thomas Sailer */
  
  	snd_es1938_chip_init(chip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
  	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  		snd_es1938_free(chip);
  		return err;
  	}
  
  	snd_card_set_dev(card, &pci->dev);
  
  	*rchip = chip;
  	return 0;
  }
  
  /* --------------------------------------------------------------------
   * Interrupt handler
   * -------------------------------------------------------------------- */
7d12e780e   David Howells   IRQ: Maintain reg...
1616
  static irqreturn_t snd_es1938_interrupt(int irq, void *dev_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1617
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1618
  	struct es1938 *chip = dev_id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1619
1620
1621
1622
1623
  	unsigned char status, audiostatus;
  	int handled = 0;
  
  	status = inb(SLIO_REG(chip, IRQCONTROL));
  #if 0
ee419653a   Takashi Iwai   ALSA: Fix missing...
1624
1625
  	printk(KERN_DEBUG "Es1938debug - interrupt status: =0x%x
  ", status);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1626
1627
1628
1629
1630
  #endif
  	
  	/* AUDIO 1 */
  	if (status & 0x10) {
  #if 0
ee419653a   Takashi Iwai   ALSA: Fix missing...
1631
1632
1633
1634
1635
1636
                  printk(KERN_DEBUG
  		       "Es1938debug - AUDIO channel 1 interrupt
  ");
  		printk(KERN_DEBUG
  		       "Es1938debug - AUDIO channel 1 DMAC DMA count: %u
  ",
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1637
  		       inw(SLDM_REG(chip, DMACOUNT)));
ee419653a   Takashi Iwai   ALSA: Fix missing...
1638
1639
1640
  		printk(KERN_DEBUG
  		       "Es1938debug - AUDIO channel 1 DMAC DMA base: %u
  ",
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1641
  		       inl(SLDM_REG(chip, DMAADDR)));
ee419653a   Takashi Iwai   ALSA: Fix missing...
1642
1643
1644
  		printk(KERN_DEBUG
  		       "Es1938debug - AUDIO channel 1 DMAC DMA status: 0x%x
  ",
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1645
  		       inl(SLDM_REG(chip, DMASTATUS)));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
  #endif
  		/* clear irq */
  		handled = 1;
  		audiostatus = inb(SLSB_REG(chip, STATUS));
  		if (chip->active & ADC1)
  			snd_pcm_period_elapsed(chip->capture_substream);
  		else if (chip->active & DAC1)
  			snd_pcm_period_elapsed(chip->playback2_substream);
  	}
  	
  	/* AUDIO 2 */
  	if (status & 0x20) {
  #if 0
ee419653a   Takashi Iwai   ALSA: Fix missing...
1659
1660
1661
1662
1663
1664
                  printk(KERN_DEBUG
  		       "Es1938debug - AUDIO channel 2 interrupt
  ");
  		printk(KERN_DEBUG
  		       "Es1938debug - AUDIO channel 2 DMAC DMA count: %u
  ",
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1665
  		       inw(SLIO_REG(chip, AUDIO2DMACOUNT)));
ee419653a   Takashi Iwai   ALSA: Fix missing...
1666
1667
1668
  		printk(KERN_DEBUG
  		       "Es1938debug - AUDIO channel 2 DMAC DMA base: %u
  ",
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1669
  		       inl(SLIO_REG(chip, AUDIO2DMAADDR)));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
  
  #endif
  		/* clear irq */
  		handled = 1;
  		snd_es1938_mixer_bits(chip, ESSSB_IREG_AUDIO2CONTROL2, 0x80, 0);
  		if (chip->active & DAC2)
  			snd_pcm_period_elapsed(chip->playback1_substream);
  	}
  
  	/* Hardware volume */
  	if (status & 0x40) {
  		int split = snd_es1938_mixer_read(chip, 0x64) & 0x80;
  		handled = 1;
  		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_switch->id);
  		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_volume->id);
  		if (!split) {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1686
1687
1688
1689
  			snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  				       &chip->master_switch->id);
  			snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  				       &chip->master_volume->id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
  		}
  		/* ack interrupt */
  		snd_es1938_mixer_write(chip, 0x66, 0x00);
  	}
  
  	/* MPU401 */
  	if (status & 0x80) {
  		// the following line is evil! It switches off MIDI interrupt handling after the first interrupt received.
  		// replacing the last 0 by 0x40 works for ESS-Solo1, but just doing nothing works as well!
  		// andreas@flying-snail.de
  		// snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0); /* ack? */
  		if (chip->rmidi) {
  			handled = 1;
7d12e780e   David Howells   IRQ: Maintain reg...
1703
  			snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1704
1705
1706
1707
1708
1709
  		}
  	}
  	return IRQ_RETVAL(handled);
  }
  
  #define ES1938_DMA_SIZE 64
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1710
  static int __devinit snd_es1938_mixer(struct es1938 *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1711
  {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1712
  	struct snd_card *card;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1713
1714
1715
1716
1717
1718
1719
1720
  	unsigned int idx;
  	int err;
  
  	card = chip->card;
  
  	strcpy(card->mixername, "ESS Solo-1");
  
  	for (idx = 0; idx < ARRAY_SIZE(snd_es1938_controls); idx++) {
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1721
  		struct snd_kcontrol *kctl;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
  		kctl = snd_ctl_new1(&snd_es1938_controls[idx], chip);
  		switch (idx) {
  			case 0:
  				chip->master_volume = kctl;
  				kctl->private_free = snd_es1938_hwv_free;
  				break;
  			case 1:
  				chip->master_switch = kctl;
  				kctl->private_free = snd_es1938_hwv_free;
  				break;
  			case 2:
  				chip->hw_volume = kctl;
  				kctl->private_free = snd_es1938_hwv_free;
  				break;
  			case 3:
  				chip->hw_switch = kctl;
  				kctl->private_free = snd_es1938_hwv_free;
  				break;
  			}
  		if ((err = snd_ctl_add(card, kctl)) < 0)
  			return err;
  	}
  	return 0;
  }
         
  
  static int __devinit snd_es1938_probe(struct pci_dev *pci,
  				      const struct pci_device_id *pci_id)
  {
  	static int dev;
e571f5943   Takashi Iwai   [ALSA] Remove xxx...
1752
1753
1754
  	struct snd_card *card;
  	struct es1938 *chip;
  	struct snd_opl3 *opl3;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1755
1756
1757
1758
1759
1760
1761
1762
  	int idx, err;
  
  	if (dev >= SNDRV_CARDS)
  		return -ENODEV;
  	if (!enable[dev]) {
  		dev++;
  		return -ENOENT;
  	}
e58de7baf   Takashi Iwai   ALSA: Convert to ...
1763
1764
1765
  	err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  	if (err < 0)
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
  	for (idx = 0; idx < 5; idx++) {
  		if (pci_resource_start(pci, idx) == 0 ||
  		    !(pci_resource_flags(pci, idx) & IORESOURCE_IO)) {
  		    	snd_card_free(card);
  		    	return -ENODEV;
  		}
  	}
  	if ((err = snd_es1938_create(card, pci, &chip)) < 0) {
  		snd_card_free(card);
  		return err;
  	}
b34a580ec   Takashi Iwai   [ALSA] es1938 - F...
1777
  	card->private_data = chip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
  
  	strcpy(card->driver, "ES1938");
  	strcpy(card->shortname, "ESS ES1938 (Solo-1)");
  	sprintf(card->longname, "%s rev %i, irq %i",
  		card->shortname,
  		chip->revision,
  		chip->irq);
  
  	if ((err = snd_es1938_new_pcm(chip, 0)) < 0) {
  		snd_card_free(card);
  		return err;
  	}
  	if ((err = snd_es1938_mixer(chip)) < 0) {
  		snd_card_free(card);
  		return err;
  	}
  	if (snd_opl3_create(card,
  			    SLSB_REG(chip, FMLOWADDR),
  			    SLSB_REG(chip, FMHIGHADDR),
  			    OPL3_HW_OPL3, 1, &opl3) < 0) {
  		printk(KERN_ERR "es1938: OPL3 not detected at 0x%lx
  ",
  			   SLSB_REG(chip, FMLOWADDR));
  	} else {
  	        if ((err = snd_opl3_timer_new(opl3, 0, 1)) < 0) {
  	                snd_card_free(card);
  	                return err;
  		}
  	        if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  	                snd_card_free(card);
  	                return err;
  		}
  	}
  	if (snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
dba8b4699   Clemens Ladisch   ALSA: mpu401: cle...
1812
1813
1814
  				chip->mpu_port,
  				MPU401_INFO_INTEGRATED | MPU401_INFO_IRQ_HOOK,
  				-1, &chip->rmidi) < 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
  		printk(KERN_ERR "es1938: unable to initialize MPU-401
  ");
  	} else {
  		// this line is vital for MIDI interrupt handling on ess-solo1
  		// andreas@flying-snail.de
  		snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0x40);
  	}
  
  	snd_es1938_create_gameport(chip);
  
  	if ((err = snd_card_register(card)) < 0) {
  		snd_card_free(card);
  		return err;
  	}
  
  	pci_set_drvdata(pci, card);
  	dev++;
  	return 0;
  }
  
  static void __devexit snd_es1938_remove(struct pci_dev *pci)
  {
  	snd_card_free(pci_get_drvdata(pci));
  	pci_set_drvdata(pci, NULL);
  }
  
  static struct pci_driver driver = {
3733e424c   Takashi Iwai   ALSA: Use KBUILD_...
1842
  	.name = KBUILD_MODNAME,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1843
1844
1845
  	.id_table = snd_es1938_ids,
  	.probe = snd_es1938_probe,
  	.remove = __devexit_p(snd_es1938_remove),
b34a580ec   Takashi Iwai   [ALSA] es1938 - F...
1846
1847
1848
1849
  #ifdef CONFIG_PM
  	.suspend = es1938_suspend,
  	.resume = es1938_resume,
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1850
1851
1852
1853
  };
  
  static int __init alsa_card_es1938_init(void)
  {
01d25d460   Takashi Iwai   [ALSA] Replace pc...
1854
  	return pci_register_driver(&driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1855
1856
1857
1858
1859
1860
1861
1862
1863
  }
  
  static void __exit alsa_card_es1938_exit(void)
  {
  	pci_unregister_driver(&driver);
  }
  
  module_init(alsa_card_es1938_init)
  module_exit(alsa_card_es1938_exit)