Merge branch 'audit.b3' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/audit...
[linux-2.6] / sound / isa / opl3sa2.c
1 /*
2  *  Driver for Yamaha OPL3-SA[2,3] soundcards
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4  *
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21
22 #include <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/err.h>
25 #include <linux/platform_device.h>
26 #include <linux/interrupt.h>
27 #include <linux/pm.h>
28 #include <linux/slab.h>
29 #include <linux/pnp.h>
30 #include <linux/moduleparam.h>
31 #include <sound/core.h>
32 #include <sound/cs4231.h>
33 #include <sound/mpu401.h>
34 #include <sound/opl3.h>
35 #include <sound/initval.h>
36
37 #include <asm/io.h>
38
39 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
40 MODULE_DESCRIPTION("Yamaha OPL3SA2+");
41 MODULE_LICENSE("GPL");
42 MODULE_SUPPORTED_DEVICE("{{Yamaha,YMF719E-S},"
43                 "{Genius,Sound Maker 3DX},"
44                 "{Yamaha,OPL3SA3},"
45                 "{Intel,AL440LX sound},"
46                 "{NeoMagic,MagicWave 3DX}}");
47
48 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
49 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
50 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
51 #ifdef CONFIG_PNP
52 static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
53 #endif
54 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* 0xf86,0x370,0x100 */
55 static long sb_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;  /* 0x220,0x240,0x260 */
56 static long wss_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;/* 0x530,0xe80,0xf40,0x604 */
57 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;  /* 0x388 */
58 static long midi_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;/* 0x330,0x300 */
59 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* 0,1,3,5,9,11,12,15 */
60 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 1,3,5,6,7 */
61 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 1,3,5,6,7 */
62 static int opl3sa3_ymode[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = 0 };   /* 0,1,2,3 */ /*SL Added*/
63
64 module_param_array(index, int, NULL, 0444);
65 MODULE_PARM_DESC(index, "Index value for OPL3-SA soundcard.");
66 module_param_array(id, charp, NULL, 0444);
67 MODULE_PARM_DESC(id, "ID string for OPL3-SA soundcard.");
68 module_param_array(enable, bool, NULL, 0444);
69 MODULE_PARM_DESC(enable, "Enable OPL3-SA soundcard.");
70 #ifdef CONFIG_PNP
71 module_param_array(isapnp, bool, NULL, 0444);
72 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
73 #endif
74 module_param_array(port, long, NULL, 0444);
75 MODULE_PARM_DESC(port, "Port # for OPL3-SA driver.");
76 module_param_array(sb_port, long, NULL, 0444);
77 MODULE_PARM_DESC(sb_port, "SB port # for OPL3-SA driver.");
78 module_param_array(wss_port, long, NULL, 0444);
79 MODULE_PARM_DESC(wss_port, "WSS port # for OPL3-SA driver.");
80 module_param_array(fm_port, long, NULL, 0444);
81 MODULE_PARM_DESC(fm_port, "FM port # for OPL3-SA driver.");
82 module_param_array(midi_port, long, NULL, 0444);
83 MODULE_PARM_DESC(midi_port, "MIDI port # for OPL3-SA driver.");
84 module_param_array(irq, int, NULL, 0444);
85 MODULE_PARM_DESC(irq, "IRQ # for OPL3-SA driver.");
86 module_param_array(dma1, int, NULL, 0444);
87 MODULE_PARM_DESC(dma1, "DMA1 # for OPL3-SA driver.");
88 module_param_array(dma2, int, NULL, 0444);
89 MODULE_PARM_DESC(dma2, "DMA2 # for OPL3-SA driver.");
90 module_param_array(opl3sa3_ymode, int, NULL, 0444);
91 MODULE_PARM_DESC(opl3sa3_ymode, "Speaker size selection for 3D Enhancement mode: Desktop/Large Notebook/Small Notebook/HiFi.");
92
93 static struct platform_device *platform_devices[SNDRV_CARDS];
94 #ifdef CONFIG_PNP
95 static int pnp_registered;
96 static int pnpc_registered;
97 #endif
98
99 /* control ports */
100 #define OPL3SA2_PM_CTRL         0x01
101 #define OPL3SA2_SYS_CTRL                0x02
102 #define OPL3SA2_IRQ_CONFIG      0x03
103 #define OPL3SA2_IRQ_STATUS      0x04
104 #define OPL3SA2_DMA_CONFIG      0x06
105 #define OPL3SA2_MASTER_LEFT     0x07
106 #define OPL3SA2_MASTER_RIGHT    0x08
107 #define OPL3SA2_MIC             0x09
108 #define OPL3SA2_MISC            0x0A
109
110 /* opl3sa3 only */
111 #define OPL3SA3_DGTL_DOWN       0x12
112 #define OPL3SA3_ANLG_DOWN       0x13
113 #define OPL3SA3_WIDE            0x14
114 #define OPL3SA3_BASS            0x15
115 #define OPL3SA3_TREBLE          0x16
116
117 /* power management bits */
118 #define OPL3SA2_PM_ADOWN                0x20
119 #define OPL3SA2_PM_PSV          0x04            
120 #define OPL3SA2_PM_PDN          0x02
121 #define OPL3SA2_PM_PDX          0x01
122
123 #define OPL3SA2_PM_D0   0x00
124 #define OPL3SA2_PM_D3   (OPL3SA2_PM_ADOWN|OPL3SA2_PM_PSV|OPL3SA2_PM_PDN|OPL3SA2_PM_PDX)
125
126 struct snd_opl3sa2 {
127         struct snd_card *card;
128         int version;            /* 2 or 3 */
129         unsigned long port;     /* control port */
130         struct resource *res_port; /* control port resource */
131         int irq;
132         int single_dma;
133         spinlock_t reg_lock;
134         struct snd_hwdep *synth;
135         struct snd_rawmidi *rmidi;
136         struct snd_cs4231 *cs4231;
137         unsigned char ctlregs[0x20];
138         int ymode;              /* SL added */
139         struct snd_kcontrol *master_switch;
140         struct snd_kcontrol *master_volume;
141 };
142
143 #define PFX     "opl3sa2: "
144
145 #ifdef CONFIG_PNP
146
147 static struct pnp_device_id snd_opl3sa2_pnpbiosids[] = {
148         { .id = "YMH0021" },
149         { .id = "NMX2210" },    /* Gateway Solo 2500 */
150         { .id = "" }            /* end */
151 };
152
153 MODULE_DEVICE_TABLE(pnp, snd_opl3sa2_pnpbiosids);
154
155 static struct pnp_card_device_id snd_opl3sa2_pnpids[] = {
156         /* Yamaha YMF719E-S (Genius Sound Maker 3DX) */
157         { .id = "YMH0020", .devs = { { "YMH0021" } } },
158         /* Yamaha OPL3-SA3 (integrated on Intel's Pentium II AL440LX motherboard) */
159         { .id = "YMH0030", .devs = { { "YMH0021" } } },
160         /* Yamaha OPL3-SA2 */
161         { .id = "YMH0800", .devs = { { "YMH0021" } } },
162         /* Yamaha OPL3-SA2 */
163         { .id = "YMH0801", .devs = { { "YMH0021" } } },
164         /* NeoMagic MagicWave 3DX */
165         { .id = "NMX2200", .devs = { { "YMH2210" } } },
166         /* --- */
167         { .id = "" }    /* end */
168 };
169
170 MODULE_DEVICE_TABLE(pnp_card, snd_opl3sa2_pnpids);
171
172 #endif /* CONFIG_PNP */
173
174
175 /* read control port (w/o spinlock) */
176 static unsigned char __snd_opl3sa2_read(struct snd_opl3sa2 *chip, unsigned char reg)
177 {
178         unsigned char result;
179 #if 0
180         outb(0x1d, port);       /* password */
181         printk("read [0x%lx] = 0x%x\n", port, inb(port));
182 #endif
183         outb(reg, chip->port);  /* register */
184         result = inb(chip->port + 1);
185 #if 0
186         printk("read [0x%lx] = 0x%x [0x%x]\n", port, result, inb(port));
187 #endif
188         return result;
189 }
190
191 /* read control port (with spinlock) */
192 static unsigned char snd_opl3sa2_read(struct snd_opl3sa2 *chip, unsigned char reg)
193 {
194         unsigned long flags;
195         unsigned char result;
196
197         spin_lock_irqsave(&chip->reg_lock, flags);
198         result = __snd_opl3sa2_read(chip, reg);
199         spin_unlock_irqrestore(&chip->reg_lock, flags);
200         return result;
201 }
202
203 /* write control port (w/o spinlock) */
204 static void __snd_opl3sa2_write(struct snd_opl3sa2 *chip, unsigned char reg, unsigned char value)
205 {
206 #if 0
207         outb(0x1d, port);       /* password */
208 #endif
209         outb(reg, chip->port);  /* register */
210         outb(value, chip->port + 1);
211         chip->ctlregs[reg] = value;
212 }
213
214 /* write control port (with spinlock) */
215 static void snd_opl3sa2_write(struct snd_opl3sa2 *chip, unsigned char reg, unsigned char value)
216 {
217         unsigned long flags;
218         spin_lock_irqsave(&chip->reg_lock, flags);
219         __snd_opl3sa2_write(chip, reg, value);
220         spin_unlock_irqrestore(&chip->reg_lock, flags);
221 }
222
223 static int __init snd_opl3sa2_detect(struct snd_opl3sa2 *chip)
224 {
225         struct snd_card *card;
226         unsigned long port;
227         unsigned char tmp, tmp1;
228         char str[2];
229
230         card = chip->card;
231         port = chip->port;
232         if ((chip->res_port = request_region(port, 2, "OPL3-SA control")) == NULL) {
233                 snd_printk(KERN_ERR PFX "can't grab port 0x%lx\n", port);
234                 return -EBUSY;
235         }
236         // snd_printk("REG 0A = 0x%x\n", snd_opl3sa2_read(chip, 0x0a));
237         chip->version = 0;
238         tmp = snd_opl3sa2_read(chip, OPL3SA2_MISC);
239         if (tmp == 0xff) {
240                 snd_printd("OPL3-SA [0x%lx] detect = 0x%x\n", port, tmp);
241                 return -ENODEV;
242         }
243         switch (tmp & 0x07) {
244         case 0x01:
245                 chip->version = 2; /* YMF711 */
246                 break;
247         default:
248                 chip->version = 3;
249                 /* 0x02 - standard */
250                 /* 0x03 - YM715B */
251                 /* 0x04 - YM719 - OPL-SA4? */
252                 /* 0x05 - OPL3-SA3 - Libretto 100 */
253                 break;
254         }
255         str[0] = chip->version + '0';
256         str[1] = 0;
257         strcat(card->shortname, str);
258         snd_opl3sa2_write(chip, OPL3SA2_MISC, tmp ^ 7);
259         if ((tmp1 = snd_opl3sa2_read(chip, OPL3SA2_MISC)) != tmp) {
260                 snd_printd("OPL3-SA [0x%lx] detect (1) = 0x%x (0x%x)\n", port, tmp, tmp1);
261                 return -ENODEV;
262         }
263         /* try if the MIC register is accesible */
264         tmp = snd_opl3sa2_read(chip, OPL3SA2_MIC);
265         snd_opl3sa2_write(chip, OPL3SA2_MIC, 0x8a);
266         if (((tmp1 = snd_opl3sa2_read(chip, OPL3SA2_MIC)) & 0x9f) != 0x8a) {
267                 snd_printd("OPL3-SA [0x%lx] detect (2) = 0x%x (0x%x)\n", port, tmp, tmp1);
268                 return -ENODEV;
269         }
270         snd_opl3sa2_write(chip, OPL3SA2_MIC, 0x9f);
271         /* initialization */
272         /* Power Management - full on */
273         snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D0);
274         if (chip->version > 2) {
275                 /* ymode is bits 4&5 (of 0 to 7) on all but opl3sa2 versions */
276                 snd_opl3sa2_write(chip, OPL3SA2_SYS_CTRL, (chip->ymode << 4));
277         } else {
278                 /* default for opl3sa2 versions */
279                 snd_opl3sa2_write(chip, OPL3SA2_SYS_CTRL, 0x00);
280         }
281         snd_opl3sa2_write(chip, OPL3SA2_IRQ_CONFIG, 0x0d);      /* Interrupt Channel Configuration - IRQ A = OPL3 + MPU + WSS */
282         if (chip->single_dma) {
283                 snd_opl3sa2_write(chip, OPL3SA2_DMA_CONFIG, 0x03);      /* DMA Configuration - DMA A = WSS-R + WSS-P */
284         } else {
285                 snd_opl3sa2_write(chip, OPL3SA2_DMA_CONFIG, 0x21);      /* DMA Configuration - DMA B = WSS-R, DMA A = WSS-P */
286         }
287         snd_opl3sa2_write(chip, OPL3SA2_MISC, 0x80 | (tmp & 7));        /* Miscellaneous - default */
288         if (chip->version > 2) {
289                 snd_opl3sa2_write(chip, OPL3SA3_DGTL_DOWN, 0x00);       /* Digital Block Partial Power Down - default */
290                 snd_opl3sa2_write(chip, OPL3SA3_ANLG_DOWN, 0x00);       /* Analog Block Partial Power Down - default */
291         }
292         return 0;
293 }
294
295 static irqreturn_t snd_opl3sa2_interrupt(int irq, void *dev_id, struct pt_regs *regs)
296 {
297         unsigned short status;
298         struct snd_opl3sa2 *chip = dev_id;
299         int handled = 0;
300
301         if (chip == NULL || chip->card == NULL)
302                 return IRQ_NONE;
303
304         status = snd_opl3sa2_read(chip, OPL3SA2_IRQ_STATUS);
305
306         if (status & 0x20) {
307                 handled = 1;
308                 snd_opl3_interrupt(chip->synth);
309         }
310
311         if ((status & 0x10) && chip->rmidi != NULL) {
312                 handled = 1;
313                 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
314         }
315
316         if (status & 0x07) {    /* TI,CI,PI */
317                 handled = 1;
318                 snd_cs4231_interrupt(irq, chip->cs4231, regs);
319         }
320
321         if (status & 0x40) { /* hardware volume change */
322                 handled = 1;
323                 /* reading from Master Lch register at 0x07 clears this bit */
324                 snd_opl3sa2_read(chip, OPL3SA2_MASTER_RIGHT);
325                 snd_opl3sa2_read(chip, OPL3SA2_MASTER_LEFT);
326                 if (chip->master_switch && chip->master_volume) {
327                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id);
328                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_volume->id);
329                 }
330         }
331         return IRQ_RETVAL(handled);
332 }
333
334 #define OPL3SA2_SINGLE(xname, xindex, reg, shift, mask, invert) \
335 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
336   .info = snd_opl3sa2_info_single, \
337   .get = snd_opl3sa2_get_single, .put = snd_opl3sa2_put_single, \
338   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
339
340 static int snd_opl3sa2_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
341 {
342         int mask = (kcontrol->private_value >> 16) & 0xff;
343
344         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
345         uinfo->count = 1;
346         uinfo->value.integer.min = 0;
347         uinfo->value.integer.max = mask;
348         return 0;
349 }
350
351 static int snd_opl3sa2_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
352 {
353         struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
354         unsigned long flags;
355         int reg = kcontrol->private_value & 0xff;
356         int shift = (kcontrol->private_value >> 8) & 0xff;
357         int mask = (kcontrol->private_value >> 16) & 0xff;
358         int invert = (kcontrol->private_value >> 24) & 0xff;
359
360         spin_lock_irqsave(&chip->reg_lock, flags);
361         ucontrol->value.integer.value[0] = (chip->ctlregs[reg] >> shift) & mask;
362         spin_unlock_irqrestore(&chip->reg_lock, flags);
363         if (invert)
364                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
365         return 0;
366 }
367
368 static int snd_opl3sa2_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
369 {
370         struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
371         unsigned long flags;
372         int reg = kcontrol->private_value & 0xff;
373         int shift = (kcontrol->private_value >> 8) & 0xff;
374         int mask = (kcontrol->private_value >> 16) & 0xff;
375         int invert = (kcontrol->private_value >> 24) & 0xff;
376         int change;
377         unsigned short val, oval;
378         
379         val = (ucontrol->value.integer.value[0] & mask);
380         if (invert)
381                 val = mask - val;
382         val <<= shift;
383         spin_lock_irqsave(&chip->reg_lock, flags);
384         oval = chip->ctlregs[reg];
385         val = (oval & ~(mask << shift)) | val;
386         change = val != oval;
387         __snd_opl3sa2_write(chip, reg, val);
388         spin_unlock_irqrestore(&chip->reg_lock, flags);
389         return change;
390 }
391
392 #define OPL3SA2_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
393 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
394   .info = snd_opl3sa2_info_double, \
395   .get = snd_opl3sa2_get_double, .put = snd_opl3sa2_put_double, \
396   .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
397
398 static int snd_opl3sa2_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
399 {
400         int mask = (kcontrol->private_value >> 24) & 0xff;
401
402         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
403         uinfo->count = 2;
404         uinfo->value.integer.min = 0;
405         uinfo->value.integer.max = mask;
406         return 0;
407 }
408
409 static int snd_opl3sa2_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
410 {
411         struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
412         unsigned long flags;
413         int left_reg = kcontrol->private_value & 0xff;
414         int right_reg = (kcontrol->private_value >> 8) & 0xff;
415         int shift_left = (kcontrol->private_value >> 16) & 0x07;
416         int shift_right = (kcontrol->private_value >> 19) & 0x07;
417         int mask = (kcontrol->private_value >> 24) & 0xff;
418         int invert = (kcontrol->private_value >> 22) & 1;
419         
420         spin_lock_irqsave(&chip->reg_lock, flags);
421         ucontrol->value.integer.value[0] = (chip->ctlregs[left_reg] >> shift_left) & mask;
422         ucontrol->value.integer.value[1] = (chip->ctlregs[right_reg] >> shift_right) & mask;
423         spin_unlock_irqrestore(&chip->reg_lock, flags);
424         if (invert) {
425                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
426                 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
427         }
428         return 0;
429 }
430
431 static int snd_opl3sa2_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
432 {
433         struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
434         unsigned long flags;
435         int left_reg = kcontrol->private_value & 0xff;
436         int right_reg = (kcontrol->private_value >> 8) & 0xff;
437         int shift_left = (kcontrol->private_value >> 16) & 0x07;
438         int shift_right = (kcontrol->private_value >> 19) & 0x07;
439         int mask = (kcontrol->private_value >> 24) & 0xff;
440         int invert = (kcontrol->private_value >> 22) & 1;
441         int change;
442         unsigned short val1, val2, oval1, oval2;
443         
444         val1 = ucontrol->value.integer.value[0] & mask;
445         val2 = ucontrol->value.integer.value[1] & mask;
446         if (invert) {
447                 val1 = mask - val1;
448                 val2 = mask - val2;
449         }
450         val1 <<= shift_left;
451         val2 <<= shift_right;
452         spin_lock_irqsave(&chip->reg_lock, flags);
453         if (left_reg != right_reg) {
454                 oval1 = chip->ctlregs[left_reg];
455                 oval2 = chip->ctlregs[right_reg];
456                 val1 = (oval1 & ~(mask << shift_left)) | val1;
457                 val2 = (oval2 & ~(mask << shift_right)) | val2;
458                 change = val1 != oval1 || val2 != oval2;
459                 __snd_opl3sa2_write(chip, left_reg, val1);
460                 __snd_opl3sa2_write(chip, right_reg, val2);
461         } else {
462                 oval1 = chip->ctlregs[left_reg];
463                 val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
464                 change = val1 != oval1;
465                 __snd_opl3sa2_write(chip, left_reg, val1);
466         }
467         spin_unlock_irqrestore(&chip->reg_lock, flags);
468         return change;
469 }
470
471 static struct snd_kcontrol_new snd_opl3sa2_controls[] = {
472 OPL3SA2_DOUBLE("Master Playback Switch", 0, 0x07, 0x08, 7, 7, 1, 1),
473 OPL3SA2_DOUBLE("Master Playback Volume", 0, 0x07, 0x08, 0, 0, 15, 1),
474 OPL3SA2_SINGLE("Mic Playback Switch", 0, 0x09, 7, 1, 1),
475 OPL3SA2_SINGLE("Mic Playback Volume", 0, 0x09, 0, 31, 1)
476 };
477
478 static struct snd_kcontrol_new snd_opl3sa2_tone_controls[] = {
479 OPL3SA2_DOUBLE("3D Control - Wide", 0, 0x14, 0x14, 4, 0, 7, 0),
480 OPL3SA2_DOUBLE("Tone Control - Bass", 0, 0x15, 0x15, 4, 0, 7, 0),
481 OPL3SA2_DOUBLE("Tone Control - Treble", 0, 0x16, 0x16, 4, 0, 7, 0)
482 };
483
484 static void snd_opl3sa2_master_free(struct snd_kcontrol *kcontrol)
485 {
486         struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
487         chip->master_switch = NULL;
488         chip->master_volume = NULL;
489 }
490
491 static int __init snd_opl3sa2_mixer(struct snd_opl3sa2 *chip)
492 {
493         struct snd_card *card = chip->card;
494         struct snd_ctl_elem_id id1, id2;
495         struct snd_kcontrol *kctl;
496         unsigned int idx;
497         int err;
498
499         memset(&id1, 0, sizeof(id1));
500         memset(&id2, 0, sizeof(id2));
501         id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
502         /* reassign AUX0 to CD */
503         strcpy(id1.name, "Aux Playback Switch");
504         strcpy(id2.name, "CD Playback Switch");
505         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
506                 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
507                 return err;
508         }
509         strcpy(id1.name, "Aux Playback Volume");
510         strcpy(id2.name, "CD Playback Volume");
511         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
512                 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
513                 return err;
514         }
515         /* reassign AUX1 to FM */
516         strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
517         strcpy(id2.name, "FM Playback Switch");
518         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
519                 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
520                 return err;
521         }
522         strcpy(id1.name, "Aux Playback Volume");
523         strcpy(id2.name, "FM Playback Volume");
524         if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
525                 snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
526                 return err;
527         }
528         /* add OPL3SA2 controls */
529         for (idx = 0; idx < ARRAY_SIZE(snd_opl3sa2_controls); idx++) {
530                 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_opl3sa2_controls[idx], chip))) < 0)
531                         return err;
532                 switch (idx) {
533                 case 0: chip->master_switch = kctl; kctl->private_free = snd_opl3sa2_master_free; break;
534                 case 1: chip->master_volume = kctl; kctl->private_free = snd_opl3sa2_master_free; break;
535                 }
536         }
537         if (chip->version > 2) {
538                 for (idx = 0; idx < ARRAY_SIZE(snd_opl3sa2_tone_controls); idx++)
539                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_opl3sa2_tone_controls[idx], chip))) < 0)
540                                 return err;
541         }
542         return 0;
543 }
544
545 /* Power Management support functions */
546 #ifdef CONFIG_PM
547 static int snd_opl3sa2_suspend(struct snd_card *card, pm_message_t state)
548 {
549         struct snd_opl3sa2 *chip = card->private_data;
550
551         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
552         chip->cs4231->suspend(chip->cs4231);
553         /* power down */
554         snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D3);
555
556         return 0;
557 }
558
559 static int snd_opl3sa2_resume(struct snd_card *card)
560 {
561         struct snd_opl3sa2 *chip = card->private_data;
562         int i;
563
564         /* power up */
565         snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D0);
566
567         /* restore registers */
568         for (i = 2; i <= 0x0a; i++) {
569                 if (i != OPL3SA2_IRQ_STATUS)
570                         snd_opl3sa2_write(chip, i, chip->ctlregs[i]);
571         }
572         if (chip->version > 2) {
573                 for (i = 0x12; i <= 0x16; i++)
574                         snd_opl3sa2_write(chip, i, chip->ctlregs[i]);
575         }
576         /* restore cs4231 */
577         chip->cs4231->resume(chip->cs4231);
578
579         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
580         return 0;
581 }
582 #endif /* CONFIG_PM */
583
584 #ifdef CONFIG_PNP
585 static int __init snd_opl3sa2_pnp(int dev, struct snd_opl3sa2 *chip,
586                                   struct pnp_dev *pdev)
587 {
588         struct pnp_resource_table * cfg;
589         int err;
590
591         cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
592         if (!cfg) {
593                 snd_printk(KERN_ERR PFX "cannot allocate pnp cfg\n");
594                 return -ENOMEM;
595         }
596         /* PnP initialization */
597         pnp_init_resource_table(cfg);
598         if (sb_port[dev] != SNDRV_AUTO_PORT)
599                 pnp_resource_change(&cfg->port_resource[0], sb_port[dev], 16);
600         if (wss_port[dev] != SNDRV_AUTO_PORT)
601                 pnp_resource_change(&cfg->port_resource[1], wss_port[dev], 8);
602         if (fm_port[dev] != SNDRV_AUTO_PORT)
603                 pnp_resource_change(&cfg->port_resource[2], fm_port[dev], 4);
604         if (midi_port[dev] != SNDRV_AUTO_PORT)
605                 pnp_resource_change(&cfg->port_resource[3], midi_port[dev], 2);
606         if (port[dev] != SNDRV_AUTO_PORT)
607                 pnp_resource_change(&cfg->port_resource[4], port[dev], 2);
608         if (dma1[dev] != SNDRV_AUTO_DMA)
609                 pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
610         if (dma2[dev] != SNDRV_AUTO_DMA)
611                 pnp_resource_change(&cfg->dma_resource[1], dma2[dev], 1);
612         if (irq[dev] != SNDRV_AUTO_IRQ)
613                 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
614         err = pnp_manual_config_dev(pdev, cfg, 0);
615         if (err < 0)
616                 snd_printk(KERN_WARNING "PnP manual resources are invalid, using auto config\n");
617         err = pnp_activate_dev(pdev);
618         if (err < 0) {
619                 kfree(cfg);
620                 snd_printk(KERN_ERR "PnP configure failure (out of resources?) err = %d\n", err);
621                 return -EBUSY;
622         }
623         sb_port[dev] = pnp_port_start(pdev, 0);
624         wss_port[dev] = pnp_port_start(pdev, 1);
625         fm_port[dev] = pnp_port_start(pdev, 2);
626         midi_port[dev] = pnp_port_start(pdev, 3);
627         port[dev] = pnp_port_start(pdev, 4);
628         dma1[dev] = pnp_dma(pdev, 0);
629         dma2[dev] = pnp_dma(pdev, 1);
630         irq[dev] = pnp_irq(pdev, 0);
631         snd_printdd("%sPnP OPL3-SA: sb port=0x%lx, wss port=0x%lx, fm port=0x%lx, midi port=0x%lx\n",
632                 pnp_device_is_pnpbios(pdev) ? "BIOS" : "ISA", sb_port[dev], wss_port[dev], fm_port[dev], midi_port[dev]);
633         snd_printdd("%sPnP OPL3-SA: control port=0x%lx, dma1=%i, dma2=%i, irq=%i\n",
634                 pnp_device_is_pnpbios(pdev) ? "BIOS" : "ISA", port[dev], dma1[dev], dma2[dev], irq[dev]);
635         kfree(cfg);
636         return 0;
637 }
638 #endif /* CONFIG_PNP */
639
640 static void snd_opl3sa2_free(struct snd_card *card)
641 {
642         struct snd_opl3sa2 *chip = card->private_data;
643         if (chip->irq >= 0)
644                 free_irq(chip->irq, (void *)chip);
645         release_and_free_resource(chip->res_port);
646 }
647
648 static struct snd_card *snd_opl3sa2_card_new(int dev)
649 {
650         struct snd_card *card;
651         struct snd_opl3sa2 *chip;
652
653         card = snd_card_new(index[dev], id[dev], THIS_MODULE, sizeof(struct snd_opl3sa2));
654         if (card == NULL)
655                 return NULL;
656         strcpy(card->driver, "OPL3SA2");
657         strcpy(card->shortname, "Yamaha OPL3-SA2");
658         chip = card->private_data;
659         spin_lock_init(&chip->reg_lock);
660         chip->irq = -1;
661         chip->card = card;
662         card->private_free = snd_opl3sa2_free;
663         return card;
664 }
665
666 static int __devinit snd_opl3sa2_probe(struct snd_card *card, int dev)
667 {
668         int xirq, xdma1, xdma2;
669         struct snd_opl3sa2 *chip;
670         struct snd_cs4231 *cs4231;
671         struct snd_opl3 *opl3;
672         int err;
673
674         /* initialise this card from supplied (or default) parameter*/ 
675         chip = card->private_data;
676         chip->ymode = opl3sa3_ymode[dev] & 0x03 ;
677         chip->port = port[dev];
678         xirq = irq[dev];
679         xdma1 = dma1[dev];
680         xdma2 = dma2[dev];
681         if (xdma2 < 0)
682                 chip->single_dma = 1;
683         if ((err = snd_opl3sa2_detect(chip)) < 0)
684                 return err;
685         if (request_irq(xirq, snd_opl3sa2_interrupt, SA_INTERRUPT, "OPL3-SA2", chip)) {
686                 snd_printk(KERN_ERR PFX "can't grab IRQ %d\n", xirq);
687                 return -ENODEV;
688         }
689         chip->irq = xirq;
690         if ((err = snd_cs4231_create(card,
691                                      wss_port[dev] + 4, -1,
692                                      xirq, xdma1, xdma2,
693                                      CS4231_HW_OPL3SA2,
694                                      CS4231_HWSHARE_IRQ,
695                                      &cs4231)) < 0) {
696                 snd_printd("Oops, WSS not detected at 0x%lx\n", wss_port[dev] + 4);
697                 return err;
698         }
699         chip->cs4231 = cs4231;
700         if ((err = snd_cs4231_pcm(cs4231, 0, NULL)) < 0)
701                 return err;
702         if ((err = snd_cs4231_mixer(cs4231)) < 0)
703                 return err;
704         if ((err = snd_opl3sa2_mixer(chip)) < 0)
705                 return err;
706         if ((err = snd_cs4231_timer(cs4231, 0, NULL)) < 0)
707                 return err;
708         if (fm_port[dev] >= 0x340 && fm_port[dev] < 0x400) {
709                 if ((err = snd_opl3_create(card, fm_port[dev],
710                                            fm_port[dev] + 2,
711                                            OPL3_HW_OPL3, 0, &opl3)) < 0)
712                         return err;
713                 if ((err = snd_opl3_timer_new(opl3, 1, 2)) < 0)
714                         return err;
715                 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, &chip->synth)) < 0)
716                         return err;
717         }
718         if (midi_port[dev] >= 0x300 && midi_port[dev] < 0x340) {
719                 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_OPL3SA2,
720                                                midi_port[dev], 0,
721                                                xirq, 0, &chip->rmidi)) < 0)
722                         return err;
723         }
724         sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
725                 card->shortname, chip->port, xirq, xdma1);
726         if (xdma2 >= 0)
727                 sprintf(card->longname + strlen(card->longname), "&%d", xdma2);
728
729         return snd_card_register(card);
730 }
731
732 #ifdef CONFIG_PNP
733 static int __devinit snd_opl3sa2_pnp_detect(struct pnp_dev *pdev,
734                                             const struct pnp_device_id *id)
735 {
736         static int dev;
737         int err;
738         struct snd_card *card;
739
740         if (pnp_device_is_isapnp(pdev))
741                 return -ENOENT; /* we have another procedure - card */
742         for (; dev < SNDRV_CARDS; dev++) {
743                 if (enable[dev] && isapnp[dev])
744                         break;
745         }
746         if (dev >= SNDRV_CARDS)
747                 return -ENODEV;
748
749         card = snd_opl3sa2_card_new(dev);
750         if (! card)
751                 return -ENOMEM;
752         if ((err = snd_opl3sa2_pnp(dev, card->private_data, pdev)) < 0) {
753                 snd_card_free(card);
754                 return err;
755         }
756         snd_card_set_dev(card, &pdev->dev);
757         if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
758                 snd_card_free(card);
759                 return err;
760         }
761         pnp_set_drvdata(pdev, card);
762         dev++;
763         return 0;
764 }
765
766 static void __devexit snd_opl3sa2_pnp_remove(struct pnp_dev * pdev)
767 {
768         snd_card_free(pnp_get_drvdata(pdev));
769         pnp_set_drvdata(pdev, NULL);
770 }
771
772 #ifdef CONFIG_PM
773 static int snd_opl3sa2_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
774 {
775         return snd_opl3sa2_suspend(pnp_get_drvdata(pdev), state);
776 }
777 static int snd_opl3sa2_pnp_resume(struct pnp_dev *pdev)
778 {
779         return snd_opl3sa2_resume(pnp_get_drvdata(pdev));
780 }
781 #endif
782
783 static struct pnp_driver opl3sa2_pnp_driver = {
784         .name = "snd-opl3sa2-pnpbios",
785         .id_table = snd_opl3sa2_pnpbiosids,
786         .probe = snd_opl3sa2_pnp_detect,
787         .remove = __devexit_p(snd_opl3sa2_pnp_remove),
788 #ifdef CONFIG_PM
789         .suspend = snd_opl3sa2_pnp_suspend,
790         .resume = snd_opl3sa2_pnp_resume,
791 #endif
792 };
793
794 static int __devinit snd_opl3sa2_pnp_cdetect(struct pnp_card_link *pcard,
795                                              const struct pnp_card_device_id *id)
796 {
797         static int dev;
798         struct pnp_dev *pdev;
799         int err;
800         struct snd_card *card;
801
802         pdev = pnp_request_card_device(pcard, id->devs[0].id, NULL);
803         if (pdev == NULL) {
804                 snd_printk(KERN_ERR PFX "can't get pnp device from id '%s'\n",
805                            id->devs[0].id);
806                 return -EBUSY;
807         }
808         for (; dev < SNDRV_CARDS; dev++) {
809                 if (enable[dev] && isapnp[dev])
810                         break;
811         }
812         if (dev >= SNDRV_CARDS)
813                 return -ENODEV;
814
815         card = snd_opl3sa2_card_new(dev);
816         if (! card)
817                 return -ENOMEM;
818         if ((err = snd_opl3sa2_pnp(dev, card->private_data, pdev)) < 0) {
819                 snd_card_free(card);
820                 return err;
821         }
822         snd_card_set_dev(card, &pdev->dev);
823         if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
824                 snd_card_free(card);
825                 return err;
826         }
827         pnp_set_card_drvdata(pcard, card);
828         dev++;
829         return 0;
830 }
831
832 static void __devexit snd_opl3sa2_pnp_cremove(struct pnp_card_link * pcard)
833 {
834         snd_card_free(pnp_get_card_drvdata(pcard));
835         pnp_set_card_drvdata(pcard, NULL);
836 }
837
838 #ifdef CONFIG_PM
839 static int snd_opl3sa2_pnp_csuspend(struct pnp_card_link *pcard, pm_message_t state)
840 {
841         return snd_opl3sa2_suspend(pnp_get_card_drvdata(pcard), state);
842 }
843 static int snd_opl3sa2_pnp_cresume(struct pnp_card_link *pcard)
844 {
845         return snd_opl3sa2_resume(pnp_get_card_drvdata(pcard));
846 }
847 #endif
848
849 static struct pnp_card_driver opl3sa2_pnpc_driver = {
850         .flags = PNP_DRIVER_RES_DISABLE,
851         .name = "snd-opl3sa2-cpnp",
852         .id_table = snd_opl3sa2_pnpids,
853         .probe = snd_opl3sa2_pnp_cdetect,
854         .remove = __devexit_p(snd_opl3sa2_pnp_cremove),
855 #ifdef CONFIG_PM
856         .suspend = snd_opl3sa2_pnp_csuspend,
857         .resume = snd_opl3sa2_pnp_cresume,
858 #endif
859 };
860 #endif /* CONFIG_PNP */
861
862 static int __init snd_opl3sa2_nonpnp_probe(struct platform_device *pdev)
863 {
864         struct snd_card *card;
865         int err;
866         int dev = pdev->id;
867
868         if (port[dev] == SNDRV_AUTO_PORT) {
869                 snd_printk(KERN_ERR PFX "specify port\n");
870                 return -EINVAL;
871         }
872         if (wss_port[dev] == SNDRV_AUTO_PORT) {
873                 snd_printk(KERN_ERR PFX "specify wss_port\n");
874                 return -EINVAL;
875         }
876         if (fm_port[dev] == SNDRV_AUTO_PORT) {
877                 snd_printk(KERN_ERR PFX "specify fm_port\n");
878                 return -EINVAL;
879         }
880         if (midi_port[dev] == SNDRV_AUTO_PORT) {
881                 snd_printk(KERN_ERR PFX "specify midi_port\n");
882                 return -EINVAL;
883         }
884
885         card = snd_opl3sa2_card_new(dev);
886         if (! card)
887                 return -ENOMEM;
888         snd_card_set_dev(card, &pdev->dev);
889         if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
890                 snd_card_free(card);
891                 return err;
892         }
893         platform_set_drvdata(pdev, card);
894         return 0;
895 }
896
897 static int snd_opl3sa2_nonpnp_remove(struct platform_device *devptr)
898 {
899         snd_card_free(platform_get_drvdata(devptr));
900         platform_set_drvdata(devptr, NULL);
901         return 0;
902 }
903
904 #ifdef CONFIG_PM
905 static int snd_opl3sa2_nonpnp_suspend(struct platform_device *dev, pm_message_t state)
906 {
907         return snd_opl3sa2_suspend(platform_get_drvdata(dev), state);
908 }
909
910 static int snd_opl3sa2_nonpnp_resume(struct platform_device *dev)
911 {
912         return snd_opl3sa2_resume(platform_get_drvdata(dev));
913 }
914 #endif
915
916 #define OPL3SA2_DRIVER  "snd_opl3sa2"
917
918 static struct platform_driver snd_opl3sa2_nonpnp_driver = {
919         .probe          = snd_opl3sa2_nonpnp_probe,
920         .remove         = snd_opl3sa2_nonpnp_remove,
921 #ifdef CONFIG_PM
922         .suspend        = snd_opl3sa2_nonpnp_suspend,
923         .resume         = snd_opl3sa2_nonpnp_resume,
924 #endif
925         .driver         = {
926                 .name   = OPL3SA2_DRIVER
927         },
928 };
929
930 static void __init_or_module snd_opl3sa2_unregister_all(void)
931 {
932         int i;
933
934 #ifdef CONFIG_PNP
935         if (pnpc_registered)
936                 pnp_unregister_card_driver(&opl3sa2_pnpc_driver);
937         if (pnp_registered)
938                 pnp_unregister_driver(&opl3sa2_pnp_driver);
939 #endif
940         for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
941                 platform_device_unregister(platform_devices[i]);
942         platform_driver_unregister(&snd_opl3sa2_nonpnp_driver);
943 }
944
945 static int __init alsa_card_opl3sa2_init(void)
946 {
947         int i, err, cards = 0;
948
949         if ((err = platform_driver_register(&snd_opl3sa2_nonpnp_driver)) < 0)
950                 return err;
951
952         for (i = 0; i < SNDRV_CARDS; i++) {
953                 struct platform_device *device;
954                 if (! enable[i])
955                         continue;
956 #ifdef CONFIG_PNP
957                 if (isapnp[i])
958                         continue;
959 #endif
960                 device = platform_device_register_simple(OPL3SA2_DRIVER,
961                                                          i, NULL, 0);
962                 if (IS_ERR(device)) {
963                         err = PTR_ERR(device);
964                         goto errout;
965                 }
966                 platform_devices[i] = device;
967                 cards++;
968         }
969
970 #ifdef CONFIG_PNP
971         err = pnp_register_driver(&opl3sa2_pnp_driver);
972         if (err >= 0) {
973                 pnp_registered = 1;
974                 cards += err;
975         }
976         err = pnp_register_card_driver(&opl3sa2_pnpc_driver);
977         if (err >= 0) {
978                 pnpc_registered = 1;
979                 cards += err;
980         }
981 #endif
982
983         if (!cards) {
984 #ifdef MODULE
985                 snd_printk(KERN_ERR "Yamaha OPL3-SA soundcard not found or device busy\n");
986 #endif
987                 err = -ENODEV;
988                 goto errout;
989         }
990         return 0;
991
992  errout:
993         snd_opl3sa2_unregister_all();
994         return err;
995 }
996
997 static void __exit alsa_card_opl3sa2_exit(void)
998 {
999         snd_opl3sa2_unregister_all();
1000 }
1001
1002 module_init(alsa_card_opl3sa2_init)
1003 module_exit(alsa_card_opl3sa2_exit)