[ALSA] Add missing KERN_* suffix to printk
[linux-2.6] / sound / pci / azt3328.c
1 /*
2  *  azt3328.c - driver for Aztech AZF3328 based soundcards (e.g. PCI168).
3  *  Copyright (C) 2002 by Andreas Mohr <hw7oshyuv3001@sneakemail.com>
4  *
5  *  Framework borrowed from Bart Hartgers's als4000.c.
6  *  Driver developed on PCI168 AP(W) version (PCI rev. 10, subsystem ID 1801),
7  *  found in a Fujitsu-Siemens PC ("Cordant", aluminum case).
8  *  Other versions are:
9  *  PCI168 A(W), sub ID 1800
10  *  PCI168 A/AP, sub ID 8000
11  *  Please give me feedback in case you try my driver with one of these!!
12  *
13  * GPL LICENSE
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License as published by
16  *  the Free Software Foundation; either version 2 of the License, or
17  *  (at your option) any later version.
18  *
19  *  This program is distributed in the hope that it will be useful,
20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *  GNU General Public License for more details.
23
24  *  You should have received a copy of the GNU General Public License
25  *  along with this program; if not, write to the Free Software
26  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
27  *
28  * NOTES
29  *  Since Aztech does not provide any chipset documentation,
30  *  even on repeated request to various addresses,
31  *  and the answer that was finally given was negative
32  *  (and I was stupid enough to manage to get hold of a PCI168 soundcard
33  *  in the first place >:-P}),
34  *  I was forced to base this driver on reverse engineering
35  *  (3 weeks' worth of evenings filled with driver work).
36  *  (and no, I did NOT go the easy way: to pick up a PCI128 for 9 Euros)
37  *
38  *  The AZF3328 chip (note: AZF3328, *not* AZT3328, that's just the driver name
39  *  for compatibility reasons) has the following features:
40  *
41  *  - builtin AC97 conformant codec (SNR over 80dB)
42  *    (really AC97 compliant?? I really doubt it when looking
43  *    at the mixer register layout)
44  *  - builtin genuine OPL3
45  *  - full duplex 16bit playback/record at independent sampling rate
46  *  - MPU401 (+ legacy address support) FIXME: how to enable legacy addr??
47  *  - game port (legacy address support)
48  *  - built-in General DirectX timer having a 20 bits counter
49  *    with 1us resolution (FIXME: where is it?)
50  *  - I2S serial port for external DAC
51  *  - supports 33MHz PCI spec 2.1, PCI power management 1.0, compliant with ACPI
52  *  - supports hardware volume control
53  *  - single chip low cost solution (128 pin QFP)
54  *  - supports programmable Sub-vendor and Sub-system ID
55  *    required for Microsoft's logo compliance (FIXME: where?)
56  *  - PCI168 AP(W) card: power amplifier with 4 Watts/channel at 4 Ohms
57  *
58  *  Certain PCI versions of this card are susceptible to DMA traffic underruns
59  *  in some systems (resulting in sound crackling/clicking/popping),
60  *  probably because they don't have a DMA FIFO buffer or so.
61  *  Overview (PCI ID/PCI subID/PCI rev.):
62  *  - no DMA crackling on SiS735: 0x50DC/0x1801/16
63  *  - unknown performance: 0x50DC/0x1801/10
64  *  
65  *  Crackling happens with VIA chipsets or, in my case, an SiS735, which is
66  *  supposed to be very fast and supposed to get rid of crackling much
67  *  better than a VIA, yet ironically I still get crackling, like many other
68  *  people with the same chipset.
69  *  Possible remedies:
70  *  - plug card into a different PCI slot, preferrably one that isn't shared
71  *    too much (this helps a lot, but not completely!)
72  *  - get rid of PCI VGA card, use AGP instead
73  *  - upgrade or downgrade BIOS
74  *  - fiddle with PCI latency settings (setpci -v -s BUSID latency_timer=XX)
75  *    Not too helpful.
76  *  - Disable ACPI/power management/"Auto Detect RAM/PCI Clk" in BIOS
77  * 
78  * BUGS
79  *  - when Ctrl-C'ing mpg321, the playback loops a bit
80  *    (premature DMA playback reset?)
81  *  - full-duplex sometimes breaks (IRQ management issues?).
82  *    Once even a spontaneous REBOOT happened!!!
83  * 
84  * TODO
85  *  - test MPU401 MIDI playback etc.
86  *  - power management (CONFIG_PM). See e.g. intel8x0 or cs4281.
87  *    This would be nice since the chip runs a bit hot, and it's *required*
88  *    anyway for proper ACPI power management. In other words: rest
89  *    assured that I *will* implement this very soon; as soon as Linux 2.5.x
90  *    has power management that's bugfree enough to work properly on my desktop.
91  *  - figure out what all unknown port bits are responsible for
92  */
93
94 #include <sound/driver.h>
95 #include <asm/io.h>
96 #include <linux/init.h>
97 #include <linux/pci.h>
98 #include <linux/delay.h>
99 #include <linux/slab.h>
100 #include <linux/gameport.h>
101 #include <linux/moduleparam.h>
102 #include <sound/core.h>
103 #include <sound/control.h>
104 #include <sound/pcm.h>
105 #include <sound/rawmidi.h>
106 #include <sound/mpu401.h>
107 #include <sound/opl3.h>
108 #include <sound/initval.h>
109 #include "azt3328.h"
110
111 MODULE_AUTHOR("Andreas Mohr <hw7oshyuv3001@sneakemail.com>");
112 MODULE_DESCRIPTION("Aztech AZF3328 (PCI168)");
113 MODULE_LICENSE("GPL");
114 MODULE_SUPPORTED_DEVICE("{{Aztech,AZF3328}}");
115
116 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
117 #define SUPPORT_JOYSTICK 1
118 #endif
119
120 #define DEBUG_MISC      0
121 #define DEBUG_CALLS     0
122 #define DEBUG_MIXER     0
123 #define DEBUG_PLAY_REC  0
124 #define DEBUG_IO        0
125 #define MIXER_TESTING   0
126
127 #if DEBUG_MISC
128 #define snd_azf3328_dbgmisc(format, args...) printk(KERN_ERR format, ##args)
129 #else
130 #define snd_azf3328_dbgmisc(format, args...)
131 #endif          
132
133 #if DEBUG_CALLS
134 #define snd_azf3328_dbgcalls(format, args...) printk(format, ##args)
135 #define snd_azf3328_dbgcallenter() printk(KERN_ERR "entering %s\n", __FUNCTION__)
136 #define snd_azf3328_dbgcallleave() printk(KERN_ERR "leaving %s\n", __FUNCTION__)
137 #else
138 #define snd_azf3328_dbgcalls(format, args...)
139 #define snd_azf3328_dbgcallenter()
140 #define snd_azf3328_dbgcallleave()
141 #endif          
142
143 #if DEBUG_MIXER
144 #define snd_azf3328_dbgmixer(format, args...) printk(format, ##args)
145 #else
146 #define snd_azf3328_dbgmixer(format, args...)
147 #endif          
148
149 #if DEBUG_PLAY_REC
150 #define snd_azf3328_dbgplay(format, args...) printk(KERN_ERR format, ##args)
151 #else
152 #define snd_azf3328_dbgplay(format, args...)
153 #endif          
154
155 #if DEBUG_IO
156 #define snd_azf3328_dbgio(chip, where) \
157             printk(KERN_ERR "%s: IDX_IO_PLAY_FLAGS %04x, IDX_IO_PLAY_IRQMASK %04x, IDX_IO_IRQSTATUS %04x\n", where, inw(chip->codec_port+IDX_IO_PLAY_FLAGS), inw(chip->codec_port+IDX_IO_PLAY_IRQMASK), inw(chip->codec_port+IDX_IO_IRQSTATUS))
158 #else
159 #define snd_azf3328_dbgio(chip, where)
160 #endif
161             
162 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
163 module_param_array(index, int, NULL, 0444);
164 MODULE_PARM_DESC(index, "Index value for AZF3328 soundcard.");
165
166 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
167 module_param_array(id, charp, NULL, 0444);
168 MODULE_PARM_DESC(id, "ID string for AZF3328 soundcard.");
169
170 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;      /* Enable this card */
171 module_param_array(enable, bool, NULL, 0444);
172 MODULE_PARM_DESC(enable, "Enable AZF3328 soundcard.");
173
174 #ifdef SUPPORT_JOYSTICK
175 static int joystick[SNDRV_CARDS];
176 module_param_array(joystick, bool, NULL, 0444);
177 MODULE_PARM_DESC(joystick, "Enable joystick for AZF3328 soundcard.");
178 #endif
179
180 typedef struct _snd_azf3328 azf3328_t;
181
182 struct _snd_azf3328 {
183         int irq;
184
185         unsigned long codec_port;
186         unsigned long io2_port;
187         unsigned long mpu_port;
188         unsigned long synth_port;
189         unsigned long mixer_port;
190
191 #ifdef SUPPORT_JOYSTICK
192         struct gameport *gameport;
193 #endif
194
195         struct pci_dev *pci;
196         snd_card_t *card;
197
198         snd_pcm_t *pcm;
199         snd_rawmidi_t *rmidi;
200         snd_pcm_substream_t *playback_substream;
201         snd_pcm_substream_t *capture_substream;
202         unsigned int is_playing;
203         unsigned int is_recording;
204
205         spinlock_t reg_lock;
206 };
207
208 static struct pci_device_id snd_azf3328_ids[] = {
209         { 0x122D, 0x50DC, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },   /* PCI168/3328 */
210         { 0x122D, 0x80DA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },   /* 3328 */
211         { 0, }
212 };
213
214 MODULE_DEVICE_TABLE(pci, snd_azf3328_ids);
215
216 static inline void snd_azf3328_io2_write(azf3328_t *chip, int reg, unsigned char value)
217 {
218         outb(value, chip->io2_port + reg);
219 }
220
221 static inline unsigned char snd_azf3328_io2_read(azf3328_t *chip, int reg)
222 {
223         return inb(chip->io2_port + reg);
224 }
225
226 static void snd_azf3328_mixer_write(azf3328_t *chip, int reg, unsigned long value, int type)
227 {
228         switch(type) {
229         case WORD_VALUE:
230                 outw(value, chip->mixer_port + reg);
231                 break;
232         case DWORD_VALUE:
233                 outl(value, chip->mixer_port + reg);
234                 break;
235         case BYTE_VALUE:
236                 outb(value, chip->mixer_port + reg);
237                 break;
238         }
239 }
240
241 static void snd_azf3328_mixer_set_mute(azf3328_t *chip, int reg, int do_mute)
242 {
243         unsigned char oldval;
244
245         /* the mute bit is on the *second* (i.e. right) register of a
246          * left/right channel setting */
247         oldval = inb(chip->mixer_port + reg + 1);
248         if (do_mute)
249                 oldval |= 0x80;
250         else
251                 oldval &= ~0x80;
252         outb(oldval, chip->mixer_port + reg + 1);
253 }
254
255 static void snd_azf3328_mixer_write_volume_gradually(azf3328_t *chip, int reg, unsigned char dst_vol_left, unsigned char dst_vol_right, int chan_sel, int delay)
256 {
257         unsigned char curr_vol_left = 0, curr_vol_right = 0;
258         int left_done = 0, right_done = 0;
259         
260         snd_azf3328_dbgcallenter();
261         if (chan_sel & SET_CHAN_LEFT)
262                 curr_vol_left  = inb(chip->mixer_port + reg + 1);
263         else
264                 left_done = 1;
265         if (chan_sel & SET_CHAN_RIGHT)
266                 curr_vol_right = inb(chip->mixer_port + reg + 0);
267         else
268                 right_done = 1;
269         
270         /* take care of muting flag (0x80) contained in left channel */
271         if (curr_vol_left & 0x80)
272                 dst_vol_left |= 0x80;
273         else
274                 dst_vol_left &= ~0x80;
275
276         do
277         {
278                 if (!left_done)
279                 {
280                         if (curr_vol_left > dst_vol_left)
281                                 curr_vol_left--;
282                         else
283                         if (curr_vol_left < dst_vol_left)
284                                 curr_vol_left++;
285                         else
286                             left_done = 1;
287                         outb(curr_vol_left, chip->mixer_port + reg + 1);
288                 }
289                 if (!right_done)
290                 {
291                         if (curr_vol_right > dst_vol_right)
292                                 curr_vol_right--;
293                         else
294                         if (curr_vol_right < dst_vol_right)
295                                 curr_vol_right++;
296                         else
297                             right_done = 1;
298                         /* during volume change, the right channel is crackling
299                          * somewhat more than the left channel, unfortunately.
300                          * This seems to be a hardware issue. */
301                         outb(curr_vol_right, chip->mixer_port + reg + 0);
302                 }
303                 if (delay)
304                         mdelay(delay);
305         }
306         while ((!left_done) || (!right_done));
307         snd_azf3328_dbgcallleave();
308 }
309
310 /*
311  * general mixer element
312  */
313 typedef struct azf3328_mixer_reg {
314         unsigned int reg;
315         unsigned int lchan_shift, rchan_shift;
316         unsigned int mask;
317         unsigned int invert: 1;
318         unsigned int stereo: 1;
319         unsigned int enum_c: 4;
320 } azf3328_mixer_reg_t;
321
322 #define COMPOSE_MIXER_REG(reg,lchan_shift,rchan_shift,mask,invert,stereo,enum_c) \
323  ((reg) | (lchan_shift << 8) | (rchan_shift << 12) | (mask << 16) | (invert << 24) | (stereo << 25) | (enum_c << 26))
324
325 static void snd_azf3328_mixer_reg_decode(azf3328_mixer_reg_t *r, unsigned long val)
326 {
327         r->reg = val & 0xff;
328         r->lchan_shift = (val >> 8) & 0x0f;
329         r->rchan_shift = (val >> 12) & 0x0f;
330         r->mask = (val >> 16) & 0xff;
331         r->invert = (val >> 24) & 1;
332         r->stereo = (val >> 25) & 1;
333         r->enum_c = (val >> 26) & 0x0f;
334 }
335
336 /*
337  * mixer switches/volumes
338  */
339
340 #define AZF3328_MIXER_SWITCH(xname, reg, shift, invert) \
341 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
342   .info = snd_azf3328_info_mixer, \
343   .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
344   .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0x1, invert, 0, 0), \
345 }
346
347 #define AZF3328_MIXER_VOL_STEREO(xname, reg, mask, invert) \
348 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
349   .info = snd_azf3328_info_mixer, \
350   .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
351   .private_value = COMPOSE_MIXER_REG(reg, 8, 0, mask, invert, 1, 0), \
352 }
353
354 #define AZF3328_MIXER_VOL_MONO(xname, reg, mask, is_right_chan) \
355 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
356   .info = snd_azf3328_info_mixer, \
357   .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
358   .private_value = COMPOSE_MIXER_REG(reg, is_right_chan ? 0 : 8, 0, mask, 1, 0, 0), \
359 }
360
361 #define AZF3328_MIXER_VOL_SPECIAL(xname, reg, mask, shift, invert) \
362 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
363   .info = snd_azf3328_info_mixer, \
364   .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
365   .private_value = COMPOSE_MIXER_REG(reg, shift, 0, mask, invert, 0, 0), \
366 }
367
368 #define AZF3328_MIXER_ENUM(xname, reg, enum_c, shift) \
369 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
370   .info = snd_azf3328_info_mixer_enum, \
371   .get = snd_azf3328_get_mixer_enum, .put = snd_azf3328_put_mixer_enum, \
372   .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0, 0, 0, enum_c), \
373 }
374
375 static int snd_azf3328_info_mixer(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
376 {
377         azf3328_mixer_reg_t reg;
378
379         snd_azf3328_dbgcallenter();
380         snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
381         uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
382         uinfo->count = reg.stereo + 1;
383         uinfo->value.integer.min = 0;
384         uinfo->value.integer.max = reg.mask;
385         snd_azf3328_dbgcallleave();
386         return 0;
387 }
388
389 static int snd_azf3328_get_mixer(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
390 {
391         azf3328_t *chip = snd_kcontrol_chip(kcontrol);
392         azf3328_mixer_reg_t reg;
393         unsigned int oreg, val;
394
395         snd_azf3328_dbgcallenter();
396         snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
397
398         oreg = inw(chip->mixer_port + reg.reg);
399         val = (oreg >> reg.lchan_shift) & reg.mask;
400         if (reg.invert)
401                 val = reg.mask - val;
402         ucontrol->value.integer.value[0] = val;
403         if (reg.stereo) {
404                 val = (oreg >> reg.rchan_shift) & reg.mask;
405                 if (reg.invert)
406                         val = reg.mask - val;
407                 ucontrol->value.integer.value[1] = val;
408         }
409         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);
410         snd_azf3328_dbgcallleave();
411         return 0;
412 }
413
414 static int snd_azf3328_put_mixer(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
415 {
416         azf3328_t *chip = snd_kcontrol_chip(kcontrol);
417         azf3328_mixer_reg_t reg;
418         unsigned int oreg, nreg, val;
419
420         snd_azf3328_dbgcallenter();
421         snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
422         oreg = inw(chip->mixer_port + reg.reg);
423         val = ucontrol->value.integer.value[0] & reg.mask;
424         if (reg.invert)
425                 val = reg.mask - val;
426         nreg = oreg & ~(reg.mask << reg.lchan_shift);
427         nreg |= (val << reg.lchan_shift);
428         if (reg.stereo) {
429                 val = ucontrol->value.integer.value[1] & reg.mask;
430                 if (reg.invert)
431                         val = reg.mask - val;
432                 nreg &= ~(reg.mask << reg.rchan_shift);
433                 nreg |= (val << reg.rchan_shift);
434         }
435         if (reg.mask >= 0x07) /* it's a volume control, so better take care */
436                 snd_azf3328_mixer_write_volume_gradually(chip, reg.reg, nreg >> 8, nreg & 0xff, SET_CHAN_LEFT|SET_CHAN_RIGHT, 0); /* just set both channels, doesn't matter */
437         else
438                 outw(nreg, chip->mixer_port + reg.reg);
439
440         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, inw(chip->mixer_port + reg.reg));
441         snd_azf3328_dbgcallleave();
442         return (nreg != oreg);
443 }
444
445 static int snd_azf3328_info_mixer_enum(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
446 {
447         azf3328_mixer_reg_t reg;
448         static char *texts1[2] = { "ModemOut1", "ModemOut2" };
449         static char *texts2[2] = { "MonoSelectSource1", "MonoSelectSource2" };
450         static char *texts3[8] = {
451                 "Mic", "CD", "Video", "Aux", "Line",
452                 "Mix", "Mix Mono", "Phone"
453         };
454
455         snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
456         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
457         uinfo->count = (reg.reg == IDX_MIXER_REC_SELECT) ? 2 : 1;
458         uinfo->value.enumerated.items = reg.enum_c;
459         if (uinfo->value.enumerated.item > reg.enum_c - 1U)
460                 uinfo->value.enumerated.item = reg.enum_c - 1U;
461         if (reg.reg == IDX_MIXER_ADVCTL2)
462         {
463                 if (reg.lchan_shift == 8) /* modem out sel */
464                         strcpy(uinfo->value.enumerated.name, texts1[uinfo->value.enumerated.item]);
465                 else /* mono sel source */
466                         strcpy(uinfo->value.enumerated.name, texts2[uinfo->value.enumerated.item]);
467         }
468         else
469                 strcpy(uinfo->value.enumerated.name, texts3[uinfo->value.enumerated.item]
470 );
471         return 0;
472 }
473
474 static int snd_azf3328_get_mixer_enum(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
475 {
476         azf3328_mixer_reg_t reg;
477         azf3328_t *chip = snd_kcontrol_chip(kcontrol);
478         unsigned short val;
479         
480         snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
481         val = inw(chip->mixer_port + reg.reg);
482         if (reg.reg == IDX_MIXER_REC_SELECT)
483         {
484                 ucontrol->value.enumerated.item[0] = (val >> 8) & (reg.enum_c - 1);
485                 ucontrol->value.enumerated.item[1] = (val >> 0) & (reg.enum_c - 1);
486         }
487         else
488                 ucontrol->value.enumerated.item[0] = (val >> reg.lchan_shift) & (reg.enum_c - 1);
489         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);
490         return 0;
491 }
492
493 static int snd_azf3328_put_mixer_enum(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
494 {
495         azf3328_mixer_reg_t reg;
496         azf3328_t *chip = snd_kcontrol_chip(kcontrol);
497         unsigned int oreg, nreg, val;
498         
499         snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
500         oreg = inw(chip->mixer_port + reg.reg);
501         val = oreg;
502         if (reg.reg == IDX_MIXER_REC_SELECT)
503         {
504                 if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U ||
505                 ucontrol->value.enumerated.item[1] > reg.enum_c - 1U)
506                         return -EINVAL;
507                 val = (ucontrol->value.enumerated.item[0] << 8) |
508                       (ucontrol->value.enumerated.item[1] << 0);
509         }
510         else
511         {
512                 if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U)
513                         return -EINVAL;
514                 val &= ~((reg.enum_c - 1) << reg.lchan_shift);
515                 val |= (ucontrol->value.enumerated.item[0] << reg.lchan_shift);
516         }
517         outw(val, chip->mixer_port + reg.reg);
518         nreg = val;
519
520         snd_azf3328_dbgmixer("put_enum: %02x to %04x, oreg %04x\n", reg.reg, val, oreg);
521         return (nreg != oreg);
522 }
523
524 static snd_kcontrol_new_t snd_azf3328_mixer_controls[] __devinitdata = {
525         AZF3328_MIXER_SWITCH("Master Playback Switch", IDX_MIXER_PLAY_MASTER, 15, 1),
526         AZF3328_MIXER_VOL_STEREO("Master Playback Volume", IDX_MIXER_PLAY_MASTER, 0x1f, 1),
527         AZF3328_MIXER_SWITCH("Wave Playback Switch", IDX_MIXER_WAVEOUT, 15, 1),
528         AZF3328_MIXER_VOL_STEREO("Wave Playback Volume", IDX_MIXER_WAVEOUT, 0x1f, 1),
529         AZF3328_MIXER_SWITCH("Wave Playback 3D Bypass", IDX_MIXER_ADVCTL2, 7, 1),
530         AZF3328_MIXER_SWITCH("FM Playback Switch", IDX_MIXER_FMSYNTH, 15, 1),
531         AZF3328_MIXER_VOL_STEREO("FM Playback Volume", IDX_MIXER_FMSYNTH, 0x1f, 1),
532         AZF3328_MIXER_SWITCH("CD Playback Switch", IDX_MIXER_CDAUDIO, 15, 1),
533         AZF3328_MIXER_VOL_STEREO("CD Playback Volume", IDX_MIXER_CDAUDIO, 0x1f, 1),
534         AZF3328_MIXER_SWITCH("Capture Switch", IDX_MIXER_REC_VOLUME, 15, 1),
535         AZF3328_MIXER_VOL_STEREO("Capture Volume", IDX_MIXER_REC_VOLUME, 0x0f, 0),
536         AZF3328_MIXER_ENUM("Capture Source", IDX_MIXER_REC_SELECT, 8, 0),
537         AZF3328_MIXER_SWITCH("Mic Playback Switch", IDX_MIXER_MIC, 15, 1),
538         AZF3328_MIXER_VOL_MONO("Mic Playback Volume", IDX_MIXER_MIC, 0x1f, 1),
539         AZF3328_MIXER_SWITCH("Mic Boost (+20dB)", IDX_MIXER_MIC, 6, 0),
540         AZF3328_MIXER_SWITCH("Line Playback Switch", IDX_MIXER_LINEIN, 15, 1),
541         AZF3328_MIXER_VOL_STEREO("Line Playback Volume", IDX_MIXER_LINEIN, 0x1f, 1),
542         AZF3328_MIXER_SWITCH("PCBeep Playback Switch", IDX_MIXER_PCBEEP, 15, 1),
543         AZF3328_MIXER_VOL_SPECIAL("PCBeep Playback Volume", IDX_MIXER_PCBEEP, 0x0f, 1, 1),
544         AZF3328_MIXER_SWITCH("Video Playback Switch", IDX_MIXER_VIDEO, 15, 1),
545         AZF3328_MIXER_VOL_STEREO("Video Playback Volume", IDX_MIXER_VIDEO, 0x1f, 1),
546         AZF3328_MIXER_SWITCH("Aux Playback Switch", IDX_MIXER_AUX, 15, 1),
547         AZF3328_MIXER_VOL_STEREO("Aux Playback Volume", IDX_MIXER_AUX, 0x1f, 1),
548         AZF3328_MIXER_SWITCH("Modem Playback Switch", IDX_MIXER_MODEMOUT, 15, 1),
549         AZF3328_MIXER_VOL_MONO("Modem Playback Volume", IDX_MIXER_MODEMOUT, 0x1f, 1),
550         AZF3328_MIXER_SWITCH("Modem Capture Switch", IDX_MIXER_MODEMIN, 15, 1),
551         AZF3328_MIXER_VOL_MONO("Modem Capture Volume", IDX_MIXER_MODEMIN, 0x1f, 1),
552         AZF3328_MIXER_ENUM("Modem Out Select", IDX_MIXER_ADVCTL2, 2, 8),
553         AZF3328_MIXER_ENUM("Mono Select Source", IDX_MIXER_ADVCTL2, 2, 9),
554         AZF3328_MIXER_VOL_SPECIAL("Tone Control - Treble", IDX_MIXER_BASSTREBLE, 0x07, 1, 0),
555         AZF3328_MIXER_VOL_SPECIAL("Tone Control - Bass", IDX_MIXER_BASSTREBLE, 0x07, 9, 0),
556         AZF3328_MIXER_SWITCH("3D Control - Toggle", IDX_MIXER_ADVCTL2, 13, 0),
557         AZF3328_MIXER_VOL_SPECIAL("3D Control - Volume", IDX_MIXER_ADVCTL1, 0x07, 1, 0), /* "3D Width" */
558         AZF3328_MIXER_VOL_SPECIAL("3D Control - Space", IDX_MIXER_ADVCTL1, 0x03, 8, 0), /* "Hifi 3D" */
559 #if MIXER_TESTING
560         AZF3328_MIXER_SWITCH("0", IDX_MIXER_ADVCTL2, 0, 0),
561         AZF3328_MIXER_SWITCH("1", IDX_MIXER_ADVCTL2, 1, 0),
562         AZF3328_MIXER_SWITCH("2", IDX_MIXER_ADVCTL2, 2, 0),
563         AZF3328_MIXER_SWITCH("3", IDX_MIXER_ADVCTL2, 3, 0),
564         AZF3328_MIXER_SWITCH("4", IDX_MIXER_ADVCTL2, 4, 0),
565         AZF3328_MIXER_SWITCH("5", IDX_MIXER_ADVCTL2, 5, 0),
566         AZF3328_MIXER_SWITCH("6", IDX_MIXER_ADVCTL2, 6, 0),
567         AZF3328_MIXER_SWITCH("7", IDX_MIXER_ADVCTL2, 7, 0),
568         AZF3328_MIXER_SWITCH("8", IDX_MIXER_ADVCTL2, 8, 0),
569         AZF3328_MIXER_SWITCH("9", IDX_MIXER_ADVCTL2, 9, 0),
570         AZF3328_MIXER_SWITCH("10", IDX_MIXER_ADVCTL2, 10, 0),
571         AZF3328_MIXER_SWITCH("11", IDX_MIXER_ADVCTL2, 11, 0),
572         AZF3328_MIXER_SWITCH("12", IDX_MIXER_ADVCTL2, 12, 0),
573         AZF3328_MIXER_SWITCH("13", IDX_MIXER_ADVCTL2, 13, 0),
574         AZF3328_MIXER_SWITCH("14", IDX_MIXER_ADVCTL2, 14, 0),
575         AZF3328_MIXER_SWITCH("15", IDX_MIXER_ADVCTL2, 15, 0),
576 #endif
577 };
578
579 #define AZF3328_INIT_VALUES (sizeof(snd_azf3328_init_values)/sizeof(unsigned int)/2)
580
581 static unsigned int snd_azf3328_init_values[][2] = {
582         { IDX_MIXER_PLAY_MASTER,        MIXER_MUTE_MASK|0x1f1f },
583         { IDX_MIXER_MODEMOUT,           MIXER_MUTE_MASK|0x1f1f },
584         { IDX_MIXER_BASSTREBLE,         0x0000 },
585         { IDX_MIXER_PCBEEP,             MIXER_MUTE_MASK|0x1f1f },
586         { IDX_MIXER_MODEMIN,            MIXER_MUTE_MASK|0x1f1f },
587         { IDX_MIXER_MIC,                MIXER_MUTE_MASK|0x001f },
588         { IDX_MIXER_LINEIN,             MIXER_MUTE_MASK|0x1f1f },
589         { IDX_MIXER_CDAUDIO,            MIXER_MUTE_MASK|0x1f1f },
590         { IDX_MIXER_VIDEO,              MIXER_MUTE_MASK|0x1f1f },
591         { IDX_MIXER_AUX,                MIXER_MUTE_MASK|0x1f1f },
592         { IDX_MIXER_WAVEOUT,            MIXER_MUTE_MASK|0x1f1f },
593         { IDX_MIXER_FMSYNTH,            MIXER_MUTE_MASK|0x1f1f },
594         { IDX_MIXER_REC_VOLUME,         MIXER_MUTE_MASK|0x0707 },
595 };
596
597 static int __devinit snd_azf3328_mixer_new(azf3328_t *chip)
598 {
599         snd_card_t *card;
600         snd_kcontrol_new_t *sw;
601         unsigned int idx;
602         int err;
603
604         snd_azf3328_dbgcallenter();
605         snd_assert(chip != NULL && chip->card != NULL, return -EINVAL);
606
607         card = chip->card;
608
609         /* mixer reset */
610         snd_azf3328_mixer_write(chip, IDX_MIXER_RESET, 0x0, WORD_VALUE);
611
612         /* mute and zero volume channels */
613         for (idx = 0; idx < AZF3328_INIT_VALUES; idx++) {
614                 snd_azf3328_mixer_write(chip, snd_azf3328_init_values[idx][0], snd_azf3328_init_values[idx][1], WORD_VALUE);
615         }
616         
617         /* add mixer controls */
618         sw = snd_azf3328_mixer_controls;
619         for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_mixer_controls); idx++, sw++) {
620                 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(sw, chip))) < 0)
621                         return err;
622         }
623         snd_component_add(card, "AZF3328 mixer");
624         strcpy(card->mixername, "AZF3328 mixer");
625
626         snd_azf3328_dbgcallleave();
627         return 0;
628 }
629
630 static int snd_azf3328_hw_params(snd_pcm_substream_t * substream,
631                                  snd_pcm_hw_params_t * hw_params)
632 {
633         int res;
634         snd_azf3328_dbgcallenter();
635         res = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
636         snd_azf3328_dbgcallleave();
637         return res;
638 }
639
640 static int snd_azf3328_hw_free(snd_pcm_substream_t * substream)
641 {
642         snd_azf3328_dbgcallenter();
643         snd_pcm_lib_free_pages(substream);
644         snd_azf3328_dbgcallleave();
645         return 0;
646 }
647
648 static void snd_azf3328_setfmt(azf3328_t *chip,
649                                unsigned int reg,
650                                unsigned int bitrate,
651                                unsigned int format_width,
652                                unsigned int channels
653 )
654 {
655         unsigned int val = 0xff00;
656         unsigned long flags;
657
658         snd_azf3328_dbgcallenter();
659         switch (bitrate) {
660         case  5512: val |= 0x0d; break; /* the AZF3328 names it "5510" for some strange reason */
661         case  6620: val |= 0x0b; break;
662         case  8000: val |= 0x00; break;
663         case  9600: val |= 0x08; break;
664         case 11025: val |= 0x01; break;
665         case 16000: val |= 0x02; break;
666         case 22050: val |= 0x03; break;
667         case 32000: val |= 0x04; break;
668         case 44100: val |= 0x05; break;
669         case 48000: val |= 0x06; break;
670         case 64000: val |= 0x07; break;
671         default:
672                 snd_printk(KERN_WARNING "unknown bitrate %d, assuming 44.1kHz!\n", bitrate);
673                 val |= 0x05; /* 44100 */
674                 break;
675         }
676         /* val = 0xff07; 3m27.993s (65301Hz; -> 64000Hz???) */
677         /* val = 0xff09; 17m15.098s (13123,478Hz; -> 12000Hz???) */
678         /* val = 0xff0a; 47m30.599s (4764,891Hz; -> 4800Hz???) */
679         /* val = 0xff0c; 57m0.510s (4010,263Hz; -> 4000Hz???) */
680         /* val = 0xff05; 5m11.556s (... -> 44100Hz) */
681         /* val = 0xff03; 10m21.529s (21872,463Hz; -> 22050Hz???) */
682         /* val = 0xff0f; 20m41.883s (10937,993Hz; -> 11025Hz???) */
683         /* val = 0xff0d; 41m23.135s (5523,600Hz; -> 5512Hz???) */
684         /* val = 0xff0e; 28m30.777s (8017Hz; -> 8000Hz???) */
685         if (channels == 2)
686                 val |= SOUNDFORMAT_FLAG_2CHANNELS;
687
688         if (format_width == 16)
689                 val |= SOUNDFORMAT_FLAG_16BIT;
690
691         spin_lock_irqsave(&chip->reg_lock, flags);
692         
693         /* set bitrate/format */
694         outw(val, chip->codec_port+reg);
695         
696         /* changing the bitrate/format settings switches off the
697          * audio output with an annoying click in case of 8/16bit format change
698          * (maybe shutting down DAC/ADC?), thus immediately
699          * do some tweaking to reenable it and get rid of the clicking
700          * (FIXME: yes, it works, but what exactly am I doing here?? :)
701          * FIXME: does this have some side effects for full-duplex
702          * or other dramatic side effects? */
703         if (reg == IDX_IO_PLAY_SOUNDFORMAT) /* only do it for playback */
704                 outw(inw(chip->codec_port + IDX_IO_PLAY_FLAGS)|DMA_PLAY_SOMETHING1|DMA_PLAY_SOMETHING2|SOMETHING_ALMOST_ALWAYS_SET|DMA_EPILOGUE_SOMETHING|DMA_SOMETHING_ELSE, chip->codec_port + IDX_IO_PLAY_FLAGS);
705
706         spin_unlock_irqrestore(&chip->reg_lock, flags);
707         snd_azf3328_dbgcallleave();
708 }
709
710 static void snd_azf3328_setdmaa(azf3328_t *chip,
711                                 long unsigned int addr,
712                                 unsigned int count,
713                                 unsigned int size,
714                                 int do_recording)
715 {
716         long unsigned int addr1;
717         long unsigned int addr2;
718         unsigned int count1;
719         unsigned int count2;
720         unsigned long flags;
721         int reg_offs = do_recording ? 0x20 : 0x00;
722
723         snd_azf3328_dbgcallenter();
724         /* AZF3328 uses a two buffer pointer DMA playback approach */
725         if (!chip->is_playing)
726         {
727                 addr1 = addr;
728                 addr2 = addr+(size/2);
729                 count1 = (size/2)-1;
730                 count2 = (size/2)-1;
731 #if DEBUG_PLAY_REC
732                 snd_azf3328_dbgplay("setting dma: buf1 %08lx[%d], buf2 %08lx[%d]\n", addr1, count1, addr2, count2);
733 #endif
734                 spin_lock_irqsave(&chip->reg_lock, flags);
735                 outl(addr1, chip->codec_port+reg_offs+IDX_IO_PLAY_DMA_START_1);
736                 outl(addr2, chip->codec_port+reg_offs+IDX_IO_PLAY_DMA_START_2);
737                 outw(count1, chip->codec_port+reg_offs+IDX_IO_PLAY_DMA_LEN_1);
738                 outw(count2, chip->codec_port+reg_offs+IDX_IO_PLAY_DMA_LEN_2);
739                 spin_unlock_irqrestore(&chip->reg_lock, flags);
740         }
741         snd_azf3328_dbgcallleave();
742 }
743
744 static int snd_azf3328_playback_prepare(snd_pcm_substream_t *substream)
745 {
746 #if 0
747         azf3328_t *chip = snd_pcm_substream_chip(substream);
748         snd_pcm_runtime_t *runtime = substream->runtime;
749         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
750         unsigned int count = snd_pcm_lib_period_bytes(substream);
751 #endif
752
753         snd_azf3328_dbgcallenter();
754 #if 0
755         snd_azf3328_setfmt(chip, IDX_IO_PLAY_SOUNDFORMAT, runtime->rate, snd_pcm_format_width(runtime->format), runtime->channels);
756         snd_azf3328_setdmaa(chip, runtime->dma_addr, count, size, 0);
757 #endif
758         snd_azf3328_dbgcallleave();
759         return 0;
760 }
761
762 static int snd_azf3328_capture_prepare(snd_pcm_substream_t * substream)
763 {
764 #if 0
765         azf3328_t *chip = snd_pcm_substream_chip(substream);
766         snd_pcm_runtime_t *runtime = substream->runtime;
767         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
768         unsigned int count = snd_pcm_lib_period_bytes(substream);
769 #endif
770
771         snd_azf3328_dbgcallenter();
772 #if 0
773         snd_azf3328_setfmt(chip, IDX_IO_REC_SOUNDFORMAT, runtime->rate, snd_pcm_format_width(runtime->format), runtime->channels);
774         snd_azf3328_setdmaa(chip, runtime->dma_addr, count, size, 1);
775 #endif
776         snd_azf3328_dbgcallleave();
777         return 0;
778 }
779
780 static int snd_azf3328_playback_trigger(snd_pcm_substream_t * substream, int cmd)
781 {
782         azf3328_t *chip = snd_pcm_substream_chip(substream);
783         snd_pcm_runtime_t *runtime = substream->runtime;
784         int result = 0;
785         unsigned int status1;
786
787         snd_azf3328_dbgcalls("snd_azf3328_playback_trigger cmd %d\n", cmd);
788         switch (cmd) {
789         case SNDRV_PCM_TRIGGER_START:
790
791                 snd_azf3328_dbgio(chip, "trigger1");
792
793                 /* mute WaveOut */
794                 snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 1);
795
796                 snd_azf3328_setfmt(chip, IDX_IO_PLAY_SOUNDFORMAT, runtime->rate, snd_pcm_format_width(runtime->format), runtime->channels);
797
798                 spin_lock(&chip->reg_lock);
799                 /* stop playback */
800                 status1 = inw(chip->codec_port+IDX_IO_PLAY_FLAGS);
801                 status1 &= ~DMA_RESUME;
802                 outw(status1, chip->codec_port+IDX_IO_PLAY_FLAGS);
803             
804                 /* FIXME: clear interrupts or what??? */
805                 outw(0xffff, chip->codec_port+IDX_IO_PLAY_IRQMASK);
806                 spin_unlock(&chip->reg_lock);
807
808                 snd_azf3328_setdmaa(chip, runtime->dma_addr, snd_pcm_lib_period_bytes(substream), snd_pcm_lib_buffer_bytes(substream), 0);
809
810                 spin_lock(&chip->reg_lock);
811 #ifdef WIN9X
812                 /* FIXME: enable playback/recording??? */
813                 status1 |= DMA_PLAY_SOMETHING1 | DMA_PLAY_SOMETHING2;
814                 outw(status1, chip->codec_port+IDX_IO_PLAY_FLAGS);
815
816                 /* start playback again */
817                 /* FIXME: what is this value (0x0010)??? */
818                 status1 |= DMA_RESUME | DMA_EPILOGUE_SOMETHING;
819                 outw(status1, chip->codec_port+IDX_IO_PLAY_FLAGS);
820 #else /* NT4 */
821                 outw(0x00, chip->codec_port+IDX_IO_PLAY_FLAGS);
822                 outw(DMA_PLAY_SOMETHING1, chip->codec_port+IDX_IO_PLAY_FLAGS);
823                 outw(DMA_PLAY_SOMETHING1|DMA_PLAY_SOMETHING2, chip->codec_port+IDX_IO_PLAY_FLAGS);
824                 outw(DMA_RESUME|SOMETHING_ALMOST_ALWAYS_SET|DMA_EPILOGUE_SOMETHING|DMA_SOMETHING_ELSE, chip->codec_port+IDX_IO_PLAY_FLAGS);
825 #endif
826                 spin_unlock(&chip->reg_lock);
827
828                 /* now unmute WaveOut */
829                 snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 0);
830
831                 snd_azf3328_dbgio(chip, "trigger2");
832                 chip->is_playing = 1;
833                 break;
834         case SNDRV_PCM_TRIGGER_STOP:
835                 /* mute WaveOut */
836                 snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 1);
837
838                 spin_lock(&chip->reg_lock);
839                 /* stop playback */
840                 status1 = inw(chip->codec_port+IDX_IO_PLAY_FLAGS);
841
842                 status1 &= ~DMA_RESUME;
843                 outw(status1, chip->codec_port+IDX_IO_PLAY_FLAGS);
844
845                 status1 |= DMA_PLAY_SOMETHING1;
846                 outw(status1, chip->codec_port+IDX_IO_PLAY_FLAGS);
847
848                 status1 &= ~DMA_PLAY_SOMETHING1;
849                 outw(status1, chip->codec_port+IDX_IO_PLAY_FLAGS);
850                 spin_unlock(&chip->reg_lock);
851             
852                 /* now unmute WaveOut */
853                 snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 0);
854                 chip->is_playing = 0;
855                 break;
856         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
857                 snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_PUSH NIY!\n");
858                 break;
859         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
860                 snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_RELEASE NIY!\n");
861                 break;
862         default:
863                 return -EINVAL;
864         }
865         
866         snd_azf3328_dbgcallleave();
867         return result;
868 }
869
870 /* this is just analogous to playback; I'm not quite sure whether recording
871  * should actually be triggered like that */
872 static int snd_azf3328_capture_trigger(snd_pcm_substream_t * substream, int cmd)
873 {
874         azf3328_t *chip = snd_pcm_substream_chip(substream);
875         snd_pcm_runtime_t *runtime = substream->runtime;
876         int result = 0;
877         unsigned int status1;
878
879         snd_azf3328_dbgcalls("snd_azf3328_capture_trigger cmd %d\n", cmd);
880         switch (cmd) {
881         case SNDRV_PCM_TRIGGER_START:
882
883                 snd_azf3328_dbgio(chip, "trigger1");
884
885                 snd_azf3328_setfmt(chip, IDX_IO_REC_SOUNDFORMAT, runtime->rate, snd_pcm_format_width(runtime->format), runtime->channels);
886
887                 spin_lock(&chip->reg_lock);
888                 /* stop recording */
889                 status1 = inw(chip->codec_port+IDX_IO_REC_FLAGS);
890                 status1 &= ~DMA_RESUME;
891                 outw(status1, chip->codec_port+IDX_IO_REC_FLAGS);
892             
893                 /* FIXME: clear interrupts or what??? */
894                 outw(0xffff, chip->codec_port+IDX_IO_REC_IRQMASK);
895                 spin_unlock(&chip->reg_lock);
896
897                 snd_azf3328_setdmaa(chip, runtime->dma_addr, snd_pcm_lib_period_bytes(substream), snd_pcm_lib_buffer_bytes(substream), 1);
898
899                 spin_lock(&chip->reg_lock);
900 #ifdef WIN9X
901                 /* FIXME: enable playback/recording??? */
902                 status1 |= DMA_PLAY_SOMETHING1 | DMA_PLAY_SOMETHING2;
903                 outw(status1, chip->codec_port+IDX_IO_REC_FLAGS);
904
905                 /* start playback again */
906                 /* FIXME: what is this value (0x0010)??? */
907                 status1 |= DMA_RESUME | DMA_EPILOGUE_SOMETHING;
908                 outw(status1, chip->codec_port+IDX_IO_REC_FLAGS);
909 #else
910                 outw(0x00, chip->codec_port+IDX_IO_REC_FLAGS);
911                 outw(DMA_PLAY_SOMETHING1, chip->codec_port+IDX_IO_REC_FLAGS);
912                 outw(DMA_PLAY_SOMETHING1|DMA_PLAY_SOMETHING2, chip->codec_port+IDX_IO_REC_FLAGS);
913                 outw(DMA_RESUME|SOMETHING_ALMOST_ALWAYS_SET|DMA_EPILOGUE_SOMETHING|DMA_SOMETHING_ELSE, chip->codec_port+IDX_IO_REC_FLAGS);
914 #endif
915                 spin_unlock(&chip->reg_lock);
916
917                 snd_azf3328_dbgio(chip, "trigger2");
918                 chip->is_playing = 1;
919                 break;
920         case SNDRV_PCM_TRIGGER_STOP:
921                 spin_lock(&chip->reg_lock);
922                 /* stop recording */
923                 status1 = inw(chip->codec_port+IDX_IO_REC_FLAGS);
924
925                 status1 &= ~DMA_RESUME;
926                 outw(status1, chip->codec_port+IDX_IO_REC_FLAGS);
927
928                 status1 |= DMA_PLAY_SOMETHING1;
929                 outw(status1, chip->codec_port+IDX_IO_REC_FLAGS);
930
931                 status1 &= ~DMA_PLAY_SOMETHING1;
932                 outw(status1, chip->codec_port+IDX_IO_REC_FLAGS);
933                 spin_unlock(&chip->reg_lock);
934             
935                 chip->is_playing = 0;
936                 break;
937         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
938                 snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_PUSH NIY!\n");
939                 break;
940         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
941                 snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_RELEASE NIY!\n");
942                 break;
943         default:
944                 return -EINVAL;
945         }
946         
947         snd_azf3328_dbgcallleave();
948         return result;
949 }
950
951 static snd_pcm_uframes_t snd_azf3328_playback_pointer(snd_pcm_substream_t * substream)
952 {
953         azf3328_t *chip = snd_pcm_substream_chip(substream);
954         unsigned long bufptr, playptr;
955         unsigned long result;
956         snd_pcm_uframes_t frmres;
957
958 #ifdef QUERY_HARDWARE
959         bufptr = inl(chip->codec_port+IDX_IO_PLAY_DMA_START_1);
960 #else
961         bufptr = substream->runtime->dma_addr;
962 #endif
963         playptr = inl(chip->codec_port+IDX_IO_PLAY_DMA_CURRPOS);
964
965         result = playptr - bufptr;
966         frmres = bytes_to_frames( substream->runtime, result );
967         snd_azf3328_dbgplay("result %lx, playptr %lx (base %x), frames %ld\n", result, playptr, substream->runtime->dma_addr, frmres);
968         return frmres;
969 }
970
971 static snd_pcm_uframes_t snd_azf3328_capture_pointer(snd_pcm_substream_t * substream)
972 {
973         azf3328_t *chip = snd_pcm_substream_chip(substream);
974         unsigned long bufptr, recptr;
975         unsigned long result;
976         snd_pcm_uframes_t frmres;
977
978 #ifdef QUERY_HARDWARE
979         bufptr = inl(chip->codec_port+IDX_IO_REC_DMA_START_1);
980 #else
981         bufptr = substream->runtime->dma_addr;
982 #endif
983         recptr = inl(chip->codec_port+IDX_IO_REC_DMA_CURRPOS);
984
985         result = recptr - bufptr;
986         frmres = bytes_to_frames( substream->runtime, result );
987         snd_azf3328_dbgplay("result %lx, rec ptr %lx (base %x), frames %ld\n", result, recptr, substream->runtime->dma_addr, frmres);
988         return frmres;
989 }
990
991 static irqreturn_t snd_azf3328_interrupt(int irq, void *dev_id, struct pt_regs *regs)
992 {
993         azf3328_t *chip = dev_id;
994         unsigned int status, which;
995         static unsigned long count;
996
997         status  = inw(chip->codec_port+IDX_IO_IRQSTATUS);
998
999         /* fast path out, to ease interrupt sharing */
1000         if (!(status & (IRQ_PLAYBACK|IRQ_RECORDING|IRQ_MPU401|IRQ_SOMEIRQ)))
1001                 return IRQ_NONE; /* must be interrupt for another device */
1002
1003         snd_azf3328_dbgplay("Interrupt %ld!\nIDX_IO_PLAY_FLAGS %04x, IDX_IO_PLAY_IRQMASK %04x, IDX_IO_IRQSTATUS %04x\n", count, inw(chip->codec_port+IDX_IO_PLAY_FLAGS), inw(chip->codec_port+IDX_IO_PLAY_IRQMASK), inw(chip->codec_port+IDX_IO_IRQSTATUS));
1004                 
1005         if (status & IRQ_PLAYBACK)
1006         {
1007                 spin_lock(&chip->reg_lock);
1008                 which = inw(chip->codec_port+IDX_IO_PLAY_IRQMASK);
1009                 if (which & IRQ_FINISHED_PLAYBUF_1)
1010                         /* ack IRQ */
1011                         outw(which | IRQ_FINISHED_PLAYBUF_1, chip->codec_port+IDX_IO_PLAY_IRQMASK);
1012                 if (which & IRQ_FINISHED_PLAYBUF_2)
1013                         /* ack IRQ */
1014                         outw(which | IRQ_FINISHED_PLAYBUF_2, chip->codec_port+IDX_IO_PLAY_IRQMASK);
1015                 if (which & IRQ_PLAY_SOMETHING)
1016                 {
1017                         snd_azf3328_dbgplay("azt3328: unknown play IRQ type occurred, please report!\n");
1018                 }
1019                 if (chip->pcm && chip->playback_substream)
1020                 {
1021                         snd_azf3328_dbgplay("which %x, playptr %lx\n", which, inl(chip->codec_port+IDX_IO_PLAY_DMA_CURRPOS));
1022                         snd_pcm_period_elapsed(chip->playback_substream);
1023                         snd_azf3328_dbgplay("period done, playptr %lx.\n", inl(chip->codec_port+IDX_IO_PLAY_DMA_CURRPOS));
1024                 }
1025                 else
1026                         snd_azf3328_dbgplay("azt3328: ouch, irq handler problem!\n");
1027                 spin_unlock(&chip->reg_lock);
1028         }
1029         if (status & IRQ_RECORDING)
1030         {
1031                 spin_lock(&chip->reg_lock);
1032                 which = inw(chip->codec_port+IDX_IO_REC_IRQMASK);
1033                 if (which & IRQ_FINISHED_RECBUF_1)
1034                         /* ack interrupt */
1035                         outw(which | IRQ_FINISHED_RECBUF_1, chip->codec_port+IDX_IO_REC_IRQMASK);
1036                 if (which & IRQ_FINISHED_RECBUF_2)
1037                         /* ack interrupt */
1038                         outw(which | IRQ_FINISHED_RECBUF_2, chip->codec_port+IDX_IO_REC_IRQMASK);
1039                 if (which & IRQ_REC_SOMETHING)
1040                 {
1041                         snd_azf3328_dbgplay("azt3328: unknown rec IRQ type occurred, please report!\n");
1042                 }
1043                 if (chip->pcm && chip->capture_substream)
1044                 {
1045                         snd_azf3328_dbgplay("which %x, recptr %lx\n", which, inl(chip->codec_port+IDX_IO_REC_DMA_CURRPOS));
1046                         spin_unlock(&chip->reg_lock);
1047                         snd_pcm_period_elapsed(chip->capture_substream);
1048                         spin_lock(&chip->reg_lock);
1049                         snd_azf3328_dbgplay("period done, recptr %lx.\n", inl(chip->codec_port+IDX_IO_REC_DMA_CURRPOS));
1050                 }
1051                 spin_unlock(&chip->reg_lock);
1052         }
1053         if (status & IRQ_MPU401)
1054                 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
1055         if (status & IRQ_SOMEIRQ)
1056                 snd_azf3328_dbgplay("azt3328: unknown IRQ type occurred, please report!\n");
1057         count++;
1058         return IRQ_HANDLED;
1059 }
1060
1061 /*****************************************************************/
1062
1063 static snd_pcm_hardware_t snd_azf3328_playback =
1064 {
1065         /* FIXME!! Correct? */
1066         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1067                                  SNDRV_PCM_INFO_MMAP_VALID),
1068         .formats =              SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
1069                                 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE,
1070         .rates =                SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_64000 | SNDRV_PCM_RATE_KNOT,
1071         .rate_min =             5512,
1072         .rate_max =             64000,
1073         .channels_min =         1,
1074         .channels_max =         2,
1075         .buffer_bytes_max =     65536,
1076         .period_bytes_min =     64,
1077         .period_bytes_max =     65536,
1078         .periods_min =          1,
1079         .periods_max =          1024,
1080         /* FIXME: maybe that card actually has a FIFO?
1081          * Hmm, it seems newer revisions do have one, but we still don't know
1082          * its size... */
1083         .fifo_size =            0,
1084 };
1085
1086 static snd_pcm_hardware_t snd_azf3328_capture =
1087 {
1088         /* FIXME */
1089         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1090                                  SNDRV_PCM_INFO_MMAP_VALID),
1091         .formats =              SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
1092                                 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE,
1093         .rates =                SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_64000 | SNDRV_PCM_RATE_KNOT,
1094         .rate_min =             5512,
1095         .rate_max =             64000,
1096         .channels_min =         1,
1097         .channels_max =         2,
1098         .buffer_bytes_max =     65536,
1099         .period_bytes_min =     64,
1100         .period_bytes_max =     65536,
1101         .periods_min =          1,
1102         .periods_max =          1024,
1103         .fifo_size =            0,
1104 };
1105
1106
1107 static unsigned int snd_azf3328_fixed_rates[] = {
1108         5512, 6620, 8000, 9600, 11025, 16000, 22050, 32000, 44100, 48000, 64000
1109 };
1110 static snd_pcm_hw_constraint_list_t snd_azf3328_hw_constraints_rates = {
1111         .count = ARRAY_SIZE(snd_azf3328_fixed_rates), 
1112         .list = snd_azf3328_fixed_rates,
1113         .mask = 0,
1114 };
1115
1116 /*****************************************************************/
1117
1118 static int snd_azf3328_playback_open(snd_pcm_substream_t * substream)
1119 {
1120         azf3328_t *chip = snd_pcm_substream_chip(substream);
1121         snd_pcm_runtime_t *runtime = substream->runtime;
1122
1123         snd_azf3328_dbgcallenter();
1124         chip->playback_substream = substream;
1125         runtime->hw = snd_azf3328_playback;
1126         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1127                                    &snd_azf3328_hw_constraints_rates);
1128         snd_azf3328_dbgcallleave();
1129         return 0;
1130 }
1131
1132 static int snd_azf3328_capture_open(snd_pcm_substream_t * substream)
1133 {
1134         azf3328_t *chip = snd_pcm_substream_chip(substream);
1135         snd_pcm_runtime_t *runtime = substream->runtime;
1136
1137         snd_azf3328_dbgcallenter();
1138         chip->capture_substream = substream;
1139         runtime->hw = snd_azf3328_capture;
1140         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1141                                    &snd_azf3328_hw_constraints_rates);
1142         snd_azf3328_dbgcallleave();
1143         return 0;
1144 }
1145
1146 static int snd_azf3328_playback_close(snd_pcm_substream_t * substream)
1147 {
1148         azf3328_t *chip = snd_pcm_substream_chip(substream);
1149
1150         snd_azf3328_dbgcallenter();
1151
1152         chip->playback_substream = NULL;
1153         snd_azf3328_dbgcallleave();
1154         return 0;
1155 }
1156
1157 static int snd_azf3328_capture_close(snd_pcm_substream_t * substream)
1158 {
1159         azf3328_t *chip = snd_pcm_substream_chip(substream);
1160
1161         snd_azf3328_dbgcallenter();
1162         chip->capture_substream = NULL;
1163         snd_azf3328_dbgcallleave();
1164         return 0;
1165 }
1166
1167 /******************************************************************/
1168
1169 static snd_pcm_ops_t snd_azf3328_playback_ops = {
1170         .open =         snd_azf3328_playback_open,
1171         .close =        snd_azf3328_playback_close,
1172         .ioctl =        snd_pcm_lib_ioctl,
1173         .hw_params =    snd_azf3328_hw_params,
1174         .hw_free =      snd_azf3328_hw_free,
1175         .prepare =      snd_azf3328_playback_prepare,
1176         .trigger =      snd_azf3328_playback_trigger,
1177         .pointer =      snd_azf3328_playback_pointer
1178 };
1179
1180 static snd_pcm_ops_t snd_azf3328_capture_ops = {
1181         .open =         snd_azf3328_capture_open,
1182         .close =        snd_azf3328_capture_close,
1183         .ioctl =        snd_pcm_lib_ioctl,
1184         .hw_params =    snd_azf3328_hw_params,
1185         .hw_free =      snd_azf3328_hw_free,
1186         .prepare =      snd_azf3328_capture_prepare,
1187         .trigger =      snd_azf3328_capture_trigger,
1188         .pointer =      snd_azf3328_capture_pointer
1189 };
1190
1191 static void snd_azf3328_pcm_free(snd_pcm_t *pcm)
1192 {
1193         azf3328_t *chip = pcm->private_data;
1194         chip->pcm = NULL;
1195         snd_pcm_lib_preallocate_free_for_all(pcm);
1196 }
1197
1198 static int __devinit snd_azf3328_pcm(azf3328_t *chip, int device)
1199 {
1200         snd_pcm_t *pcm;
1201         int err;
1202
1203         snd_azf3328_dbgcallenter();
1204         if ((err = snd_pcm_new(chip->card, "AZF3328 DSP", device, 1, 1, &pcm)) < 0)
1205                 return err;
1206         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_azf3328_playback_ops);
1207         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_azf3328_capture_ops);
1208
1209         pcm->private_data = chip;
1210         pcm->private_free = snd_azf3328_pcm_free;
1211         pcm->info_flags = 0;
1212         strcpy(pcm->name, chip->card->shortname);
1213         chip->pcm = pcm;
1214
1215         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1216                                               snd_dma_pci_data(chip->pci), 64*1024, 64*1024);
1217
1218         snd_azf3328_dbgcallleave();
1219         return 0;
1220 }
1221
1222 /******************************************************************/
1223
1224 #ifdef SUPPORT_JOYSTICK
1225 static int __devinit snd_azf3328_config_joystick(azf3328_t *chip, int dev)
1226 {
1227         struct gameport *gp;
1228         struct resource *r;
1229
1230         if (!joystick[dev])
1231                 return -ENODEV;
1232
1233         if (!(r = request_region(0x200, 8, "AZF3328 gameport"))) {
1234                 printk(KERN_WARNING "azt3328: cannot reserve joystick ports\n");
1235                 return -EBUSY;
1236         }
1237
1238         chip->gameport = gp = gameport_allocate_port();
1239         if (!gp) {
1240                 printk(KERN_ERR "azt3328: cannot allocate memory for gameport\n");
1241                 release_and_free_resource(r);
1242                 return -ENOMEM;
1243         }
1244
1245         gameport_set_name(gp, "AZF3328 Gameport");
1246         gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1247         gameport_set_dev_parent(gp, &chip->pci->dev);
1248         gp->io = 0x200;
1249         gameport_set_port_data(gp, r);
1250
1251         snd_azf3328_io2_write(chip, IDX_IO2_LEGACY_ADDR,
1252                               snd_azf3328_io2_read(chip, IDX_IO2_LEGACY_ADDR) | LEGACY_JOY);
1253
1254         gameport_register_port(chip->gameport);
1255
1256         return 0;
1257 }
1258
1259 static void snd_azf3328_free_joystick(azf3328_t *chip)
1260 {
1261         if (chip->gameport) {
1262                 struct resource *r = gameport_get_port_data(chip->gameport);
1263
1264                 gameport_unregister_port(chip->gameport);
1265                 chip->gameport = NULL;
1266                 /* disable gameport */
1267                 snd_azf3328_io2_write(chip, IDX_IO2_LEGACY_ADDR,
1268                                       snd_azf3328_io2_read(chip, IDX_IO2_LEGACY_ADDR) & ~LEGACY_JOY);
1269                 release_and_free_resource(r);
1270         }
1271 }
1272 #else
1273 static inline int snd_azf3328_config_joystick(azf3328_t *chip, int dev) { return -ENOSYS; }
1274 static inline void snd_azf3328_free_joystick(azf3328_t *chip) { }
1275 #endif
1276
1277 /******************************************************************/
1278
1279 static int snd_azf3328_free(azf3328_t *chip)
1280 {
1281         if (chip->irq < 0)
1282                 goto __end_hw;
1283
1284         /* reset (close) mixer */
1285         snd_azf3328_mixer_set_mute(chip, IDX_MIXER_PLAY_MASTER, 1); /* first mute master volume */
1286         snd_azf3328_mixer_write(chip, IDX_MIXER_RESET, 0x0, WORD_VALUE);
1287
1288         /* interrupt setup - mask everything */
1289         /* FIXME */
1290
1291         synchronize_irq(chip->irq);
1292       __end_hw:
1293         snd_azf3328_free_joystick(chip);
1294         if (chip->irq >= 0)
1295                 free_irq(chip->irq, (void *)chip);
1296         pci_release_regions(chip->pci);
1297         pci_disable_device(chip->pci);
1298
1299         kfree(chip);
1300         return 0;
1301 }
1302
1303 static int snd_azf3328_dev_free(snd_device_t *device)
1304 {
1305         azf3328_t *chip = device->device_data;
1306         return snd_azf3328_free(chip);
1307 }
1308
1309 #if 0
1310 /* check whether a bit can be modified */
1311 static void snd_azf3328_test_bit(unsigned int reg, int bit)
1312 {
1313         unsigned char val, valoff, valon;
1314
1315         val = inb(reg);
1316
1317         outb(val & ~(1 << bit), reg);
1318         valoff = inb(reg);
1319
1320         outb(val|(1 << bit), reg);
1321         valon = inb(reg);
1322         
1323         outb(val, reg);
1324
1325         printk(KERN_ERR "reg %04x bit %d: %02x %02x %02x\n", reg, bit, val, valoff, valon);
1326 }
1327 #endif
1328
1329 static int __devinit snd_azf3328_create(snd_card_t * card,
1330                                          struct pci_dev *pci,
1331                                          unsigned long device_type,
1332                                          azf3328_t ** rchip)
1333 {
1334         azf3328_t *chip;
1335         int err;
1336         static snd_device_ops_t ops = {
1337                 .dev_free =     snd_azf3328_dev_free,
1338         };
1339         u16 tmp;
1340
1341         *rchip = NULL;
1342
1343         if ((err = pci_enable_device(pci)) < 0)
1344                 return err;
1345
1346         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1347         if (chip == NULL) {
1348                 pci_disable_device(pci);
1349                 return -ENOMEM;
1350         }
1351         spin_lock_init(&chip->reg_lock);
1352         chip->card = card;
1353         chip->pci = pci;
1354         chip->irq = -1;
1355
1356         /* check if we can restrict PCI DMA transfers to 24 bits */
1357         if (pci_set_dma_mask(pci, 0x00ffffff) < 0 ||
1358             pci_set_consistent_dma_mask(pci, 0x00ffffff) < 0) {
1359                 snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
1360                 pci_disable_device(pci);
1361                 return -ENXIO;
1362         }
1363
1364         if ((err = pci_request_regions(pci, "Aztech AZF3328")) < 0) {
1365                 kfree(chip);
1366                 pci_disable_device(pci);
1367                 return err;
1368         }
1369
1370         chip->codec_port = pci_resource_start(pci, 0);
1371         chip->io2_port = pci_resource_start(pci, 1);
1372         chip->mpu_port = pci_resource_start(pci, 2);
1373         chip->synth_port = pci_resource_start(pci, 3);
1374         chip->mixer_port = pci_resource_start(pci, 4);
1375
1376         if (request_irq(pci->irq, snd_azf3328_interrupt, SA_INTERRUPT|SA_SHIRQ, card->shortname, (void *)chip)) {
1377                 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
1378                 snd_azf3328_free(chip);
1379                 return -EBUSY;
1380         }
1381         chip->irq = pci->irq;
1382         pci_set_master(pci);
1383         synchronize_irq(chip->irq);
1384
1385         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);
1386
1387         snd_azf3328_dbgmisc("io2 %02x %02x %02x %02x %02x %02x\n", snd_azf3328_io2_read(chip, 0), snd_azf3328_io2_read(chip, 1), snd_azf3328_io2_read(chip, 2), snd_azf3328_io2_read(chip, 3), snd_azf3328_io2_read(chip, 4), snd_azf3328_io2_read(chip, 5));
1388
1389         for (tmp=0; tmp <= 0x01; tmp += 1)
1390                 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));
1391
1392         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1393                 snd_azf3328_free(chip);
1394                 return err;
1395         }
1396
1397         /* create mixer interface & switches */
1398         if ((err = snd_azf3328_mixer_new(chip)) < 0)
1399                 return err;
1400
1401 #if 0
1402         /* set very low bitrate to reduce noise and power consumption? */
1403         snd_azf3328_setfmt(chip, IDX_IO_PLAY_SOUNDFORMAT, 5512, 8, 1);
1404 #endif
1405
1406         /* standard chip init stuff */
1407         spin_lock_irq(&chip->reg_lock);
1408         outb(DMA_PLAY_SOMETHING2|DMA_EPILOGUE_SOMETHING|DMA_SOMETHING_ELSE, chip->codec_port + IDX_IO_PLAY_FLAGS);
1409         outb(DMA_PLAY_SOMETHING2|DMA_EPILOGUE_SOMETHING|DMA_SOMETHING_ELSE, chip->codec_port + IDX_IO_SOMETHING_FLAGS);
1410         outb(DMA_PLAY_SOMETHING2|DMA_EPILOGUE_SOMETHING|DMA_SOMETHING_ELSE, chip->codec_port + IDX_IO_REC_FLAGS);
1411         outb(0x0, chip->codec_port + IDX_IO_IRQ63H);
1412
1413         spin_unlock_irq(&chip->reg_lock);
1414
1415         snd_card_set_dev(card, &pci->dev);
1416
1417         *rchip = chip;
1418         return 0;
1419 }
1420
1421 static int __devinit snd_azf3328_probe(struct pci_dev *pci,
1422                                           const struct pci_device_id *pci_id)
1423 {
1424         static int dev;
1425         snd_card_t *card;
1426         azf3328_t *chip;
1427         opl3_t *opl3;
1428         int err;
1429
1430         snd_azf3328_dbgcallenter();
1431         if (dev >= SNDRV_CARDS)
1432                 return -ENODEV;
1433         if (!enable[dev]) {
1434                 dev++;
1435                 return -ENOENT;
1436         }
1437
1438         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0 );
1439         if (card == NULL)
1440                 return -ENOMEM;
1441
1442         strcpy(card->driver, "AZF3328");
1443         strcpy(card->shortname, "Aztech AZF3328 (PCI168)");
1444
1445         if ((err = snd_azf3328_create(card, pci, pci_id->driver_data, &chip)) < 0) {
1446                 snd_card_free(card);
1447                 return err;
1448         }
1449
1450         if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_MPU401,
1451                                         chip->mpu_port, 1, pci->irq, 0,
1452                                         &chip->rmidi)) < 0) {
1453                 snd_printk(KERN_ERR "azf3328: no MPU-401 device at 0x%lx?\n", chip->mpu_port);
1454                 snd_card_free(card);
1455                 return err;
1456         }
1457
1458         if ((err = snd_azf3328_pcm(chip, 0)) < 0) {
1459                 snd_card_free(card);
1460                 return err;
1461         }
1462
1463         if (snd_opl3_create(card, chip->synth_port, chip->synth_port+2,
1464                             OPL3_HW_AUTO, 1, &opl3) < 0) {
1465                 snd_printk(KERN_ERR "azf3328: no OPL3 device at 0x%lx-0x%lx?\n",
1466                            chip->synth_port, chip->synth_port+2 );
1467         } else {
1468                 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
1469                         snd_card_free(card);
1470                         return err;
1471                 }
1472         }
1473
1474         snd_azf3328_dbgio(chip, "create");
1475
1476         sprintf(card->longname, "%s at 0x%lx, irq %i",
1477                 card->shortname, chip->codec_port, chip->irq);
1478
1479         if ((err = snd_card_register(card)) < 0) {
1480                 snd_card_free(card);
1481                 return err;
1482         }
1483
1484 #ifdef MODULE
1485         printk(
1486 "azt3328: Experimental driver for Aztech AZF3328-based soundcards such as PCI168.\n"
1487 "azt3328: ZERO support from Aztech: you might think hard about future purchase.\n"
1488 "azt3328: Feel free to contact hw7oshyuv3001@sneakemail.com for bug reports etc.!\n");
1489 #endif
1490
1491         if (snd_azf3328_config_joystick(chip, dev) < 0)
1492                 snd_azf3328_io2_write(chip, IDX_IO2_LEGACY_ADDR,
1493                               snd_azf3328_io2_read(chip, IDX_IO2_LEGACY_ADDR) & ~LEGACY_JOY);
1494
1495         pci_set_drvdata(pci, card);
1496         dev++;
1497
1498         snd_azf3328_dbgcallleave();
1499         return 0;
1500 }
1501
1502 static void __devexit snd_azf3328_remove(struct pci_dev *pci)
1503 {
1504         snd_azf3328_dbgcallenter();
1505         snd_card_free(pci_get_drvdata(pci));
1506         pci_set_drvdata(pci, NULL);
1507         snd_azf3328_dbgcallleave();
1508 }
1509
1510 static struct pci_driver driver = {
1511         .name = "AZF3328",
1512         .owner = THIS_MODULE,
1513         .id_table = snd_azf3328_ids,
1514         .probe = snd_azf3328_probe,
1515         .remove = __devexit_p(snd_azf3328_remove),
1516 };
1517
1518 static int __init alsa_card_azf3328_init(void)
1519 {
1520         int err;
1521         snd_azf3328_dbgcallenter();
1522         err = pci_register_driver(&driver);
1523         snd_azf3328_dbgcallleave();
1524         return err;
1525 }
1526
1527 static void __exit alsa_card_azf3328_exit(void)
1528 {
1529         snd_azf3328_dbgcallenter();
1530         pci_unregister_driver(&driver);
1531         snd_azf3328_dbgcallleave();
1532 }
1533
1534 module_init(alsa_card_azf3328_init)
1535 module_exit(alsa_card_azf3328_exit)