azt3328.c 62.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  azt3328.c - driver for Aztech AZF3328 based soundcards (e.g. PCI168).
3
 *  Copyright (C) 2002, 2005, 2006, 2007 by Andreas Mohr <andi AT lisas.de>
L
Linus Torvalds 已提交
4 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
 *
 *  Framework borrowed from Bart Hartgers's als4000.c.
 *  Driver developed on PCI168 AP(W) version (PCI rev. 10, subsystem ID 1801),
 *  found in a Fujitsu-Siemens PC ("Cordant", aluminum case).
 *  Other versions are:
 *  PCI168 A(W), sub ID 1800
 *  PCI168 A/AP, sub ID 8000
 *  Please give me feedback in case you try my driver with one of these!!
 *
 * GPL LICENSE
 *  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
 *  Since Aztech does not provide any chipset documentation,
 *  even on repeated request to various addresses,
 *  and the answer that was finally given was negative
 *  (and I was stupid enough to manage to get hold of a PCI168 soundcard
 *  in the first place >:-P}),
 *  I was forced to base this driver on reverse engineering
 *  (3 weeks' worth of evenings filled with driver work).
36
 *  (and no, I did NOT go the easy way: to pick up a SB PCI128 for 9 Euros)
L
Linus Torvalds 已提交
37 38 39 40 41
 *
 *  The AZF3328 chip (note: AZF3328, *not* AZT3328, that's just the driver name
 *  for compatibility reasons) has the following features:
 *
 *  - builtin AC97 conformant codec (SNR over 80dB)
42 43 44 45 46 47 48 49 50
 *    Note that "conformant" != "compliant"!! this chip's mixer register layout
 *    *differs* from the standard AC97 layout:
 *    they chose to not implement the headphone register (which is not a
 *    problem since it's merely optional), yet when doing this, they committed
 *    the grave sin of letting other registers follow immediately instead of
 *    keeping a headphone dummy register, thereby shifting the mixer register
 *    addresses illegally. So far unfortunately it looks like the very flexible
 *    ALSA AC97 support is still not enough to easily compensate for such a
 *    grave layout violation despite all tweaks and quirks mechanisms it offers.
L
Linus Torvalds 已提交
51 52 53 54
 *  - builtin genuine OPL3
 *  - full duplex 16bit playback/record at independent sampling rate
 *  - MPU401 (+ legacy address support) FIXME: how to enable legacy addr??
 *  - game port (legacy address support)
55 56 57
 *  - builtin 3D enhancement (said to be YAMAHA Ymersion)
 *  - builtin DirectInput support, helps reduce CPU overhead (interrupt-driven
 *    features supported)
L
Linus Torvalds 已提交
58
 *  - built-in General DirectX timer having a 20 bits counter
59
 *    with 1us resolution (see below!)
L
Linus Torvalds 已提交
60 61 62 63 64 65 66 67
 *  - I2S serial port for external DAC
 *  - supports 33MHz PCI spec 2.1, PCI power management 1.0, compliant with ACPI
 *  - supports hardware volume control
 *  - single chip low cost solution (128 pin QFP)
 *  - supports programmable Sub-vendor and Sub-system ID
 *    required for Microsoft's logo compliance (FIXME: where?)
 *  - PCI168 AP(W) card: power amplifier with 4 Watts/channel at 4 Ohms
 *
68 69 70 71 72 73 74 75 76
 *  Note that this driver now is actually *better* than the Windows driver,
 *  since it additionally supports the card's 1MHz DirectX timer - just try
 *  the following snd-seq module parameters etc.:
 *  - options snd-seq seq_default_timer_class=2 seq_default_timer_sclass=0
 *    seq_default_timer_card=0 seq_client_load=1 seq_default_timer_device=0
 *    seq_default_timer_subdevice=0 seq_default_timer_resolution=1000000
 *  - "timidity -iAv -B2,8 -Os -EFreverb=0"
 *  - "pmidi -p 128:0 jazz.mid"
 *
L
Linus Torvalds 已提交
77 78 79 80 81 82
 *  Certain PCI versions of this card are susceptible to DMA traffic underruns
 *  in some systems (resulting in sound crackling/clicking/popping),
 *  probably because they don't have a DMA FIFO buffer or so.
 *  Overview (PCI ID/PCI subID/PCI rev.):
 *  - no DMA crackling on SiS735: 0x50DC/0x1801/16
 *  - unknown performance: 0x50DC/0x1801/10
83 84
 *    (well, it's not bad on an Athlon 1800 with now very optimized IRQ handler)
 *
L
Linus Torvalds 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98
 *  Crackling happens with VIA chipsets or, in my case, an SiS735, which is
 *  supposed to be very fast and supposed to get rid of crackling much
 *  better than a VIA, yet ironically I still get crackling, like many other
 *  people with the same chipset.
 *  Possible remedies:
 *  - plug card into a different PCI slot, preferrably one that isn't shared
 *    too much (this helps a lot, but not completely!)
 *  - get rid of PCI VGA card, use AGP instead
 *  - upgrade or downgrade BIOS
 *  - fiddle with PCI latency settings (setpci -v -s BUSID latency_timer=XX)
 *    Not too helpful.
 *  - Disable ACPI/power management/"Auto Detect RAM/PCI Clk" in BIOS
 * 
 * BUGS
99
 *  - full-duplex might *still* be problematic, not fully tested recently
100 101 102 103
 *  - (non-bug) "Bass/Treble or 3D settings don't work" - they do get evaluated
 *    if you set PCM output switch to "pre 3D" instead of "post 3D".
 *    If this can't be set, then get a mixer application that Isn't Stupid (tm)
 *    (e.g. kmix, gamix) - unfortunately several are!!
L
Linus Torvalds 已提交
104 105 106
 * 
 * TODO
 *  - test MPU401 MIDI playback etc.
107 108 109 110 111
 *  - add some power micro-management (disable various units of the card
 *    as long as they're unused). However this requires I/O ports which I
 *    haven't figured out yet and which thus might not even exist...
 *    The standard suspend/resume functionality could probably make use of
 *    some improvement, too...
L
Linus Torvalds 已提交
112
 *  - figure out what all unknown port bits are responsible for
113 114 115
 *  - figure out some cleverly evil scheme to possibly make ALSA AC97 code
 *    fully accept our quite incompatible ""AC97"" mixer and thus save some
 *    code (but I'm not too optimistic that doing this is possible at all)
L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123 124 125
 */

#include <sound/driver.h>
#include <asm/io.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/gameport.h>
#include <linux/moduleparam.h>
126
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
127 128 129 130 131 132 133 134 135
#include <sound/core.h>
#include <sound/control.h>
#include <sound/pcm.h>
#include <sound/rawmidi.h>
#include <sound/mpu401.h>
#include <sound/opl3.h>
#include <sound/initval.h>
#include "azt3328.h"

136
MODULE_AUTHOR("Andreas Mohr <andi AT lisas.de>");
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149
MODULE_DESCRIPTION("Aztech AZF3328 (PCI168)");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("{{Aztech,AZF3328}}");

#if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
#define SUPPORT_JOYSTICK 1
#endif

#define DEBUG_MISC	0
#define DEBUG_CALLS	0
#define DEBUG_MIXER	0
#define DEBUG_PLAY_REC	0
#define DEBUG_IO	0
150
#define DEBUG_TIMER	0
L
Linus Torvalds 已提交
151 152 153 154 155 156 157 158 159 160
#define MIXER_TESTING	0

#if DEBUG_MISC
#define snd_azf3328_dbgmisc(format, args...) printk(KERN_ERR format, ##args)
#else
#define snd_azf3328_dbgmisc(format, args...)
#endif		

#if DEBUG_CALLS
#define snd_azf3328_dbgcalls(format, args...) printk(format, ##args)
161 162
#define snd_azf3328_dbgcallenter() printk(KERN_ERR "--> %s\n", __FUNCTION__)
#define snd_azf3328_dbgcallleave() printk(KERN_ERR "<-- %s\n", __FUNCTION__)
L
Linus Torvalds 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
#else
#define snd_azf3328_dbgcalls(format, args...)
#define snd_azf3328_dbgcallenter()
#define snd_azf3328_dbgcallleave()
#endif		

#if DEBUG_MIXER
#define snd_azf3328_dbgmixer(format, args...) printk(format, ##args)
#else
#define snd_azf3328_dbgmixer(format, args...)
#endif		

#if DEBUG_PLAY_REC
#define snd_azf3328_dbgplay(format, args...) printk(KERN_ERR format, ##args)
#else
#define snd_azf3328_dbgplay(format, args...)
#endif		

181 182
#if DEBUG_MISC
#define snd_azf3328_dbgtimer(format, args...) printk(KERN_ERR format, ##args)
L
Linus Torvalds 已提交
183
#else
184 185 186
#define snd_azf3328_dbgtimer(format, args...)
#endif		

L
Linus Torvalds 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
module_param_array(index, int, NULL, 0444);
MODULE_PARM_DESC(index, "Index value for AZF3328 soundcard.");

static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
module_param_array(id, charp, NULL, 0444);
MODULE_PARM_DESC(id, "ID string for AZF3328 soundcard.");

static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
module_param_array(enable, bool, NULL, 0444);
MODULE_PARM_DESC(enable, "Enable AZF3328 soundcard.");

#ifdef SUPPORT_JOYSTICK
static int joystick[SNDRV_CARDS];
module_param_array(joystick, bool, NULL, 0444);
MODULE_PARM_DESC(joystick, "Enable joystick for AZF3328 soundcard.");
#endif

205 206 207
static int seqtimer_scaling = 128;
module_param(seqtimer_scaling, int, 0444);
MODULE_PARM_DESC(seqtimer_scaling, "Set 1024000Hz sequencer timer scale factor (lockup danger!). Default 128.");
L
Linus Torvalds 已提交
208

209
struct snd_azf3328 {
210
	/* often-used fields towards beginning, then grouped */
L
Linus Torvalds 已提交
211 212 213 214 215 216
	unsigned long codec_port;
	unsigned long io2_port;
	unsigned long mpu_port;
	unsigned long synth_port;
	unsigned long mixer_port;

217
	spinlock_t reg_lock;
L
Linus Torvalds 已提交
218

219
	struct snd_timer *timer;
220
	
221 222 223
	struct snd_pcm *pcm;
	struct snd_pcm_substream *playback_substream;
	struct snd_pcm_substream *capture_substream;
L
Linus Torvalds 已提交
224 225 226
	unsigned int is_playing;
	unsigned int is_recording;

227 228
	struct snd_card *card;
	struct snd_rawmidi *rmidi;
229 230 231 232

#ifdef SUPPORT_JOYSTICK
	struct gameport *gameport;
#endif
L
Linus Torvalds 已提交
233

234 235
	struct pci_dev *pci;
	int irq;
236 237 238 239 240 241 242 243 244 245

#ifdef CONFIG_PM
	/* register value containers for power management
	 * Note: not always full I/O range preserved (just like Win driver!) */
	u16 saved_regs_codec [AZF_IO_SIZE_CODEC_PM / 2];
	u16 saved_regs_io2   [AZF_IO_SIZE_IO2_PM / 2];
	u16 saved_regs_mpu   [AZF_IO_SIZE_MPU_PM / 2];
	u16 saved_regs_synth[AZF_IO_SIZE_SYNTH_PM / 2];
	u16 saved_regs_mixer[AZF_IO_SIZE_MIXER_PM / 2];
#endif
246
};
247

248
static const struct pci_device_id snd_azf3328_ids[] = {
L
Linus Torvalds 已提交
249 250 251 252 253 254 255
	{ 0x122D, 0x50DC, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },   /* PCI168/3328 */
	{ 0x122D, 0x80DA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },   /* 3328 */
	{ 0, }
};

MODULE_DEVICE_TABLE(pci, snd_azf3328_ids);

256
static inline void
257
snd_azf3328_codec_outb(const struct snd_azf3328 *chip, int reg, u8 value)
258 259 260 261 262
{
	outb(value, chip->codec_port + reg);
}

static inline u8
263
snd_azf3328_codec_inb(const struct snd_azf3328 *chip, int reg)
264 265 266 267 268
{
	return inb(chip->codec_port + reg);
}

static inline void
269
snd_azf3328_codec_outw(const struct snd_azf3328 *chip, int reg, u16 value)
270 271 272 273 274
{
	outw(value, chip->codec_port + reg);
}

static inline u16
275
snd_azf3328_codec_inw(const struct snd_azf3328 *chip, int reg)
276 277 278 279 280
{
	return inw(chip->codec_port + reg);
}

static inline void
281
snd_azf3328_codec_outl(const struct snd_azf3328 *chip, int reg, u32 value)
282 283 284 285 286
{
	outl(value, chip->codec_port + reg);
}

static inline void
287
snd_azf3328_io2_outb(const struct snd_azf3328 *chip, int reg, u8 value)
L
Linus Torvalds 已提交
288 289 290 291
{
	outb(value, chip->io2_port + reg);
}

292
static inline u8
293
snd_azf3328_io2_inb(const struct snd_azf3328 *chip, int reg)
L
Linus Torvalds 已提交
294 295 296 297
{
	return inb(chip->io2_port + reg);
}

298
static inline void
299
snd_azf3328_mixer_outw(const struct snd_azf3328 *chip, int reg, u16 value)
L
Linus Torvalds 已提交
300
{
301 302 303 304
	outw(value, chip->mixer_port + reg);
}

static inline u16
305
snd_azf3328_mixer_inw(const struct snd_azf3328 *chip, int reg)
306 307
{
	return inw(chip->mixer_port + reg);
L
Linus Torvalds 已提交
308 309
}

310
static void
311
snd_azf3328_mixer_set_mute(const struct snd_azf3328 *chip, int reg, int do_mute)
L
Linus Torvalds 已提交
312
{
313
	unsigned long portbase = chip->mixer_port + reg + 1;
L
Linus Torvalds 已提交
314 315 316 317
	unsigned char oldval;

	/* the mute bit is on the *second* (i.e. right) register of a
	 * left/right channel setting */
318
	oldval = inb(portbase);
L
Linus Torvalds 已提交
319 320 321 322
	if (do_mute)
		oldval |= 0x80;
	else
		oldval &= ~0x80;
323
	outb(oldval, portbase);
L
Linus Torvalds 已提交
324 325
}

326
static void
327
snd_azf3328_mixer_write_volume_gradually(const struct snd_azf3328 *chip, int reg, unsigned char dst_vol_left, unsigned char dst_vol_right, int chan_sel, int delay)
L
Linus Torvalds 已提交
328
{
329
	unsigned long portbase = chip->mixer_port + reg;
L
Linus Torvalds 已提交
330 331 332 333 334
	unsigned char curr_vol_left = 0, curr_vol_right = 0;
	int left_done = 0, right_done = 0;
	
	snd_azf3328_dbgcallenter();
	if (chan_sel & SET_CHAN_LEFT)
335
		curr_vol_left  = inb(portbase + 1);
L
Linus Torvalds 已提交
336 337 338
	else
		left_done = 1;
	if (chan_sel & SET_CHAN_RIGHT)
339
		curr_vol_right = inb(portbase + 0);
L
Linus Torvalds 已提交
340 341 342 343 344 345 346 347 348
	else
		right_done = 1;
	
	/* take care of muting flag (0x80) contained in left channel */
	if (curr_vol_left & 0x80)
		dst_vol_left |= 0x80;
	else
		dst_vol_left &= ~0x80;

349 350
	do {
		if (!left_done) {
L
Linus Torvalds 已提交
351 352 353 354 355 356 357
			if (curr_vol_left > dst_vol_left)
				curr_vol_left--;
			else
			if (curr_vol_left < dst_vol_left)
				curr_vol_left++;
			else
			    left_done = 1;
358
			outb(curr_vol_left, portbase + 1);
L
Linus Torvalds 已提交
359
		}
360
		if (!right_done) {
L
Linus Torvalds 已提交
361 362 363 364 365 366 367 368 369 370
			if (curr_vol_right > dst_vol_right)
				curr_vol_right--;
			else
			if (curr_vol_right < dst_vol_right)
				curr_vol_right++;
			else
			    right_done = 1;
			/* during volume change, the right channel is crackling
			 * somewhat more than the left channel, unfortunately.
			 * This seems to be a hardware issue. */
371
			outb(curr_vol_right, portbase + 0);
L
Linus Torvalds 已提交
372 373 374
		}
		if (delay)
			mdelay(delay);
375
	} while ((!left_done) || (!right_done));
L
Linus Torvalds 已提交
376 377 378 379 380 381
	snd_azf3328_dbgcallleave();
}

/*
 * general mixer element
 */
382
struct azf3328_mixer_reg {
L
Linus Torvalds 已提交
383 384 385 386 387 388
	unsigned int reg;
	unsigned int lchan_shift, rchan_shift;
	unsigned int mask;
	unsigned int invert: 1;
	unsigned int stereo: 1;
	unsigned int enum_c: 4;
389
};
L
Linus Torvalds 已提交
390 391

#define COMPOSE_MIXER_REG(reg,lchan_shift,rchan_shift,mask,invert,stereo,enum_c) \
392 393 394 395 396
 ((reg) | (lchan_shift << 8) | (rchan_shift << 12) | \
  (mask << 16) | \
  (invert << 24) | \
  (stereo << 25) | \
  (enum_c << 26))
L
Linus Torvalds 已提交
397

398
static void snd_azf3328_mixer_reg_decode(struct azf3328_mixer_reg *r, unsigned long val)
L
Linus Torvalds 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 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 447
{
	r->reg = val & 0xff;
	r->lchan_shift = (val >> 8) & 0x0f;
	r->rchan_shift = (val >> 12) & 0x0f;
	r->mask = (val >> 16) & 0xff;
	r->invert = (val >> 24) & 1;
	r->stereo = (val >> 25) & 1;
	r->enum_c = (val >> 26) & 0x0f;
}

/*
 * mixer switches/volumes
 */

#define AZF3328_MIXER_SWITCH(xname, reg, shift, invert) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  .info = snd_azf3328_info_mixer, \
  .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
  .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0x1, invert, 0, 0), \
}

#define AZF3328_MIXER_VOL_STEREO(xname, reg, mask, invert) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  .info = snd_azf3328_info_mixer, \
  .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
  .private_value = COMPOSE_MIXER_REG(reg, 8, 0, mask, invert, 1, 0), \
}

#define AZF3328_MIXER_VOL_MONO(xname, reg, mask, is_right_chan) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  .info = snd_azf3328_info_mixer, \
  .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
  .private_value = COMPOSE_MIXER_REG(reg, is_right_chan ? 0 : 8, 0, mask, 1, 0, 0), \
}

#define AZF3328_MIXER_VOL_SPECIAL(xname, reg, mask, shift, invert) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  .info = snd_azf3328_info_mixer, \
  .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
  .private_value = COMPOSE_MIXER_REG(reg, shift, 0, mask, invert, 0, 0), \
}

#define AZF3328_MIXER_ENUM(xname, reg, enum_c, shift) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  .info = snd_azf3328_info_mixer_enum, \
  .get = snd_azf3328_get_mixer_enum, .put = snd_azf3328_put_mixer_enum, \
  .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0, 0, 0, enum_c), \
}

448
static int
449 450
snd_azf3328_info_mixer(struct snd_kcontrol *kcontrol,
		       struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
451
{
452
	struct azf3328_mixer_reg reg;
L
Linus Torvalds 已提交
453 454 455

	snd_azf3328_dbgcallenter();
	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
456 457
	uinfo->type = reg.mask == 1 ?
		SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
L
Linus Torvalds 已提交
458 459 460 461 462 463 464
	uinfo->count = reg.stereo + 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = reg.mask;
	snd_azf3328_dbgcallleave();
	return 0;
}

465
static int
466 467
snd_azf3328_get_mixer(struct snd_kcontrol *kcontrol,
		      struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
468
{
469 470
	struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
	struct azf3328_mixer_reg reg;
L
Linus Torvalds 已提交
471 472 473 474 475
	unsigned int oreg, val;

	snd_azf3328_dbgcallenter();
	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);

476
	oreg = snd_azf3328_mixer_inw(chip, reg.reg);
L
Linus Torvalds 已提交
477 478 479 480 481 482 483 484 485 486
	val = (oreg >> reg.lchan_shift) & reg.mask;
	if (reg.invert)
		val = reg.mask - val;
	ucontrol->value.integer.value[0] = val;
	if (reg.stereo) {
		val = (oreg >> reg.rchan_shift) & reg.mask;
		if (reg.invert)
			val = reg.mask - val;
		ucontrol->value.integer.value[1] = val;
	}
487 488 489 490 491
	snd_azf3328_dbgmixer("get: %02x is %04x -> vol %02lx|%02lx "
			     "(shift %02d|%02d, mask %02x, inv. %d, stereo %d)\n",
		reg.reg, oreg,
		ucontrol->value.integer.value[0], ucontrol->value.integer.value[1],
		reg.lchan_shift, reg.rchan_shift, reg.mask, reg.invert, reg.stereo);
L
Linus Torvalds 已提交
492 493 494 495
	snd_azf3328_dbgcallleave();
	return 0;
}

496
static int
497 498
snd_azf3328_put_mixer(struct snd_kcontrol *kcontrol,
		      struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
499
{
500 501
	struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
	struct azf3328_mixer_reg reg;
L
Linus Torvalds 已提交
502 503 504 505
	unsigned int oreg, nreg, val;

	snd_azf3328_dbgcallenter();
	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
506
	oreg = snd_azf3328_mixer_inw(chip, reg.reg);
L
Linus Torvalds 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519
	val = ucontrol->value.integer.value[0] & reg.mask;
	if (reg.invert)
		val = reg.mask - val;
	nreg = oreg & ~(reg.mask << reg.lchan_shift);
	nreg |= (val << reg.lchan_shift);
	if (reg.stereo) {
		val = ucontrol->value.integer.value[1] & reg.mask;
		if (reg.invert)
			val = reg.mask - val;
		nreg &= ~(reg.mask << reg.rchan_shift);
		nreg |= (val << reg.rchan_shift);
	}
	if (reg.mask >= 0x07) /* it's a volume control, so better take care */
520 521 522 523 524
		snd_azf3328_mixer_write_volume_gradually(
			chip, reg.reg, nreg >> 8, nreg & 0xff,
			/* just set both channels, doesn't matter */
			SET_CHAN_LEFT|SET_CHAN_RIGHT,
			0);
L
Linus Torvalds 已提交
525
	else
526
        	snd_azf3328_mixer_outw(chip, reg.reg, nreg);
L
Linus Torvalds 已提交
527

528 529 530 531 532
	snd_azf3328_dbgmixer("put: %02x to %02lx|%02lx, "
			     "oreg %04x; shift %02d|%02d -> nreg %04x; after: %04x\n",
		reg.reg, ucontrol->value.integer.value[0], ucontrol->value.integer.value[1],
		oreg, reg.lchan_shift, reg.rchan_shift,
		nreg, snd_azf3328_mixer_inw(chip, reg.reg));
L
Linus Torvalds 已提交
533 534 535 536
	snd_azf3328_dbgcallleave();
	return (nreg != oreg);
}

537
static int
538 539
snd_azf3328_info_mixer_enum(struct snd_kcontrol *kcontrol,
			    struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
540
{
541
	static const char * const texts1[] = {
542
		"Mic1", "Mic2"
543 544
	};
	static const char * const texts2[] = {
545
		"Mix", "Mic"
546 547 548 549
	};
	static const char * const texts3[] = {
                "Mic", "CD", "Video", "Aux",
		"Line", "Mix", "Mix Mono", "Phone"
L
Linus Torvalds 已提交
550
        };
551 552 553
	static const char * const texts4[] = {
		"pre 3D", "post 3D"
        };
554
	struct azf3328_mixer_reg reg;
L
Linus Torvalds 已提交
555 556 557 558 559 560 561

	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
        uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
        uinfo->count = (reg.reg == IDX_MIXER_REC_SELECT) ? 2 : 1;
        uinfo->value.enumerated.items = reg.enum_c;
        if (uinfo->value.enumerated.item > reg.enum_c - 1U)
                uinfo->value.enumerated.item = reg.enum_c - 1U;
562
	if (reg.reg == IDX_MIXER_ADVCTL2) {
563 564
		switch(reg.lchan_shift) {
		case 8: /* modem out sel */
L
Linus Torvalds 已提交
565
			strcpy(uinfo->value.enumerated.name, texts1[uinfo->value.enumerated.item]);
566 567
			break;
		case 9: /* mono sel source */
L
Linus Torvalds 已提交
568
			strcpy(uinfo->value.enumerated.name, texts2[uinfo->value.enumerated.item]);
569 570 571 572 573
			break;
		case 15: /* PCM Out Path */
			strcpy(uinfo->value.enumerated.name, texts4[uinfo->value.enumerated.item]);
			break;
		}
574
	} else
L
Linus Torvalds 已提交
575 576 577 578 579
        	strcpy(uinfo->value.enumerated.name, texts3[uinfo->value.enumerated.item]
);
        return 0;
}

580
static int
581 582
snd_azf3328_get_mixer_enum(struct snd_kcontrol *kcontrol,
			   struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
583
{
584 585
        struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
	struct azf3328_mixer_reg reg;
L
Linus Torvalds 已提交
586 587 588
        unsigned short val;
        
	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
589
	val = snd_azf3328_mixer_inw(chip, reg.reg);
590
	if (reg.reg == IDX_MIXER_REC_SELECT) {
L
Linus Torvalds 已提交
591 592
        	ucontrol->value.enumerated.item[0] = (val >> 8) & (reg.enum_c - 1);
        	ucontrol->value.enumerated.item[1] = (val >> 0) & (reg.enum_c - 1);
593
	} else
L
Linus Torvalds 已提交
594
        	ucontrol->value.enumerated.item[0] = (val >> reg.lchan_shift) & (reg.enum_c - 1);
595 596 597 598

	snd_azf3328_dbgmixer("get_enum: %02x is %04x -> %d|%d (shift %02d, enum_c %d)\n",
		reg.reg, val, ucontrol->value.enumerated.item[0], ucontrol->value.enumerated.item[1],
		reg.lchan_shift, reg.enum_c);
L
Linus Torvalds 已提交
599 600 601
        return 0;
}

602
static int
603 604
snd_azf3328_put_mixer_enum(struct snd_kcontrol *kcontrol,
			   struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
605
{
606 607
        struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
	struct azf3328_mixer_reg reg;
L
Linus Torvalds 已提交
608 609 610
	unsigned int oreg, nreg, val;
        
	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
611
	oreg = snd_azf3328_mixer_inw(chip, reg.reg);
L
Linus Torvalds 已提交
612
	val = oreg;
613
	if (reg.reg == IDX_MIXER_REC_SELECT) {
L
Linus Torvalds 已提交
614 615 616 617 618
        	if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U ||
            	ucontrol->value.enumerated.item[1] > reg.enum_c - 1U)
                	return -EINVAL;
        	val = (ucontrol->value.enumerated.item[0] << 8) |
        	      (ucontrol->value.enumerated.item[1] << 0);
619
	} else {
L
Linus Torvalds 已提交
620 621 622 623 624
        	if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U)
                	return -EINVAL;
		val &= ~((reg.enum_c - 1) << reg.lchan_shift);
        	val |= (ucontrol->value.enumerated.item[0] << reg.lchan_shift);
	}
625
	snd_azf3328_mixer_outw(chip, reg.reg, val);
L
Linus Torvalds 已提交
626 627 628 629 630 631
	nreg = val;

	snd_azf3328_dbgmixer("put_enum: %02x to %04x, oreg %04x\n", reg.reg, val, oreg);
	return (nreg != oreg);
}

632
static struct snd_kcontrol_new snd_azf3328_mixer_controls[] __devinitdata = {
L
Linus Torvalds 已提交
633 634 635 636
	AZF3328_MIXER_SWITCH("Master Playback Switch", IDX_MIXER_PLAY_MASTER, 15, 1),
	AZF3328_MIXER_VOL_STEREO("Master Playback Volume", IDX_MIXER_PLAY_MASTER, 0x1f, 1),
	AZF3328_MIXER_SWITCH("Wave Playback Switch", IDX_MIXER_WAVEOUT, 15, 1),
	AZF3328_MIXER_VOL_STEREO("Wave Playback Volume", IDX_MIXER_WAVEOUT, 0x1f, 1),
637
	AZF3328_MIXER_SWITCH("Wave 3D Bypass Playback Switch", IDX_MIXER_ADVCTL2, 7, 1),
L
Linus Torvalds 已提交
638 639 640 641 642 643 644 645 646 647 648 649
	AZF3328_MIXER_SWITCH("FM Playback Switch", IDX_MIXER_FMSYNTH, 15, 1),
	AZF3328_MIXER_VOL_STEREO("FM Playback Volume", IDX_MIXER_FMSYNTH, 0x1f, 1),
	AZF3328_MIXER_SWITCH("CD Playback Switch", IDX_MIXER_CDAUDIO, 15, 1),
	AZF3328_MIXER_VOL_STEREO("CD Playback Volume", IDX_MIXER_CDAUDIO, 0x1f, 1),
	AZF3328_MIXER_SWITCH("Capture Switch", IDX_MIXER_REC_VOLUME, 15, 1),
	AZF3328_MIXER_VOL_STEREO("Capture Volume", IDX_MIXER_REC_VOLUME, 0x0f, 0),
	AZF3328_MIXER_ENUM("Capture Source", IDX_MIXER_REC_SELECT, 8, 0),
	AZF3328_MIXER_SWITCH("Mic Playback Switch", IDX_MIXER_MIC, 15, 1),
	AZF3328_MIXER_VOL_MONO("Mic Playback Volume", IDX_MIXER_MIC, 0x1f, 1),
	AZF3328_MIXER_SWITCH("Mic Boost (+20dB)", IDX_MIXER_MIC, 6, 0),
	AZF3328_MIXER_SWITCH("Line Playback Switch", IDX_MIXER_LINEIN, 15, 1),
	AZF3328_MIXER_VOL_STEREO("Line Playback Volume", IDX_MIXER_LINEIN, 0x1f, 1),
650 651
	AZF3328_MIXER_SWITCH("PC Speaker Playback Switch", IDX_MIXER_PCBEEP, 15, 1),
	AZF3328_MIXER_VOL_SPECIAL("PC Speaker Playback Volume", IDX_MIXER_PCBEEP, 0x0f, 1, 1),
L
Linus Torvalds 已提交
652 653 654 655 656 657 658 659
	AZF3328_MIXER_SWITCH("Video Playback Switch", IDX_MIXER_VIDEO, 15, 1),
	AZF3328_MIXER_VOL_STEREO("Video Playback Volume", IDX_MIXER_VIDEO, 0x1f, 1),
	AZF3328_MIXER_SWITCH("Aux Playback Switch", IDX_MIXER_AUX, 15, 1),
	AZF3328_MIXER_VOL_STEREO("Aux Playback Volume", IDX_MIXER_AUX, 0x1f, 1),
	AZF3328_MIXER_SWITCH("Modem Playback Switch", IDX_MIXER_MODEMOUT, 15, 1),
	AZF3328_MIXER_VOL_MONO("Modem Playback Volume", IDX_MIXER_MODEMOUT, 0x1f, 1),
	AZF3328_MIXER_SWITCH("Modem Capture Switch", IDX_MIXER_MODEMIN, 15, 1),
	AZF3328_MIXER_VOL_MONO("Modem Capture Volume", IDX_MIXER_MODEMIN, 0x1f, 1),
660 661
	AZF3328_MIXER_ENUM("Mic Select", IDX_MIXER_ADVCTL2, 2, 8),
	AZF3328_MIXER_ENUM("Mono Output Select", IDX_MIXER_ADVCTL2, 2, 9),
662
	AZF3328_MIXER_ENUM("PCM Output Route", IDX_MIXER_ADVCTL2, 2, 15), /* PCM Out Path, place in front since it controls *both* 3D and Bass/Treble! */
L
Linus Torvalds 已提交
663 664
	AZF3328_MIXER_VOL_SPECIAL("Tone Control - Treble", IDX_MIXER_BASSTREBLE, 0x07, 1, 0),
	AZF3328_MIXER_VOL_SPECIAL("Tone Control - Bass", IDX_MIXER_BASSTREBLE, 0x07, 9, 0),
665
	AZF3328_MIXER_SWITCH("3D Control - Switch", IDX_MIXER_ADVCTL2, 13, 0),
666 667
	AZF3328_MIXER_VOL_SPECIAL("3D Control - Width", IDX_MIXER_ADVCTL1, 0x07, 1, 0), /* "3D Width" */
	AZF3328_MIXER_VOL_SPECIAL("3D Control - Depth", IDX_MIXER_ADVCTL1, 0x03, 8, 0), /* "Hifi 3D" */
L
Linus Torvalds 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
#if MIXER_TESTING
	AZF3328_MIXER_SWITCH("0", IDX_MIXER_ADVCTL2, 0, 0),
	AZF3328_MIXER_SWITCH("1", IDX_MIXER_ADVCTL2, 1, 0),
	AZF3328_MIXER_SWITCH("2", IDX_MIXER_ADVCTL2, 2, 0),
	AZF3328_MIXER_SWITCH("3", IDX_MIXER_ADVCTL2, 3, 0),
	AZF3328_MIXER_SWITCH("4", IDX_MIXER_ADVCTL2, 4, 0),
	AZF3328_MIXER_SWITCH("5", IDX_MIXER_ADVCTL2, 5, 0),
	AZF3328_MIXER_SWITCH("6", IDX_MIXER_ADVCTL2, 6, 0),
	AZF3328_MIXER_SWITCH("7", IDX_MIXER_ADVCTL2, 7, 0),
	AZF3328_MIXER_SWITCH("8", IDX_MIXER_ADVCTL2, 8, 0),
	AZF3328_MIXER_SWITCH("9", IDX_MIXER_ADVCTL2, 9, 0),
	AZF3328_MIXER_SWITCH("10", IDX_MIXER_ADVCTL2, 10, 0),
	AZF3328_MIXER_SWITCH("11", IDX_MIXER_ADVCTL2, 11, 0),
	AZF3328_MIXER_SWITCH("12", IDX_MIXER_ADVCTL2, 12, 0),
	AZF3328_MIXER_SWITCH("13", IDX_MIXER_ADVCTL2, 13, 0),
	AZF3328_MIXER_SWITCH("14", IDX_MIXER_ADVCTL2, 14, 0),
	AZF3328_MIXER_SWITCH("15", IDX_MIXER_ADVCTL2, 15, 0),
#endif
};

688
static u16 __devinitdata snd_azf3328_init_values[][2] = {
L
Linus Torvalds 已提交
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
        { IDX_MIXER_PLAY_MASTER,	MIXER_MUTE_MASK|0x1f1f },
        { IDX_MIXER_MODEMOUT,		MIXER_MUTE_MASK|0x1f1f },
	{ IDX_MIXER_BASSTREBLE,		0x0000 },
	{ IDX_MIXER_PCBEEP,		MIXER_MUTE_MASK|0x1f1f },
	{ IDX_MIXER_MODEMIN,		MIXER_MUTE_MASK|0x1f1f },
	{ IDX_MIXER_MIC,		MIXER_MUTE_MASK|0x001f },
	{ IDX_MIXER_LINEIN,		MIXER_MUTE_MASK|0x1f1f },
	{ IDX_MIXER_CDAUDIO,		MIXER_MUTE_MASK|0x1f1f },
	{ IDX_MIXER_VIDEO,		MIXER_MUTE_MASK|0x1f1f },
	{ IDX_MIXER_AUX,		MIXER_MUTE_MASK|0x1f1f },
        { IDX_MIXER_WAVEOUT,		MIXER_MUTE_MASK|0x1f1f },
        { IDX_MIXER_FMSYNTH,		MIXER_MUTE_MASK|0x1f1f },
        { IDX_MIXER_REC_VOLUME,		MIXER_MUTE_MASK|0x0707 },
};

704
static int __devinit
705
snd_azf3328_mixer_new(struct snd_azf3328 *chip)
L
Linus Torvalds 已提交
706
{
707 708
	struct snd_card *card;
	const struct snd_kcontrol_new *sw;
L
Linus Torvalds 已提交
709 710 711 712 713 714 715 716 717
	unsigned int idx;
	int err;

	snd_azf3328_dbgcallenter();
	snd_assert(chip != NULL && chip->card != NULL, return -EINVAL);

	card = chip->card;

	/* mixer reset */
718
	snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
L
Linus Torvalds 已提交
719 720

	/* mute and zero volume channels */
721 722 723 724
	for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_init_values); idx++) {
		snd_azf3328_mixer_outw(chip,
			snd_azf3328_init_values[idx][0],
			snd_azf3328_init_values[idx][1]);
L
Linus Torvalds 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
	}
	
	/* add mixer controls */
	sw = snd_azf3328_mixer_controls;
	for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_mixer_controls); idx++, sw++) {
		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(sw, chip))) < 0)
			return err;
	}
	snd_component_add(card, "AZF3328 mixer");
	strcpy(card->mixername, "AZF3328 mixer");

	snd_azf3328_dbgcallleave();
	return 0;
}

740
static int
741 742
snd_azf3328_hw_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *hw_params)
L
Linus Torvalds 已提交
743 744 745 746 747 748 749 750
{
	int res;
	snd_azf3328_dbgcallenter();
	res = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
	snd_azf3328_dbgcallleave();
	return res;
}

751
static int
752
snd_azf3328_hw_free(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
753 754 755 756 757 758 759
{
	snd_azf3328_dbgcallenter();
	snd_pcm_lib_free_pages(substream);
	snd_azf3328_dbgcallleave();
	return 0;
}

760
static void
761
snd_azf3328_setfmt(struct snd_azf3328 *chip,
L
Linus Torvalds 已提交
762 763 764 765 766 767
			       unsigned int reg,
			       unsigned int bitrate,
			       unsigned int format_width,
			       unsigned int channels
)
{
768
	u16 val = 0xff00;
L
Linus Torvalds 已提交
769 770 771 772
	unsigned long flags;

	snd_azf3328_dbgcallenter();
	switch (bitrate) {
773 774 775 776 777 778 779 780 781 782 783 784 785 786
	case  4000: val |= SOUNDFORMAT_FREQ_SUSPECTED_4000; break;
	case  4800: val |= SOUNDFORMAT_FREQ_SUSPECTED_4800; break;
	case  5512: val |= SOUNDFORMAT_FREQ_5510; break; /* the AZF3328 names it "5510" for some strange reason */
	case  6620: val |= SOUNDFORMAT_FREQ_6620; break;
	case  8000: val |= SOUNDFORMAT_FREQ_8000; break;
	case  9600: val |= SOUNDFORMAT_FREQ_9600; break;
	case 11025: val |= SOUNDFORMAT_FREQ_11025; break;
	case 13240: val |= SOUNDFORMAT_FREQ_SUSPECTED_13240; break;
	case 16000: val |= SOUNDFORMAT_FREQ_16000; break;
	case 22050: val |= SOUNDFORMAT_FREQ_22050; break;
	case 32000: val |= SOUNDFORMAT_FREQ_32000; break;
	case 44100: val |= SOUNDFORMAT_FREQ_44100; break;
	case 48000: val |= SOUNDFORMAT_FREQ_48000; break;
	case 66200: val |= SOUNDFORMAT_FREQ_SUSPECTED_66200; break;
L
Linus Torvalds 已提交
787
	default:
788
		snd_printk(KERN_WARNING "unknown bitrate %d, assuming 44.1kHz!\n", bitrate);
789
		val |= SOUNDFORMAT_FREQ_44100;
L
Linus Torvalds 已提交
790 791
		break;
	}
792 793 794 795
	/* val = 0xff07; 3m27.993s (65301Hz; -> 64000Hz???) hmm, 66120, 65967, 66123 */
	/* val = 0xff09; 17m15.098s (13123,478Hz; -> 12000Hz???) hmm, 13237.2Hz? */
	/* val = 0xff0a; 47m30.599s (4764,891Hz; -> 4800Hz???) yup, 4803Hz */
	/* val = 0xff0c; 57m0.510s (4010,263Hz; -> 4000Hz???) yup, 4003Hz */
L
Linus Torvalds 已提交
796 797 798 799 800
	/* val = 0xff05; 5m11.556s (... -> 44100Hz) */
	/* val = 0xff03; 10m21.529s (21872,463Hz; -> 22050Hz???) */
	/* val = 0xff0f; 20m41.883s (10937,993Hz; -> 11025Hz???) */
	/* val = 0xff0d; 41m23.135s (5523,600Hz; -> 5512Hz???) */
	/* val = 0xff0e; 28m30.777s (8017Hz; -> 8000Hz???) */
801

L
Linus Torvalds 已提交
802 803 804 805 806 807 808 809 810
	if (channels == 2)
		val |= SOUNDFORMAT_FLAG_2CHANNELS;

	if (format_width == 16)
		val |= SOUNDFORMAT_FLAG_16BIT;

	spin_lock_irqsave(&chip->reg_lock, flags);
	
	/* set bitrate/format */
811
	snd_azf3328_codec_outw(chip, reg, val);
L
Linus Torvalds 已提交
812 813 814 815 816 817 818 819 820
	
	/* changing the bitrate/format settings switches off the
	 * audio output with an annoying click in case of 8/16bit format change
	 * (maybe shutting down DAC/ADC?), thus immediately
	 * do some tweaking to reenable it and get rid of the clicking
	 * (FIXME: yes, it works, but what exactly am I doing here?? :)
	 * FIXME: does this have some side effects for full-duplex
	 * or other dramatic side effects? */
	if (reg == IDX_IO_PLAY_SOUNDFORMAT) /* only do it for playback */
821 822 823 824 825 826 827 828
		snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
			snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS) |
			DMA_PLAY_SOMETHING1 |
			DMA_PLAY_SOMETHING2 |
			SOMETHING_ALMOST_ALWAYS_SET |
			DMA_EPILOGUE_SOMETHING |
			DMA_SOMETHING_ELSE
		);
L
Linus Torvalds 已提交
829 830 831 832 833

	spin_unlock_irqrestore(&chip->reg_lock, flags);
	snd_azf3328_dbgcallleave();
}

834
static void
835
snd_azf3328_setdmaa(struct snd_azf3328 *chip,
L
Linus Torvalds 已提交
836 837 838 839 840
				long unsigned int addr,
                                unsigned int count,
                                unsigned int size,
				int do_recording)
{
841 842
	unsigned long flags, portbase;
	unsigned int is_running;
L
Linus Torvalds 已提交
843 844

	snd_azf3328_dbgcallenter();
845
	if (do_recording) {
846 847 848
		/* access capture registers, i.e. skip playback reg section */
		portbase = chip->codec_port + 0x20;
		is_running = chip->is_recording;
849
	} else {
850 851 852 853 854
		/* access the playback register section */
		portbase = chip->codec_port + 0x00;
		is_running = chip->is_playing;
	}

L
Linus Torvalds 已提交
855
	/* AZF3328 uses a two buffer pointer DMA playback approach */
856
	if (!is_running) {
857 858 859 860 861 862 863 864 865 866
		unsigned long addr_area2;
		unsigned long count_areas, count_tmp; /* width 32bit -- overflow!! */
		count_areas = size/2;
		addr_area2 = addr+count_areas;
		count_areas--; /* max. index */
		snd_azf3328_dbgplay("set DMA: buf1 %08lx[%lu], buf2 %08lx[%lu]\n", addr, count_areas, addr_area2, count_areas);

		/* build combined I/O buffer length word */
		count_tmp = count_areas;
		count_areas |= (count_tmp << 16);
L
Linus Torvalds 已提交
867
		spin_lock_irqsave(&chip->reg_lock, flags);
868 869 870
		outl(addr, portbase + IDX_IO_PLAY_DMA_START_1);
		outl(addr_area2, portbase + IDX_IO_PLAY_DMA_START_2);
		outl(count_areas, portbase + IDX_IO_PLAY_DMA_LEN_1);
L
Linus Torvalds 已提交
871 872 873 874 875
		spin_unlock_irqrestore(&chip->reg_lock, flags);
	}
	snd_azf3328_dbgcallleave();
}

876
static int
877
snd_azf3328_playback_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
878 879
{
#if 0
880 881
	struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
882 883 884 885 886 887
        unsigned int size = snd_pcm_lib_buffer_bytes(substream);
	unsigned int count = snd_pcm_lib_period_bytes(substream);
#endif

	snd_azf3328_dbgcallenter();
#if 0
888 889 890 891
	snd_azf3328_setfmt(chip, IDX_IO_PLAY_SOUNDFORMAT,
		runtime->rate,
		snd_pcm_format_width(runtime->format),
		runtime->channels);
L
Linus Torvalds 已提交
892 893 894 895 896 897
	snd_azf3328_setdmaa(chip, runtime->dma_addr, count, size, 0);
#endif
	snd_azf3328_dbgcallleave();
	return 0;
}

898
static int
899
snd_azf3328_capture_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
900 901
{
#if 0
902 903
	struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
904 905 906 907 908 909
        unsigned int size = snd_pcm_lib_buffer_bytes(substream);
	unsigned int count = snd_pcm_lib_period_bytes(substream);
#endif

	snd_azf3328_dbgcallenter();
#if 0
910 911 912 913
	snd_azf3328_setfmt(chip, IDX_IO_REC_SOUNDFORMAT,
		runtime->rate,
		snd_pcm_format_width(runtime->format),
		runtime->channels);
L
Linus Torvalds 已提交
914 915 916 917 918 919
	snd_azf3328_setdmaa(chip, runtime->dma_addr, count, size, 1);
#endif
	snd_azf3328_dbgcallleave();
	return 0;
}

920
static int
921
snd_azf3328_playback_trigger(struct snd_pcm_substream *substream, int cmd)
L
Linus Torvalds 已提交
922
{
923 924
	struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
925 926 927 928
	int result = 0;
	unsigned int status1;

	snd_azf3328_dbgcalls("snd_azf3328_playback_trigger cmd %d\n", cmd);
929

L
Linus Torvalds 已提交
930 931
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
932
		snd_azf3328_dbgplay("START PLAYBACK\n");
L
Linus Torvalds 已提交
933 934 935 936

		/* mute WaveOut */
		snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 1);

937 938 939 940
		snd_azf3328_setfmt(chip, IDX_IO_PLAY_SOUNDFORMAT,
			runtime->rate,
			snd_pcm_format_width(runtime->format),
			runtime->channels);
L
Linus Torvalds 已提交
941 942 943

		spin_lock(&chip->reg_lock);
		/* stop playback */
944
		status1 = snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS);
L
Linus Torvalds 已提交
945
		status1 &= ~DMA_RESUME;
946
		snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
L
Linus Torvalds 已提交
947 948
	    
		/* FIXME: clear interrupts or what??? */
949
		snd_azf3328_codec_outw(chip, IDX_IO_PLAY_IRQTYPE, 0xffff);
L
Linus Torvalds 已提交
950 951
		spin_unlock(&chip->reg_lock);

952 953 954 955
		snd_azf3328_setdmaa(chip, runtime->dma_addr,
			snd_pcm_lib_period_bytes(substream),
			snd_pcm_lib_buffer_bytes(substream),
			0);
L
Linus Torvalds 已提交
956 957 958 959 960

		spin_lock(&chip->reg_lock);
#ifdef WIN9X
		/* FIXME: enable playback/recording??? */
		status1 |= DMA_PLAY_SOMETHING1 | DMA_PLAY_SOMETHING2;
961
		snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
L
Linus Torvalds 已提交
962 963 964 965

		/* start playback again */
		/* FIXME: what is this value (0x0010)??? */
		status1 |= DMA_RESUME | DMA_EPILOGUE_SOMETHING;
966
		snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
L
Linus Torvalds 已提交
967
#else /* NT4 */
968 969 970 971 972 973 974 975 976 977 978 979
		snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
			0x0000);
		snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
			DMA_PLAY_SOMETHING1);
		snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
			DMA_PLAY_SOMETHING1 |
			DMA_PLAY_SOMETHING2);
		snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
			DMA_RESUME |
			SOMETHING_ALMOST_ALWAYS_SET |
			DMA_EPILOGUE_SOMETHING |
			DMA_SOMETHING_ELSE);
L
Linus Torvalds 已提交
980 981 982 983 984 985 986
#endif
		spin_unlock(&chip->reg_lock);

		/* now unmute WaveOut */
		snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 0);

		chip->is_playing = 1;
987
		snd_azf3328_dbgplay("STARTED PLAYBACK\n");
L
Linus Torvalds 已提交
988
		break;
989 990 991 992 993 994 995
	case SNDRV_PCM_TRIGGER_RESUME:
		snd_azf3328_dbgplay("RESUME PLAYBACK\n");
		/* resume playback if we were active */
		if (chip->is_playing)
			snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
				snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS) | DMA_RESUME);
		break;
996 997 998
	case SNDRV_PCM_TRIGGER_STOP:
		snd_azf3328_dbgplay("STOP PLAYBACK\n");

L
Linus Torvalds 已提交
999 1000 1001 1002 1003
		/* mute WaveOut */
		snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 1);

		spin_lock(&chip->reg_lock);
		/* stop playback */
1004
		status1 = snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS);
L
Linus Torvalds 已提交
1005 1006

		status1 &= ~DMA_RESUME;
1007
		snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
L
Linus Torvalds 已提交
1008

1009 1010
		/* hmm, is this really required? we're resetting the same bit
		 * immediately thereafter... */
L
Linus Torvalds 已提交
1011
		status1 |= DMA_PLAY_SOMETHING1;
1012
		snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
L
Linus Torvalds 已提交
1013 1014

		status1 &= ~DMA_PLAY_SOMETHING1;
1015
		snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
L
Linus Torvalds 已提交
1016 1017 1018 1019 1020
		spin_unlock(&chip->reg_lock);
	    
		/* now unmute WaveOut */
		snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 0);
		chip->is_playing = 0;
1021
		snd_azf3328_dbgplay("STOPPED PLAYBACK\n");
L
Linus Torvalds 已提交
1022
		break;
1023 1024 1025 1026 1027 1028
	case SNDRV_PCM_TRIGGER_SUSPEND:
		snd_azf3328_dbgplay("SUSPEND PLAYBACK\n");
		/* make sure playback is stopped */
		snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
			snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS) & ~DMA_RESUME);
		break;
L
Linus Torvalds 已提交
1029
        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1030
		snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_PUSH NIY!\n");
L
Linus Torvalds 已提交
1031 1032
                break;
        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1033
		snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_RELEASE NIY!\n");
L
Linus Torvalds 已提交
1034 1035
                break;
        default:
1036
		printk(KERN_ERR "FIXME: unknown trigger mode!\n");
L
Linus Torvalds 已提交
1037 1038 1039 1040 1041 1042 1043 1044 1045
                return -EINVAL;
	}
	
	snd_azf3328_dbgcallleave();
	return result;
}

/* this is just analogous to playback; I'm not quite sure whether recording
 * should actually be triggered like that */
1046
static int
1047
snd_azf3328_capture_trigger(struct snd_pcm_substream *substream, int cmd)
L
Linus Torvalds 已提交
1048
{
1049 1050
	struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1051 1052 1053 1054
	int result = 0;
	unsigned int status1;

	snd_azf3328_dbgcalls("snd_azf3328_capture_trigger cmd %d\n", cmd);
1055

L
Linus Torvalds 已提交
1056 1057 1058
        switch (cmd) {
        case SNDRV_PCM_TRIGGER_START:

1059
		snd_azf3328_dbgplay("START CAPTURE\n");
L
Linus Torvalds 已提交
1060

1061 1062 1063 1064
		snd_azf3328_setfmt(chip, IDX_IO_REC_SOUNDFORMAT,
			runtime->rate,
			snd_pcm_format_width(runtime->format),
			runtime->channels);
L
Linus Torvalds 已提交
1065 1066 1067

		spin_lock(&chip->reg_lock);
		/* stop recording */
1068
		status1 = snd_azf3328_codec_inw(chip, IDX_IO_REC_FLAGS);
L
Linus Torvalds 已提交
1069
		status1 &= ~DMA_RESUME;
1070
		snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
L
Linus Torvalds 已提交
1071 1072
	    
		/* FIXME: clear interrupts or what??? */
1073
		snd_azf3328_codec_outw(chip, IDX_IO_REC_IRQTYPE, 0xffff);
L
Linus Torvalds 已提交
1074 1075
		spin_unlock(&chip->reg_lock);

1076 1077 1078 1079
		snd_azf3328_setdmaa(chip, runtime->dma_addr,
			snd_pcm_lib_period_bytes(substream),
			snd_pcm_lib_buffer_bytes(substream),
			1);
L
Linus Torvalds 已提交
1080 1081 1082 1083 1084

		spin_lock(&chip->reg_lock);
#ifdef WIN9X
		/* FIXME: enable playback/recording??? */
		status1 |= DMA_PLAY_SOMETHING1 | DMA_PLAY_SOMETHING2;
1085
		snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
L
Linus Torvalds 已提交
1086

1087
		/* start capture again */
L
Linus Torvalds 已提交
1088 1089
		/* FIXME: what is this value (0x0010)??? */
		status1 |= DMA_RESUME | DMA_EPILOGUE_SOMETHING;
1090
		snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
L
Linus Torvalds 已提交
1091
#else
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
		snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
			0x0000);
		snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
			DMA_PLAY_SOMETHING1);
		snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
			DMA_PLAY_SOMETHING1 |
			DMA_PLAY_SOMETHING2);
		snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
			DMA_RESUME |
			SOMETHING_ALMOST_ALWAYS_SET |
			DMA_EPILOGUE_SOMETHING |
			DMA_SOMETHING_ELSE);
L
Linus Torvalds 已提交
1104 1105 1106
#endif
		spin_unlock(&chip->reg_lock);

1107 1108
		chip->is_recording = 1;
		snd_azf3328_dbgplay("STARTED CAPTURE\n");
L
Linus Torvalds 已提交
1109
		break;
1110 1111 1112 1113 1114 1115 1116
	case SNDRV_PCM_TRIGGER_RESUME:
		snd_azf3328_dbgplay("RESUME CAPTURE\n");
		/* resume recording if we were active */
		if (chip->is_recording)
			snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
				snd_azf3328_codec_inw(chip, IDX_IO_REC_FLAGS) | DMA_RESUME);
		break;
L
Linus Torvalds 已提交
1117
        case SNDRV_PCM_TRIGGER_STOP:
1118 1119
		snd_azf3328_dbgplay("STOP CAPTURE\n");

L
Linus Torvalds 已提交
1120 1121
		spin_lock(&chip->reg_lock);
		/* stop recording */
1122
		status1 = snd_azf3328_codec_inw(chip, IDX_IO_REC_FLAGS);
L
Linus Torvalds 已提交
1123 1124

		status1 &= ~DMA_RESUME;
1125
		snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
L
Linus Torvalds 已提交
1126 1127

		status1 |= DMA_PLAY_SOMETHING1;
1128
		snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
L
Linus Torvalds 已提交
1129 1130

		status1 &= ~DMA_PLAY_SOMETHING1;
1131
		snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
L
Linus Torvalds 已提交
1132 1133
		spin_unlock(&chip->reg_lock);
	    
1134 1135
		chip->is_recording = 0;
		snd_azf3328_dbgplay("STOPPED CAPTURE\n");
L
Linus Torvalds 已提交
1136
		break;
1137 1138 1139 1140 1141 1142
	case SNDRV_PCM_TRIGGER_SUSPEND:
		snd_azf3328_dbgplay("SUSPEND CAPTURE\n");
		/* make sure recording is stopped */
		snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
			snd_azf3328_codec_inw(chip, IDX_IO_REC_FLAGS) & ~DMA_RESUME);
		break;
L
Linus Torvalds 已提交
1143
        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1144
		snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_PUSH NIY!\n");
L
Linus Torvalds 已提交
1145 1146
                break;
        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1147
		snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_RELEASE NIY!\n");
L
Linus Torvalds 已提交
1148 1149
                break;
        default:
1150
		printk(KERN_ERR "FIXME: unknown trigger mode!\n");
L
Linus Torvalds 已提交
1151 1152 1153 1154 1155 1156 1157
                return -EINVAL;
	}
	
	snd_azf3328_dbgcallleave();
	return result;
}

1158
static snd_pcm_uframes_t
1159
snd_azf3328_playback_pointer(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1160
{
1161
	struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
1162
	unsigned long bufptr, result;
L
Linus Torvalds 已提交
1163 1164 1165 1166 1167 1168 1169
	snd_pcm_uframes_t frmres;

#ifdef QUERY_HARDWARE
	bufptr = inl(chip->codec_port+IDX_IO_PLAY_DMA_START_1);
#else
	bufptr = substream->runtime->dma_addr;
#endif
1170
	result = inl(chip->codec_port+IDX_IO_PLAY_DMA_CURRPOS);
L
Linus Torvalds 已提交
1171

1172 1173 1174 1175
	/* calculate offset */
	result -= bufptr;
	frmres = bytes_to_frames( substream->runtime, result);
	snd_azf3328_dbgplay("PLAY @ 0x%8lx, frames %8ld\n", result, frmres);
L
Linus Torvalds 已提交
1176 1177 1178
	return frmres;
}

1179
static snd_pcm_uframes_t
1180
snd_azf3328_capture_pointer(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1181
{
1182
	struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
1183
	unsigned long bufptr, result;
L
Linus Torvalds 已提交
1184 1185 1186 1187 1188 1189 1190
	snd_pcm_uframes_t frmres;

#ifdef QUERY_HARDWARE
	bufptr = inl(chip->codec_port+IDX_IO_REC_DMA_START_1);
#else
	bufptr = substream->runtime->dma_addr;
#endif
1191
	result = inl(chip->codec_port+IDX_IO_REC_DMA_CURRPOS);
L
Linus Torvalds 已提交
1192

1193 1194 1195 1196
	/* calculate offset */
	result -= bufptr;
	frmres = bytes_to_frames( substream->runtime, result);
	snd_azf3328_dbgplay("REC  @ 0x%8lx, frames %8ld\n", result, frmres);
L
Linus Torvalds 已提交
1197 1198 1199
	return frmres;
}

1200
static irqreturn_t
1201
snd_azf3328_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
1202
{
1203
	struct snd_azf3328 *chip = dev_id;
1204 1205
	u8 status, which;
	static unsigned long irq_count;
L
Linus Torvalds 已提交
1206

1207
	status = snd_azf3328_codec_inb(chip, IDX_IO_IRQSTATUS);
L
Linus Torvalds 已提交
1208 1209

        /* fast path out, to ease interrupt sharing */
1210
	if (!(status & (IRQ_PLAYBACK|IRQ_RECORDING|IRQ_MPU401|IRQ_TIMER)))
L
Linus Torvalds 已提交
1211 1212
		return IRQ_NONE; /* must be interrupt for another device */

1213 1214 1215 1216 1217
	snd_azf3328_dbgplay("Interrupt %ld!\nIDX_IO_PLAY_FLAGS %04x, IDX_IO_PLAY_IRQTYPE %04x, IDX_IO_IRQSTATUS %04x\n",
		irq_count,
		snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS),
		snd_azf3328_codec_inw(chip, IDX_IO_PLAY_IRQTYPE),
		status);
L
Linus Torvalds 已提交
1218
		
1219
	if (status & IRQ_TIMER) {
1220 1221 1222 1223 1224 1225 1226 1227 1228
		/* snd_azf3328_dbgplay("timer %ld\n", inl(chip->codec_port+IDX_IO_TIMER_VALUE) & TIMER_VALUE_MASK); */
		if (chip->timer)
			snd_timer_interrupt(chip->timer, chip->timer->sticks);
		/* ACK timer */
                spin_lock(&chip->reg_lock);
		snd_azf3328_codec_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x07);
		spin_unlock(&chip->reg_lock);
		snd_azf3328_dbgplay("azt3328: timer IRQ\n");
	}
1229
	if (status & IRQ_PLAYBACK) {
L
Linus Torvalds 已提交
1230
		spin_lock(&chip->reg_lock);
1231 1232 1233 1234 1235
		which = snd_azf3328_codec_inb(chip, IDX_IO_PLAY_IRQTYPE);
		/* ack all IRQ types immediately */
		snd_azf3328_codec_outb(chip, IDX_IO_PLAY_IRQTYPE, which);
               	spin_unlock(&chip->reg_lock);

1236
		if (chip->pcm && chip->playback_substream) {
L
Linus Torvalds 已提交
1237
			snd_pcm_period_elapsed(chip->playback_substream);
1238 1239 1240
			snd_azf3328_dbgplay("PLAY period done (#%x), @ %x\n",
				which,
				inl(chip->codec_port+IDX_IO_PLAY_DMA_CURRPOS));
1241
		} else
L
Linus Torvalds 已提交
1242
			snd_azf3328_dbgplay("azt3328: ouch, irq handler problem!\n");
1243 1244
		if (which & IRQ_PLAY_SOMETHING)
			snd_azf3328_dbgplay("azt3328: unknown play IRQ type occurred, please report!\n");
L
Linus Torvalds 已提交
1245
	}
1246
	if (status & IRQ_RECORDING) {
L
Linus Torvalds 已提交
1247
                spin_lock(&chip->reg_lock);
1248 1249 1250 1251 1252
		which = snd_azf3328_codec_inb(chip, IDX_IO_REC_IRQTYPE);
		/* ack all IRQ types immediately */
		snd_azf3328_codec_outb(chip, IDX_IO_REC_IRQTYPE, which);
		spin_unlock(&chip->reg_lock);

1253
		if (chip->pcm && chip->capture_substream) {
L
Linus Torvalds 已提交
1254
			snd_pcm_period_elapsed(chip->capture_substream);
1255 1256 1257
			snd_azf3328_dbgplay("REC  period done (#%x), @ %x\n",
				which,
				inl(chip->codec_port+IDX_IO_REC_DMA_CURRPOS));
1258
		} else
1259 1260 1261
			snd_azf3328_dbgplay("azt3328: ouch, irq handler problem!\n");
		if (which & IRQ_REC_SOMETHING)
			snd_azf3328_dbgplay("azt3328: unknown rec IRQ type occurred, please report!\n");
L
Linus Torvalds 已提交
1262
	}
1263 1264
	/* MPU401 has less critical IRQ requirements
	 * than timer and playback/recording, right? */
1265
	if (status & IRQ_MPU401) {
1266
		snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
1267 1268 1269 1270 1271 1272

		/* hmm, do we have to ack the IRQ here somehow?
		 * If so, then I don't know how... */
		snd_azf3328_dbgplay("azt3328: MPU401 IRQ\n");
	}
	irq_count++;
L
Linus Torvalds 已提交
1273 1274 1275 1276 1277
	return IRQ_HANDLED;
}

/*****************************************************************/

1278
static const struct snd_pcm_hardware snd_azf3328_playback =
L
Linus Torvalds 已提交
1279 1280
{
	/* FIXME!! Correct? */
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
	.info =			SNDRV_PCM_INFO_MMAP |
				SNDRV_PCM_INFO_INTERLEAVED |
				SNDRV_PCM_INFO_MMAP_VALID,
	.formats =		SNDRV_PCM_FMTBIT_S8 |
				SNDRV_PCM_FMTBIT_U8 |
				SNDRV_PCM_FMTBIT_S16_LE |
				SNDRV_PCM_FMTBIT_U16_LE,
	.rates =		SNDRV_PCM_RATE_5512 |
				SNDRV_PCM_RATE_8000_48000 |
				SNDRV_PCM_RATE_KNOT,
	.rate_min =		4000,
	.rate_max =		66200,
L
Linus Torvalds 已提交
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
	.channels_min =		1,
	.channels_max =		2,
	.buffer_bytes_max =	65536,
	.period_bytes_min =	64,
	.period_bytes_max =	65536,
	.periods_min =		1,
	.periods_max =		1024,
	/* FIXME: maybe that card actually has a FIFO?
	 * Hmm, it seems newer revisions do have one, but we still don't know
	 * its size... */
	.fifo_size =		0,
};

1306
static const struct snd_pcm_hardware snd_azf3328_capture =
L
Linus Torvalds 已提交
1307 1308
{
	/* FIXME */
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
	.info =			SNDRV_PCM_INFO_MMAP |
				SNDRV_PCM_INFO_INTERLEAVED |
				SNDRV_PCM_INFO_MMAP_VALID,
	.formats =		SNDRV_PCM_FMTBIT_S8 |
				SNDRV_PCM_FMTBIT_U8 |
				SNDRV_PCM_FMTBIT_S16_LE |
				SNDRV_PCM_FMTBIT_U16_LE,
	.rates =		SNDRV_PCM_RATE_5512 |
				SNDRV_PCM_RATE_8000_48000 |
				SNDRV_PCM_RATE_KNOT,
	.rate_min =		4000,
	.rate_max =		66200,
L
Linus Torvalds 已提交
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
	.channels_min =		1,
	.channels_max =		2,
	.buffer_bytes_max =	65536,
	.period_bytes_min =	64,
	.period_bytes_max =	65536,
	.periods_min =		1,
	.periods_max =		1024,
	.fifo_size =		0,
};


static unsigned int snd_azf3328_fixed_rates[] = {
1333 1334
	4000, 4800, 5512, 6620, 8000, 9600, 11025, 13240, 16000, 22050, 32000,
	44100, 48000, 66200 };
1335
static struct snd_pcm_hw_constraint_list snd_azf3328_hw_constraints_rates = {
L
Linus Torvalds 已提交
1336 1337 1338 1339 1340 1341 1342
	.count = ARRAY_SIZE(snd_azf3328_fixed_rates), 
	.list = snd_azf3328_fixed_rates,
	.mask = 0,
};

/*****************************************************************/

1343
static int
1344
snd_azf3328_playback_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1345
{
1346 1347
	struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357

	snd_azf3328_dbgcallenter();
	chip->playback_substream = substream;
	runtime->hw = snd_azf3328_playback;
	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
				   &snd_azf3328_hw_constraints_rates);
	snd_azf3328_dbgcallleave();
	return 0;
}

1358
static int
1359
snd_azf3328_capture_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1360
{
1361 1362
	struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372

	snd_azf3328_dbgcallenter();
	chip->capture_substream = substream;
	runtime->hw = snd_azf3328_capture;
	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
				   &snd_azf3328_hw_constraints_rates);
	snd_azf3328_dbgcallleave();
	return 0;
}

1373
static int
1374
snd_azf3328_playback_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1375
{
1376
	struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1377 1378 1379 1380 1381 1382 1383

	snd_azf3328_dbgcallenter();
	chip->playback_substream = NULL;
	snd_azf3328_dbgcallleave();
	return 0;
}

1384
static int
1385
snd_azf3328_capture_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1386
{
1387
	struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396

	snd_azf3328_dbgcallenter();
	chip->capture_substream = NULL;
	snd_azf3328_dbgcallleave();
	return 0;
}

/******************************************************************/

1397
static struct snd_pcm_ops snd_azf3328_playback_ops = {
L
Linus Torvalds 已提交
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
	.open =		snd_azf3328_playback_open,
	.close =	snd_azf3328_playback_close,
	.ioctl =	snd_pcm_lib_ioctl,
	.hw_params =	snd_azf3328_hw_params,
	.hw_free =	snd_azf3328_hw_free,
	.prepare =	snd_azf3328_playback_prepare,
	.trigger =	snd_azf3328_playback_trigger,
	.pointer =	snd_azf3328_playback_pointer
};

1408
static struct snd_pcm_ops snd_azf3328_capture_ops = {
L
Linus Torvalds 已提交
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
	.open =		snd_azf3328_capture_open,
	.close =	snd_azf3328_capture_close,
	.ioctl =	snd_pcm_lib_ioctl,
	.hw_params =	snd_azf3328_hw_params,
	.hw_free =	snd_azf3328_hw_free,
	.prepare =	snd_azf3328_capture_prepare,
	.trigger =	snd_azf3328_capture_trigger,
	.pointer =	snd_azf3328_capture_pointer
};

1419
static int __devinit
1420
snd_azf3328_pcm(struct snd_azf3328 *chip, int device)
L
Linus Torvalds 已提交
1421
{
1422
	struct snd_pcm *pcm;
L
Linus Torvalds 已提交
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
	int err;

	snd_azf3328_dbgcallenter();
	if ((err = snd_pcm_new(chip->card, "AZF3328 DSP", device, 1, 1, &pcm)) < 0)
		return err;
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_azf3328_playback_ops);
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_azf3328_capture_ops);

	pcm->private_data = chip;
	pcm->info_flags = 0;
	strcpy(pcm->name, chip->card->shortname);
	chip->pcm = pcm;

	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
					      snd_dma_pci_data(chip->pci), 64*1024, 64*1024);

	snd_azf3328_dbgcallleave();
	return 0;
}

/******************************************************************/

#ifdef SUPPORT_JOYSTICK
1446
static int __devinit
1447
snd_azf3328_config_joystick(struct snd_azf3328 *chip, int dev)
L
Linus Torvalds 已提交
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
{
	struct gameport *gp;
	struct resource *r;

	if (!joystick[dev])
		return -ENODEV;

	if (!(r = request_region(0x200, 8, "AZF3328 gameport"))) {
		printk(KERN_WARNING "azt3328: cannot reserve joystick ports\n");
		return -EBUSY;
	}

	chip->gameport = gp = gameport_allocate_port();
	if (!gp) {
		printk(KERN_ERR "azt3328: cannot allocate memory for gameport\n");
1463
		release_and_free_resource(r);
L
Linus Torvalds 已提交
1464 1465 1466 1467 1468 1469 1470 1471 1472
		return -ENOMEM;
	}

	gameport_set_name(gp, "AZF3328 Gameport");
	gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
	gameport_set_dev_parent(gp, &chip->pci->dev);
	gp->io = 0x200;
	gameport_set_port_data(gp, r);

1473 1474
	snd_azf3328_io2_outb(chip, IDX_IO2_LEGACY_ADDR,
			      snd_azf3328_io2_inb(chip, IDX_IO2_LEGACY_ADDR) | LEGACY_JOY);
L
Linus Torvalds 已提交
1475 1476 1477 1478 1479 1480

	gameport_register_port(chip->gameport);

	return 0;
}

1481
static void
1482
snd_azf3328_free_joystick(struct snd_azf3328 *chip)
L
Linus Torvalds 已提交
1483 1484 1485 1486 1487 1488 1489
{
	if (chip->gameport) {
		struct resource *r = gameport_get_port_data(chip->gameport);

		gameport_unregister_port(chip->gameport);
		chip->gameport = NULL;
		/* disable gameport */
1490 1491
		snd_azf3328_io2_outb(chip, IDX_IO2_LEGACY_ADDR,
				      snd_azf3328_io2_inb(chip, IDX_IO2_LEGACY_ADDR) & ~LEGACY_JOY);
1492
		release_and_free_resource(r);
L
Linus Torvalds 已提交
1493 1494 1495
	}
}
#else
1496
static inline int
1497
snd_azf3328_config_joystick(struct snd_azf3328 *chip, int dev) { return -ENOSYS; }
1498
static inline void
1499
snd_azf3328_free_joystick(struct snd_azf3328 *chip) { }
L
Linus Torvalds 已提交
1500 1501 1502 1503
#endif

/******************************************************************/

1504
static int
1505
snd_azf3328_free(struct snd_azf3328 *chip)
L
Linus Torvalds 已提交
1506 1507 1508 1509 1510 1511
{
        if (chip->irq < 0)
                goto __end_hw;

	/* reset (close) mixer */
	snd_azf3328_mixer_set_mute(chip, IDX_MIXER_PLAY_MASTER, 1); /* first mute master volume */
1512
	snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
L
Linus Torvalds 已提交
1513

1514 1515 1516
        /* interrupt setup - mask everything (FIXME!) */
	/* well, at least we know how to disable the timer IRQ */
	snd_azf3328_codec_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x00);
L
Linus Torvalds 已提交
1517 1518

        synchronize_irq(chip->irq);
1519
__end_hw:
L
Linus Torvalds 已提交
1520 1521
	snd_azf3328_free_joystick(chip);
        if (chip->irq >= 0)
1522
		free_irq(chip->irq, chip);
L
Linus Torvalds 已提交
1523 1524 1525 1526 1527 1528 1529
	pci_release_regions(chip->pci);
	pci_disable_device(chip->pci);

        kfree(chip);
        return 0;
}

1530
static int
1531
snd_azf3328_dev_free(struct snd_device *device)
L
Linus Torvalds 已提交
1532
{
1533
	struct snd_azf3328 *chip = device->device_data;
L
Linus Torvalds 已提交
1534 1535 1536
	return snd_azf3328_free(chip);
}

1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
/******************************************************************/

/*** NOTE: the physical timer resolution actually is 1024000 ticks per second,
 *** but announcing those attributes to user-space would make programs
 *** configure the timer to a 1 tick value, resulting in an absolutely fatal
 *** timer IRQ storm.
 *** Thus I chose to announce a down-scaled virtual timer to the outside and
 *** calculate real timer countdown values internally.
 *** (the scale factor can be set via module parameter "seqtimer_scaling").
 ***/

static int
1549
snd_azf3328_timer_start(struct snd_timer *timer)
1550
{
1551
	struct snd_azf3328 *chip;
1552 1553 1554 1555 1556 1557
	unsigned long flags;
	unsigned int delay;

	snd_azf3328_dbgcallenter();
	chip = snd_timer_chip(timer);
	delay = ((timer->sticks * seqtimer_scaling) - 1) & TIMER_VALUE_MASK;
1558
	if (delay < 49) {
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
		/* uhoh, that's not good, since user-space won't know about
		 * this timing tweak
		 * (we need to do it to avoid a lockup, though) */

		snd_azf3328_dbgtimer("delay was too low (%d)!\n", delay);
		delay = 49; /* minimum time is 49 ticks */
	}
	snd_azf3328_dbgtimer("setting timer countdown value %d, add COUNTDOWN|IRQ\n", delay);
	delay |= TIMER_ENABLE_COUNTDOWN | TIMER_ENABLE_IRQ;
	spin_lock_irqsave(&chip->reg_lock, flags);
	snd_azf3328_codec_outl(chip, IDX_IO_TIMER_VALUE, delay);
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	snd_azf3328_dbgcallleave();
	return 0;
}

static int
1576
snd_azf3328_timer_stop(struct snd_timer *timer)
1577
{
1578
	struct snd_azf3328 *chip;
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
	unsigned long flags;

	snd_azf3328_dbgcallenter();
	chip = snd_timer_chip(timer);
	spin_lock_irqsave(&chip->reg_lock, flags);
	/* disable timer countdown and interrupt */
	/* FIXME: should we write TIMER_ACK_IRQ here? */
	snd_azf3328_codec_outb(chip, IDX_IO_TIMER_VALUE + 3, 0);
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	snd_azf3328_dbgcallleave();
	return 0;
}


static int
1594
snd_azf3328_timer_precise_resolution(struct snd_timer *timer,
1595 1596 1597 1598 1599 1600 1601 1602 1603
					       unsigned long *num, unsigned long *den)
{
	snd_azf3328_dbgcallenter();
	*num = 1;
	*den = 1024000 / seqtimer_scaling;
	snd_azf3328_dbgcallleave();
	return 0;
}

1604
static struct snd_timer_hardware snd_azf3328_timer_hw = {
1605 1606 1607 1608 1609 1610 1611 1612 1613
	.flags = SNDRV_TIMER_HW_AUTO,
	.resolution = 977, /* 1000000/1024000 = 0.9765625us */
	.ticks = 1024000, /* max tick count, defined by the value register; actually it's not 1024000, but 1048576, but we don't care */
	.start = snd_azf3328_timer_start,
	.stop = snd_azf3328_timer_stop,
	.precise_resolution = snd_azf3328_timer_precise_resolution,
};

static int __devinit
1614
snd_azf3328_timer(struct snd_azf3328 *chip, int device)
1615
{
1616 1617
	struct snd_timer *timer = NULL;
	struct snd_timer_id tid;
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
	int err;

	snd_azf3328_dbgcallenter();
	tid.dev_class = SNDRV_TIMER_CLASS_CARD;
	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
	tid.card = chip->card->number;
	tid.device = device;
	tid.subdevice = 0;

	snd_azf3328_timer_hw.resolution *= seqtimer_scaling;
	snd_azf3328_timer_hw.ticks /= seqtimer_scaling;
	if ((err = snd_timer_new(chip->card, "AZF3328", &tid, &timer)) < 0) {
		goto out;
	}

	strcpy(timer->name, "AZF3328 timer");
	timer->private_data = chip;
	timer->hw = snd_azf3328_timer_hw;

	chip->timer = timer;

	err = 0;

out:
	snd_azf3328_dbgcallleave();
	return err;
}

/******************************************************************/

L
Linus Torvalds 已提交
1648 1649
#if 0
/* check whether a bit can be modified */
1650 1651
static void
snd_azf3328_test_bit(unsigned int reg, int bit)
L
Linus Torvalds 已提交
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
{
	unsigned char val, valoff, valon;

	val = inb(reg);

	outb(val & ~(1 << bit), reg);
	valoff = inb(reg);

	outb(val|(1 << bit), reg);
	valon = inb(reg);
	
	outb(val, reg);

	printk(KERN_ERR "reg %04x bit %d: %02x %02x %02x\n", reg, bit, val, valoff, valon);
}
#endif

1669
#if DEBUG_MISC
1670
static void
1671
snd_azf3328_debug_show_ports(const struct snd_azf3328 *chip)
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
{
	u16 tmp;

	snd_azf3328_dbgmisc("codec_port 0x%lx, io2_port 0x%lx, mpu_port 0x%lx, synth_port 0x%lx, mixer_port 0x%lx, irq %d\n", chip->codec_port, chip->io2_port, chip->mpu_port, chip->synth_port, chip->mixer_port, chip->irq);

	snd_azf3328_dbgmisc("io2 %02x %02x %02x %02x %02x %02x\n", snd_azf3328_io2_inb(chip, 0), snd_azf3328_io2_inb(chip, 1), snd_azf3328_io2_inb(chip, 2), snd_azf3328_io2_inb(chip, 3), snd_azf3328_io2_inb(chip, 4), snd_azf3328_io2_inb(chip, 5));

	for (tmp=0; tmp <= 0x01; tmp += 1)
		snd_azf3328_dbgmisc("0x%02x: opl 0x%04x, mpu300 0x%04x, mpu310 0x%04x, mpu320 0x%04x, mpu330 0x%04x\n", tmp, inb(0x388 + tmp), inb(0x300 + tmp), inb(0x310 + tmp), inb(0x320 + tmp), inb(0x330 + tmp));

1682 1683 1684 1685 1686
	for (tmp = 0; tmp < AZF_IO_SIZE_CODEC; tmp += 2)
		snd_azf3328_dbgmisc("codec 0x%02x: 0x%04x\n", tmp, snd_azf3328_codec_inw(chip, tmp));

	for (tmp = 0; tmp < AZF_IO_SIZE_MIXER; tmp += 2)
		snd_azf3328_dbgmisc("mixer 0x%02x: 0x%04x\n", tmp, snd_azf3328_mixer_inw(chip, tmp));
1687
}
1688 1689 1690 1691
#else
static inline void
snd_azf3328_debug_show_ports(const struct snd_azf3328 *chip) {}
#endif
1692 1693

static int __devinit
1694
snd_azf3328_create(struct snd_card *card,
L
Linus Torvalds 已提交
1695 1696
                                         struct pci_dev *pci,
                                         unsigned long device_type,
1697
                                         struct snd_azf3328 ** rchip)
L
Linus Torvalds 已提交
1698
{
1699
	struct snd_azf3328 *chip;
L
Linus Torvalds 已提交
1700
	int err;
1701
	static struct snd_device_ops ops = {
L
Linus Torvalds 已提交
1702 1703 1704 1705 1706 1707 1708 1709 1710
		.dev_free =     snd_azf3328_dev_free,
	};
	u16 tmp;

	*rchip = NULL;

	if ((err = pci_enable_device(pci)) < 0)
		return err;

1711
	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
L
Linus Torvalds 已提交
1712
	if (chip == NULL) {
1713 1714
		err = -ENOMEM;
		goto out_err;
L
Linus Torvalds 已提交
1715 1716 1717 1718 1719 1720 1721
	}
	spin_lock_init(&chip->reg_lock);
	chip->card = card;
	chip->pci = pci;
	chip->irq = -1;

	/* check if we can restrict PCI DMA transfers to 24 bits */
1722 1723
	if (pci_set_dma_mask(pci, DMA_24BIT_MASK) < 0 ||
	    pci_set_consistent_dma_mask(pci, DMA_24BIT_MASK) < 0) {
1724
		snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
1725 1726
		err = -ENXIO;
		goto out_err;
L
Linus Torvalds 已提交
1727 1728 1729
	}

	if ((err = pci_request_regions(pci, "Aztech AZF3328")) < 0) {
1730
		goto out_err;
L
Linus Torvalds 已提交
1731 1732 1733
	}

	chip->codec_port = pci_resource_start(pci, 0);
1734 1735
	chip->io2_port   = pci_resource_start(pci, 1);
	chip->mpu_port   = pci_resource_start(pci, 2);
L
Linus Torvalds 已提交
1736 1737 1738
	chip->synth_port = pci_resource_start(pci, 3);
	chip->mixer_port = pci_resource_start(pci, 4);

1739 1740
	if (request_irq(pci->irq, snd_azf3328_interrupt,
			IRQF_SHARED, card->shortname, chip)) {
1741
		snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
1742 1743
		err = -EBUSY;
		goto out_err;
L
Linus Torvalds 已提交
1744 1745 1746 1747 1748
	}
	chip->irq = pci->irq;
	pci_set_master(pci);
	synchronize_irq(chip->irq);

1749 1750
	snd_azf3328_debug_show_ports(chip);
	
L
Linus Torvalds 已提交
1751
	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1752
		goto out_err;
L
Linus Torvalds 已提交
1753 1754 1755 1756
	}

	/* create mixer interface & switches */
	if ((err = snd_azf3328_mixer_new(chip)) < 0)
1757
		goto out_err;
L
Linus Torvalds 已提交
1758 1759 1760 1761 1762 1763 1764

#if 0
	/* set very low bitrate to reduce noise and power consumption? */
	snd_azf3328_setfmt(chip, IDX_IO_PLAY_SOUNDFORMAT, 5512, 8, 1);
#endif

	/* standard chip init stuff */
1765 1766
	/* default IRQ init value */
	tmp = DMA_PLAY_SOMETHING2|DMA_EPILOGUE_SOMETHING|DMA_SOMETHING_ELSE;
L
Linus Torvalds 已提交
1767

1768 1769 1770 1771 1772
	spin_lock_irq(&chip->reg_lock);
	snd_azf3328_codec_outb(chip, IDX_IO_PLAY_FLAGS, tmp);
	snd_azf3328_codec_outb(chip, IDX_IO_REC_FLAGS, tmp);
	snd_azf3328_codec_outb(chip, IDX_IO_SOMETHING_FLAGS, tmp);
	snd_azf3328_codec_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x00); /* disable timer */
L
Linus Torvalds 已提交
1773 1774 1775 1776 1777
	spin_unlock_irq(&chip->reg_lock);

	snd_card_set_dev(card, &pci->dev);

	*rchip = chip;
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788

	err = 0;
	goto out;

out_err:
	if (chip)
		snd_azf3328_free(chip);
	pci_disable_device(pci);

out:
	return err;
L
Linus Torvalds 已提交
1789 1790
}

1791 1792
static int __devinit
snd_azf3328_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
L
Linus Torvalds 已提交
1793 1794
{
	static int dev;
1795 1796 1797
	struct snd_card *card;
	struct snd_azf3328 *chip;
	struct snd_opl3 *opl3;
L
Linus Torvalds 已提交
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
	int err;

	snd_azf3328_dbgcallenter();
	if (dev >= SNDRV_CARDS)
		return -ENODEV;
	if (!enable[dev]) {
		dev++;
		return -ENOENT;
	}

	card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0 );
	if (card == NULL)
		return -ENOMEM;

	strcpy(card->driver, "AZF3328");
	strcpy(card->shortname, "Aztech AZF3328 (PCI168)");

        if ((err = snd_azf3328_create(card, pci, pci_id->driver_data, &chip)) < 0) {
1816
		goto out_err;
L
Linus Torvalds 已提交
1817 1818
	}

1819 1820
	card->private_data = chip;

L
Linus Torvalds 已提交
1821
	if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_MPU401,
1822 1823
				        chip->mpu_port, MPU401_INFO_INTEGRATED,
					pci->irq, 0, &chip->rmidi)) < 0) {
1824
		snd_printk(KERN_ERR "azf3328: no MPU-401 device at 0x%lx?\n", chip->mpu_port);
1825 1826 1827 1828 1829
		goto out_err;
	}

	if ((err = snd_azf3328_timer(chip, 0)) < 0) {
		goto out_err;
L
Linus Torvalds 已提交
1830 1831 1832
	}

	if ((err = snd_azf3328_pcm(chip, 0)) < 0) {
1833
		goto out_err;
L
Linus Torvalds 已提交
1834 1835 1836 1837
	}

	if (snd_opl3_create(card, chip->synth_port, chip->synth_port+2,
			    OPL3_HW_AUTO, 1, &opl3) < 0) {
1838
		snd_printk(KERN_ERR "azf3328: no OPL3 device at 0x%lx-0x%lx?\n",
L
Linus Torvalds 已提交
1839 1840 1841
			   chip->synth_port, chip->synth_port+2 );
	} else {
		if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
1842
			goto out_err;
L
Linus Torvalds 已提交
1843 1844 1845
		}
	}

1846 1847
	opl3->private_data = chip;

L
Linus Torvalds 已提交
1848 1849 1850 1851
	sprintf(card->longname, "%s at 0x%lx, irq %i",
		card->shortname, chip->codec_port, chip->irq);

	if ((err = snd_card_register(card)) < 0) {
1852
		goto out_err;
L
Linus Torvalds 已提交
1853 1854 1855 1856
	}

#ifdef MODULE
	printk(
1857 1858
"azt3328: Sound driver for Aztech AZF3328-based soundcards such as PCI168.\n"
"azt3328: Hardware was completely undocumented, unfortunately.\n"
1859 1860 1861
"azt3328: Feel free to contact andi AT lisas.de for bug reports etc.!\n"
"azt3328: User-scalable sequencer timer set to %dHz (1024000Hz / %d).\n",
	1024000 / seqtimer_scaling, seqtimer_scaling);
L
Linus Torvalds 已提交
1862 1863 1864
#endif

	if (snd_azf3328_config_joystick(chip, dev) < 0)
1865 1866
		snd_azf3328_io2_outb(chip, IDX_IO2_LEGACY_ADDR,
			      snd_azf3328_io2_inb(chip, IDX_IO2_LEGACY_ADDR) & ~LEGACY_JOY);
L
Linus Torvalds 已提交
1867 1868 1869 1870

	pci_set_drvdata(pci, card);
	dev++;

1871 1872 1873 1874 1875 1876 1877
	err = 0;
	goto out;
	
out_err:
	snd_card_free(card);
	
out:
L
Linus Torvalds 已提交
1878
	snd_azf3328_dbgcallleave();
1879
	return err;
L
Linus Torvalds 已提交
1880 1881
}

1882 1883
static void __devexit
snd_azf3328_remove(struct pci_dev *pci)
L
Linus Torvalds 已提交
1884 1885 1886 1887 1888 1889 1890
{
	snd_azf3328_dbgcallenter();
	snd_card_free(pci_get_drvdata(pci));
	pci_set_drvdata(pci, NULL);
	snd_azf3328_dbgcallleave();
}

1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
#ifdef CONFIG_PM
static int
snd_azf3328_suspend(struct pci_dev *pci, pm_message_t state)
{
	struct snd_card *card = pci_get_drvdata(pci);
	struct snd_azf3328 *chip = card->private_data;
	int reg;

	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
	
	snd_pcm_suspend_all(chip->pcm);

	for (reg = 0; reg < AZF_IO_SIZE_MIXER_PM / 2; reg++)
		chip->saved_regs_mixer[reg] = inw(chip->mixer_port + reg * 2);

	/* make sure to disable master volume etc. to prevent looping sound */
	snd_azf3328_mixer_set_mute(chip, IDX_MIXER_PLAY_MASTER, 1);
	snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 1);
	
	for (reg = 0; reg < AZF_IO_SIZE_CODEC_PM / 2; reg++)
		chip->saved_regs_codec[reg] = inw(chip->codec_port + reg * 2);
	for (reg = 0; reg < AZF_IO_SIZE_IO2_PM / 2; reg++)
		chip->saved_regs_io2[reg] = inw(chip->io2_port + reg * 2);
	for (reg = 0; reg < AZF_IO_SIZE_MPU_PM / 2; reg++)
		chip->saved_regs_mpu[reg] = inw(chip->mpu_port + reg * 2);
	for (reg = 0; reg < AZF_IO_SIZE_SYNTH_PM / 2; reg++)
		chip->saved_regs_synth[reg] = inw(chip->synth_port + reg * 2);

	pci_disable_device(pci);
	pci_save_state(pci);
1921
	pci_set_power_state(pci, pci_choose_state(pci, state));
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
	return 0;
}

static int
snd_azf3328_resume(struct pci_dev *pci)
{
	struct snd_card *card = pci_get_drvdata(pci);
	struct snd_azf3328 *chip = card->private_data;
	int reg;

	pci_set_power_state(pci, PCI_D0);
1933 1934 1935 1936 1937 1938 1939
	pci_restore_state(pci);
	if (pci_enable_device(pci) < 0) {
		printk(KERN_ERR "azt3328: pci_enable_device failed, "
		       "disabling device\n");
		snd_card_disconnect(card);
		return -EIO;
	}
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
	pci_set_master(pci);

	for (reg = 0; reg < AZF_IO_SIZE_IO2_PM / 2; reg++)
		outw(chip->saved_regs_io2[reg], chip->io2_port + reg * 2);
	for (reg = 0; reg < AZF_IO_SIZE_MPU_PM / 2; reg++)
		outw(chip->saved_regs_mpu[reg], chip->mpu_port + reg * 2);
	for (reg = 0; reg < AZF_IO_SIZE_SYNTH_PM / 2; reg++)
		outw(chip->saved_regs_synth[reg], chip->synth_port + reg * 2);
	for (reg = 0; reg < AZF_IO_SIZE_MIXER_PM / 2; reg++)
		outw(chip->saved_regs_mixer[reg], chip->mixer_port + reg * 2);
	for (reg = 0; reg < AZF_IO_SIZE_CODEC_PM / 2; reg++)
		outw(chip->saved_regs_codec[reg], chip->codec_port + reg * 2);

	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
	return 0;
}
#endif




L
Linus Torvalds 已提交
1961 1962 1963 1964 1965
static struct pci_driver driver = {
	.name = "AZF3328",
	.id_table = snd_azf3328_ids,
	.probe = snd_azf3328_probe,
	.remove = __devexit_p(snd_azf3328_remove),
1966 1967 1968 1969
#ifdef CONFIG_PM
	.suspend = snd_azf3328_suspend,
	.resume = snd_azf3328_resume,
#endif
L
Linus Torvalds 已提交
1970 1971
};

1972 1973
static int __init
alsa_card_azf3328_init(void)
L
Linus Torvalds 已提交
1974 1975 1976
{
	int err;
	snd_azf3328_dbgcallenter();
1977
	err = pci_register_driver(&driver);
L
Linus Torvalds 已提交
1978 1979 1980 1981
	snd_azf3328_dbgcallleave();
	return err;
}

1982 1983
static void __exit
alsa_card_azf3328_exit(void)
L
Linus Torvalds 已提交
1984 1985 1986 1987 1988 1989 1990 1991
{
	snd_azf3328_dbgcallenter();
	pci_unregister_driver(&driver);
	snd_azf3328_dbgcallleave();
}

module_init(alsa_card_azf3328_init)
module_exit(alsa_card_azf3328_exit)
反馈
建议
客服 返回
顶部