Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *   ALSA soundcard driver for Miro miroSOUND PCM1 pro
0004  *                                  miroSOUND PCM12
0005  *                                  miroSOUND PCM20 Radio
0006  *
0007  *   Copyright (C) 2004-2005 Martin Langer <martin-langer@gmx.de>
0008  *
0009  *   Based on OSS ACI and ALSA OPTi9xx drivers
0010  */
0011 
0012 #include <linux/init.h>
0013 #include <linux/err.h>
0014 #include <linux/isa.h>
0015 #include <linux/pnp.h>
0016 #include <linux/delay.h>
0017 #include <linux/ioport.h>
0018 #include <linux/module.h>
0019 #include <linux/io.h>
0020 #include <asm/dma.h>
0021 #include <sound/core.h>
0022 #include <sound/wss.h>
0023 #include <sound/mpu401.h>
0024 #include <sound/opl4.h>
0025 #include <sound/control.h>
0026 #include <sound/info.h>
0027 #define SNDRV_LEGACY_FIND_FREE_IOPORT
0028 #define SNDRV_LEGACY_FIND_FREE_IRQ
0029 #define SNDRV_LEGACY_FIND_FREE_DMA
0030 #include <sound/initval.h>
0031 #include <sound/aci.h>
0032 
0033 MODULE_AUTHOR("Martin Langer <martin-langer@gmx.de>");
0034 MODULE_LICENSE("GPL");
0035 MODULE_DESCRIPTION("Miro miroSOUND PCM1 pro, PCM12, PCM20 Radio");
0036 
0037 static int index = SNDRV_DEFAULT_IDX1;      /* Index 0-MAX */
0038 static char *id = SNDRV_DEFAULT_STR1;       /* ID for this card */
0039 static long port = SNDRV_DEFAULT_PORT1;     /* 0x530,0xe80,0xf40,0x604 */
0040 static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */
0041 static long fm_port = SNDRV_DEFAULT_PORT1;  /* 0x388 */
0042 static int irq = SNDRV_DEFAULT_IRQ1;        /* 5,7,9,10,11 */
0043 static int mpu_irq = SNDRV_DEFAULT_IRQ1;    /* 5,7,9,10 */
0044 static int dma1 = SNDRV_DEFAULT_DMA1;       /* 0,1,3 */
0045 static int dma2 = SNDRV_DEFAULT_DMA1;       /* 0,1,3 */
0046 static int wss;
0047 static int ide;
0048 #ifdef CONFIG_PNP
0049 static bool isapnp = 1;             /* Enable ISA PnP detection */
0050 #endif
0051 
0052 module_param(index, int, 0444);
0053 MODULE_PARM_DESC(index, "Index value for miro soundcard.");
0054 module_param(id, charp, 0444);
0055 MODULE_PARM_DESC(id, "ID string for miro soundcard.");
0056 module_param_hw(port, long, ioport, 0444);
0057 MODULE_PARM_DESC(port, "WSS port # for miro driver.");
0058 module_param_hw(mpu_port, long, ioport, 0444);
0059 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for miro driver.");
0060 module_param_hw(fm_port, long, ioport, 0444);
0061 MODULE_PARM_DESC(fm_port, "FM Port # for miro driver.");
0062 module_param_hw(irq, int, irq, 0444);
0063 MODULE_PARM_DESC(irq, "WSS irq # for miro driver.");
0064 module_param_hw(mpu_irq, int, irq, 0444);
0065 MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for miro driver.");
0066 module_param_hw(dma1, int, dma, 0444);
0067 MODULE_PARM_DESC(dma1, "1st dma # for miro driver.");
0068 module_param_hw(dma2, int, dma, 0444);
0069 MODULE_PARM_DESC(dma2, "2nd dma # for miro driver.");
0070 module_param(wss, int, 0444);
0071 MODULE_PARM_DESC(wss, "wss mode");
0072 module_param(ide, int, 0444);
0073 MODULE_PARM_DESC(ide, "enable ide port");
0074 #ifdef CONFIG_PNP
0075 module_param(isapnp, bool, 0444);
0076 MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
0077 #endif
0078 
0079 #define OPTi9XX_HW_DETECT   0
0080 #define OPTi9XX_HW_82C928   1
0081 #define OPTi9XX_HW_82C929   2
0082 #define OPTi9XX_HW_82C924   3
0083 #define OPTi9XX_HW_82C925   4
0084 #define OPTi9XX_HW_82C930   5
0085 #define OPTi9XX_HW_82C931   6
0086 #define OPTi9XX_HW_82C933   7
0087 #define OPTi9XX_HW_LAST     OPTi9XX_HW_82C933
0088 
0089 #define OPTi9XX_MC_REG(n)   n
0090 
0091 struct snd_miro {
0092     unsigned short hardware;
0093     unsigned char password;
0094     char name[7];
0095 
0096     struct resource *res_mc_base;
0097     struct resource *res_aci_port;
0098 
0099     unsigned long mc_base;
0100     unsigned long mc_base_size;
0101     unsigned long pwd_reg;
0102 
0103     spinlock_t lock;
0104     struct snd_pcm *pcm;
0105 
0106     long wss_base;
0107     int irq;
0108     int dma1;
0109     int dma2;
0110 
0111     long mpu_port;
0112     int mpu_irq;
0113 
0114     struct snd_miro_aci *aci;
0115 };
0116 
0117 static struct snd_miro_aci aci_device;
0118 
0119 static const char * const snd_opti9xx_names[] = {
0120     "unknown",
0121     "82C928", "82C929",
0122     "82C924", "82C925",
0123     "82C930", "82C931", "82C933"
0124 };
0125 
0126 static int snd_miro_pnp_is_probed;
0127 
0128 #ifdef CONFIG_PNP
0129 
0130 static const struct pnp_card_device_id snd_miro_pnpids[] = {
0131     /* PCM20 and PCM12 in PnP mode */
0132     { .id = "MIR0924",
0133       .devs = { { "MIR0000" }, { "MIR0002" }, { "MIR0005" } }, },
0134     { .id = "" }
0135 };
0136 
0137 MODULE_DEVICE_TABLE(pnp_card, snd_miro_pnpids);
0138 
0139 #endif  /* CONFIG_PNP */
0140 
0141 /* 
0142  *  ACI control
0143  */
0144 
0145 static int aci_busy_wait(struct snd_miro_aci *aci)
0146 {
0147     long timeout;
0148     unsigned char byte;
0149 
0150     for (timeout = 1; timeout <= ACI_MINTIME + 30; timeout++) {
0151         byte = inb(aci->aci_port + ACI_REG_BUSY);
0152         if ((byte & 1) == 0) {
0153             if (timeout >= ACI_MINTIME)
0154                 snd_printd("aci ready in round %ld.\n",
0155                        timeout-ACI_MINTIME);
0156             return byte;
0157         }
0158         if (timeout >= ACI_MINTIME) {
0159             long out=10*HZ;
0160             switch (timeout-ACI_MINTIME) {
0161             case 0 ... 9:
0162                 out /= 10;
0163                 fallthrough;
0164             case 10 ... 19:
0165                 out /= 10;
0166                 fallthrough;
0167             case 20 ... 30:
0168                 out /= 10;
0169                 fallthrough;
0170             default:
0171                 set_current_state(TASK_UNINTERRUPTIBLE);
0172                 schedule_timeout(out);
0173                 break;
0174             }
0175         }
0176     }
0177     snd_printk(KERN_ERR "aci_busy_wait() time out\n");
0178     return -EBUSY;
0179 }
0180 
0181 static inline int aci_write(struct snd_miro_aci *aci, unsigned char byte)
0182 {
0183     if (aci_busy_wait(aci) >= 0) {
0184         outb(byte, aci->aci_port + ACI_REG_COMMAND);
0185         return 0;
0186     } else {
0187         snd_printk(KERN_ERR "aci busy, aci_write(0x%x) stopped.\n", byte);
0188         return -EBUSY;
0189     }
0190 }
0191 
0192 static inline int aci_read(struct snd_miro_aci *aci)
0193 {
0194     unsigned char byte;
0195 
0196     if (aci_busy_wait(aci) >= 0) {
0197         byte = inb(aci->aci_port + ACI_REG_STATUS);
0198         return byte;
0199     } else {
0200         snd_printk(KERN_ERR "aci busy, aci_read() stopped.\n");
0201         return -EBUSY;
0202     }
0203 }
0204 
0205 int snd_aci_cmd(struct snd_miro_aci *aci, int write1, int write2, int write3)
0206 {
0207     int write[] = {write1, write2, write3};
0208     int value, i;
0209 
0210     if (mutex_lock_interruptible(&aci->aci_mutex))
0211         return -EINTR;
0212 
0213     for (i=0; i<3; i++) {
0214         if (write[i]< 0 || write[i] > 255)
0215             break;
0216         else {
0217             value = aci_write(aci, write[i]);
0218             if (value < 0)
0219                 goto out;
0220         }
0221     }
0222 
0223     value = aci_read(aci);
0224 
0225 out:    mutex_unlock(&aci->aci_mutex);
0226     return value;
0227 }
0228 EXPORT_SYMBOL(snd_aci_cmd);
0229 
0230 static int aci_getvalue(struct snd_miro_aci *aci, unsigned char index)
0231 {
0232     return snd_aci_cmd(aci, ACI_STATUS, index, -1);
0233 }
0234 
0235 static int aci_setvalue(struct snd_miro_aci *aci, unsigned char index,
0236             int value)
0237 {
0238     return snd_aci_cmd(aci, index, value, -1);
0239 }
0240 
0241 struct snd_miro_aci *snd_aci_get_aci(void)
0242 {
0243     if (aci_device.aci_port == 0)
0244         return NULL;
0245     return &aci_device;
0246 }
0247 EXPORT_SYMBOL(snd_aci_get_aci);
0248 
0249 /*
0250  *  MIXER part
0251  */
0252 
0253 #define snd_miro_info_capture   snd_ctl_boolean_mono_info
0254 
0255 static int snd_miro_get_capture(struct snd_kcontrol *kcontrol,
0256                 struct snd_ctl_elem_value *ucontrol)
0257 {
0258     struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
0259     int value;
0260 
0261     value = aci_getvalue(miro->aci, ACI_S_GENERAL);
0262     if (value < 0) {
0263         snd_printk(KERN_ERR "snd_miro_get_capture() failed: %d\n",
0264                value);
0265         return value;
0266     }
0267 
0268     ucontrol->value.integer.value[0] = value & 0x20;
0269 
0270     return 0;
0271 }
0272 
0273 static int snd_miro_put_capture(struct snd_kcontrol *kcontrol,
0274                 struct snd_ctl_elem_value *ucontrol)
0275 {
0276     struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
0277     int change, value, error;
0278 
0279     value = !(ucontrol->value.integer.value[0]);
0280 
0281     error = aci_setvalue(miro->aci, ACI_SET_SOLOMODE, value);
0282     if (error < 0) {
0283         snd_printk(KERN_ERR "snd_miro_put_capture() failed: %d\n",
0284                error);
0285         return error;
0286     }
0287 
0288     change = (value != miro->aci->aci_solomode);
0289     miro->aci->aci_solomode = value;
0290     
0291     return change;
0292 }
0293 
0294 static int snd_miro_info_preamp(struct snd_kcontrol *kcontrol,
0295                 struct snd_ctl_elem_info *uinfo)
0296 {
0297     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0298     uinfo->count = 1;
0299     uinfo->value.integer.min = 0;
0300     uinfo->value.integer.max = 3;
0301 
0302     return 0;
0303 }
0304 
0305 static int snd_miro_get_preamp(struct snd_kcontrol *kcontrol,
0306                    struct snd_ctl_elem_value *ucontrol)
0307 {
0308     struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
0309     int value;
0310 
0311     if (miro->aci->aci_version <= 176) {
0312 
0313         /* 
0314            OSS says it's not readable with versions < 176.
0315            But it doesn't work on my card,
0316            which is a PCM12 with aci_version = 176.
0317         */
0318 
0319         ucontrol->value.integer.value[0] = miro->aci->aci_preamp;
0320         return 0;
0321     }
0322 
0323     value = aci_getvalue(miro->aci, ACI_GET_PREAMP);
0324     if (value < 0) {
0325         snd_printk(KERN_ERR "snd_miro_get_preamp() failed: %d\n",
0326                value);
0327         return value;
0328     }
0329     
0330     ucontrol->value.integer.value[0] = value;
0331 
0332     return 0;
0333 }
0334 
0335 static int snd_miro_put_preamp(struct snd_kcontrol *kcontrol,
0336                    struct snd_ctl_elem_value *ucontrol)
0337 {
0338     struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
0339     int error, value, change;
0340 
0341     value = ucontrol->value.integer.value[0];
0342 
0343     error = aci_setvalue(miro->aci, ACI_SET_PREAMP, value);
0344     if (error < 0) {
0345         snd_printk(KERN_ERR "snd_miro_put_preamp() failed: %d\n",
0346                error);
0347         return error;
0348     }
0349 
0350     change = (value != miro->aci->aci_preamp);
0351     miro->aci->aci_preamp = value;
0352 
0353     return change;
0354 }
0355 
0356 #define snd_miro_info_amp   snd_ctl_boolean_mono_info
0357 
0358 static int snd_miro_get_amp(struct snd_kcontrol *kcontrol,
0359                 struct snd_ctl_elem_value *ucontrol)
0360 {
0361     struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
0362     ucontrol->value.integer.value[0] = miro->aci->aci_amp;
0363 
0364     return 0;
0365 }
0366 
0367 static int snd_miro_put_amp(struct snd_kcontrol *kcontrol,
0368                 struct snd_ctl_elem_value *ucontrol)
0369 {
0370     struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
0371     int error, value, change;
0372 
0373     value = ucontrol->value.integer.value[0];
0374 
0375     error = aci_setvalue(miro->aci, ACI_SET_POWERAMP, value);
0376     if (error < 0) {
0377         snd_printk(KERN_ERR "snd_miro_put_amp() to %d failed: %d\n", value, error);
0378         return error;
0379     }
0380 
0381     change = (value != miro->aci->aci_amp);
0382     miro->aci->aci_amp = value;
0383 
0384     return change;
0385 }
0386 
0387 #define MIRO_DOUBLE(ctl_name, ctl_index, get_right_reg, set_right_reg) \
0388 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
0389   .name = ctl_name, \
0390   .index = ctl_index, \
0391   .info = snd_miro_info_double, \
0392   .get = snd_miro_get_double, \
0393   .put = snd_miro_put_double, \
0394   .private_value = get_right_reg | (set_right_reg << 8) \
0395 }
0396 
0397 static int snd_miro_info_double(struct snd_kcontrol *kcontrol, 
0398                 struct snd_ctl_elem_info *uinfo)
0399 {
0400     int reg = kcontrol->private_value & 0xff;
0401 
0402     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0403     uinfo->count = 2;
0404 
0405     if ((reg >= ACI_GET_EQ1) && (reg <= ACI_GET_EQ7)) {
0406 
0407         /* equalizer elements */
0408 
0409         uinfo->value.integer.min = - 0x7f;
0410         uinfo->value.integer.max = 0x7f;
0411     } else {
0412 
0413         /* non-equalizer elements */
0414 
0415         uinfo->value.integer.min = 0;
0416         uinfo->value.integer.max = 0x20;
0417     }
0418 
0419     return 0;
0420 }
0421 
0422 static int snd_miro_get_double(struct snd_kcontrol *kcontrol, 
0423                    struct snd_ctl_elem_value *uinfo)
0424 {
0425     struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
0426     int left_val, right_val;
0427 
0428     int right_reg = kcontrol->private_value & 0xff;
0429     int left_reg = right_reg + 1;
0430 
0431     right_val = aci_getvalue(miro->aci, right_reg);
0432     if (right_val < 0) {
0433         snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", right_reg, right_val);
0434         return right_val;
0435     }
0436 
0437     left_val = aci_getvalue(miro->aci, left_reg);
0438     if (left_val < 0) {
0439         snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", left_reg, left_val);
0440         return left_val;
0441     }
0442 
0443     if ((right_reg >= ACI_GET_EQ1) && (right_reg <= ACI_GET_EQ7)) {
0444 
0445         /* equalizer elements */
0446 
0447         if (left_val < 0x80) {
0448             uinfo->value.integer.value[0] = left_val;
0449         } else {
0450             uinfo->value.integer.value[0] = 0x80 - left_val;
0451         }
0452 
0453         if (right_val < 0x80) {
0454             uinfo->value.integer.value[1] = right_val;
0455         } else {
0456             uinfo->value.integer.value[1] = 0x80 - right_val;
0457         }
0458 
0459     } else {
0460 
0461         /* non-equalizer elements */
0462 
0463         uinfo->value.integer.value[0] = 0x20 - left_val;
0464         uinfo->value.integer.value[1] = 0x20 - right_val;
0465     }
0466 
0467     return 0;
0468 }
0469 
0470 static int snd_miro_put_double(struct snd_kcontrol *kcontrol, 
0471                    struct snd_ctl_elem_value *ucontrol)
0472 {
0473     struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
0474     struct snd_miro_aci *aci = miro->aci;
0475     int left, right, left_old, right_old;
0476     int setreg_left, setreg_right, getreg_left, getreg_right;
0477     int change, error;
0478 
0479     left = ucontrol->value.integer.value[0];
0480     right = ucontrol->value.integer.value[1];
0481 
0482     setreg_right = (kcontrol->private_value >> 8) & 0xff;
0483     setreg_left = setreg_right + 8;
0484     if (setreg_right == ACI_SET_MASTER)
0485         setreg_left -= 7;
0486 
0487     getreg_right = kcontrol->private_value & 0xff;
0488     getreg_left = getreg_right + 1;
0489 
0490     left_old = aci_getvalue(aci, getreg_left);
0491     if (left_old < 0) {
0492         snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", getreg_left, left_old);
0493         return left_old;
0494     }
0495 
0496     right_old = aci_getvalue(aci, getreg_right);
0497     if (right_old < 0) {
0498         snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", getreg_right, right_old);
0499         return right_old;
0500     }
0501 
0502     if ((getreg_right >= ACI_GET_EQ1) && (getreg_right <= ACI_GET_EQ7)) {
0503 
0504         /* equalizer elements */
0505 
0506         if (left < -0x7f || left > 0x7f ||
0507             right < -0x7f || right > 0x7f)
0508             return -EINVAL;
0509 
0510         if (left_old > 0x80) 
0511             left_old = 0x80 - left_old;
0512         if (right_old > 0x80) 
0513             right_old = 0x80 - right_old;
0514 
0515         if (left >= 0) {
0516             error = aci_setvalue(aci, setreg_left, left);
0517             if (error < 0) {
0518                 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
0519                        left, error);
0520                 return error;
0521             }
0522         } else {
0523             error = aci_setvalue(aci, setreg_left, 0x80 - left);
0524             if (error < 0) {
0525                 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
0526                        0x80 - left, error);
0527                 return error;
0528             }
0529         }
0530 
0531         if (right >= 0) {
0532             error = aci_setvalue(aci, setreg_right, right);
0533             if (error < 0) {
0534                 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
0535                        right, error);
0536                 return error;
0537             }
0538         } else {
0539             error = aci_setvalue(aci, setreg_right, 0x80 - right);
0540             if (error < 0) {
0541                 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
0542                        0x80 - right, error);
0543                 return error;
0544             }
0545         }
0546 
0547     } else {
0548 
0549         /* non-equalizer elements */
0550 
0551         if (left < 0 || left > 0x20 ||
0552             right < 0 || right > 0x20)
0553             return -EINVAL;
0554 
0555         left_old = 0x20 - left_old;
0556         right_old = 0x20 - right_old;
0557 
0558         error = aci_setvalue(aci, setreg_left, 0x20 - left);
0559         if (error < 0) {
0560             snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
0561                    0x20 - left, error);
0562             return error;
0563         }
0564         error = aci_setvalue(aci, setreg_right, 0x20 - right);
0565         if (error < 0) {
0566             snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
0567                    0x20 - right, error);
0568             return error;
0569         }
0570     }
0571 
0572     change = (left != left_old) || (right != right_old);
0573 
0574     return change;
0575 }
0576 
0577 static const struct snd_kcontrol_new snd_miro_controls[] = {
0578 MIRO_DOUBLE("Master Playback Volume", 0, ACI_GET_MASTER, ACI_SET_MASTER),
0579 MIRO_DOUBLE("Mic Playback Volume", 1, ACI_GET_MIC, ACI_SET_MIC),
0580 MIRO_DOUBLE("Line Playback Volume", 1, ACI_GET_LINE, ACI_SET_LINE),
0581 MIRO_DOUBLE("CD Playback Volume", 0, ACI_GET_CD, ACI_SET_CD),
0582 MIRO_DOUBLE("Synth Playback Volume", 0, ACI_GET_SYNTH, ACI_SET_SYNTH),
0583 MIRO_DOUBLE("PCM Playback Volume", 1, ACI_GET_PCM, ACI_SET_PCM),
0584 MIRO_DOUBLE("Aux Playback Volume", 2, ACI_GET_LINE2, ACI_SET_LINE2),
0585 };
0586 
0587 /* Equalizer with seven bands (only PCM20) 
0588    from -12dB up to +12dB on each band */
0589 static const struct snd_kcontrol_new snd_miro_eq_controls[] = {
0590 MIRO_DOUBLE("Tone Control - 28 Hz", 0, ACI_GET_EQ1, ACI_SET_EQ1),
0591 MIRO_DOUBLE("Tone Control - 160 Hz", 0, ACI_GET_EQ2, ACI_SET_EQ2),
0592 MIRO_DOUBLE("Tone Control - 400 Hz", 0, ACI_GET_EQ3, ACI_SET_EQ3),
0593 MIRO_DOUBLE("Tone Control - 1 kHz", 0, ACI_GET_EQ4, ACI_SET_EQ4),
0594 MIRO_DOUBLE("Tone Control - 2.5 kHz", 0, ACI_GET_EQ5, ACI_SET_EQ5),
0595 MIRO_DOUBLE("Tone Control - 6.3 kHz", 0, ACI_GET_EQ6, ACI_SET_EQ6),
0596 MIRO_DOUBLE("Tone Control - 16 kHz", 0, ACI_GET_EQ7, ACI_SET_EQ7),
0597 };
0598 
0599 static const struct snd_kcontrol_new snd_miro_radio_control[] = {
0600 MIRO_DOUBLE("Radio Playback Volume", 0, ACI_GET_LINE1, ACI_SET_LINE1),
0601 };
0602 
0603 static const struct snd_kcontrol_new snd_miro_line_control[] = {
0604 MIRO_DOUBLE("Line Playback Volume", 2, ACI_GET_LINE1, ACI_SET_LINE1),
0605 };
0606 
0607 static const struct snd_kcontrol_new snd_miro_preamp_control[] = {
0608 {
0609     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0610     .name = "Mic Boost",
0611     .index = 1,
0612     .info = snd_miro_info_preamp,
0613     .get = snd_miro_get_preamp,
0614     .put = snd_miro_put_preamp,
0615 }};
0616 
0617 static const struct snd_kcontrol_new snd_miro_amp_control[] = {
0618 {
0619     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0620     .name = "Line Boost",
0621     .index = 0,
0622     .info = snd_miro_info_amp,
0623     .get = snd_miro_get_amp,
0624     .put = snd_miro_put_amp,
0625 }};
0626 
0627 static const struct snd_kcontrol_new snd_miro_capture_control[] = {
0628 {
0629     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0630     .name = "PCM Capture Switch",
0631     .index = 0,
0632     .info = snd_miro_info_capture,
0633     .get = snd_miro_get_capture,
0634     .put = snd_miro_put_capture,
0635 }};
0636 
0637 static const unsigned char aci_init_values[][2] = {
0638     { ACI_SET_MUTE, 0x00 },
0639     { ACI_SET_POWERAMP, 0x00 },
0640     { ACI_SET_PREAMP, 0x00 },
0641     { ACI_SET_SOLOMODE, 0x00 },
0642     { ACI_SET_MIC + 0, 0x20 },
0643     { ACI_SET_MIC + 8, 0x20 },
0644     { ACI_SET_LINE + 0, 0x20 },
0645     { ACI_SET_LINE + 8, 0x20 },
0646     { ACI_SET_CD + 0, 0x20 },
0647     { ACI_SET_CD + 8, 0x20 },
0648     { ACI_SET_PCM + 0, 0x20 },
0649     { ACI_SET_PCM + 8, 0x20 },
0650     { ACI_SET_LINE1 + 0, 0x20 },
0651     { ACI_SET_LINE1 + 8, 0x20 },
0652     { ACI_SET_LINE2 + 0, 0x20 },
0653     { ACI_SET_LINE2 + 8, 0x20 },
0654     { ACI_SET_SYNTH + 0, 0x20 },
0655     { ACI_SET_SYNTH + 8, 0x20 },
0656     { ACI_SET_MASTER + 0, 0x20 },
0657     { ACI_SET_MASTER + 1, 0x20 },
0658 };
0659 
0660 static int snd_set_aci_init_values(struct snd_miro *miro)
0661 {
0662     int idx, error;
0663     struct snd_miro_aci *aci = miro->aci;
0664 
0665     /* enable WSS on PCM1 */
0666 
0667     if ((aci->aci_product == 'A') && wss) {
0668         error = aci_setvalue(aci, ACI_SET_WSS, wss);
0669         if (error < 0) {
0670             snd_printk(KERN_ERR "enabling WSS mode failed\n");
0671             return error;
0672         }
0673     }
0674 
0675     /* enable IDE port */
0676 
0677     if (ide) {
0678         error = aci_setvalue(aci, ACI_SET_IDE, ide);
0679         if (error < 0) {
0680             snd_printk(KERN_ERR "enabling IDE port failed\n");
0681             return error;
0682         }
0683     }
0684 
0685     /* set common aci values */
0686 
0687     for (idx = 0; idx < ARRAY_SIZE(aci_init_values); idx++) {
0688         error = aci_setvalue(aci, aci_init_values[idx][0],
0689                      aci_init_values[idx][1]);
0690         if (error < 0) {
0691             snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n", 
0692                    aci_init_values[idx][0], error);
0693                         return error;
0694                 }
0695     }
0696     aci->aci_amp = 0;
0697     aci->aci_preamp = 0;
0698     aci->aci_solomode = 1;
0699 
0700     return 0;
0701 }
0702 
0703 static int snd_miro_mixer(struct snd_card *card,
0704               struct snd_miro *miro)
0705 {
0706     unsigned int idx;
0707     int err;
0708 
0709     if (snd_BUG_ON(!miro || !card))
0710         return -EINVAL;
0711 
0712     switch (miro->hardware) {
0713     case OPTi9XX_HW_82C924:
0714         strcpy(card->mixername, "ACI & OPTi924");
0715         break;
0716     case OPTi9XX_HW_82C929:
0717         strcpy(card->mixername, "ACI & OPTi929");
0718         break;
0719     default:
0720         snd_BUG();
0721         break;
0722     }
0723 
0724     for (idx = 0; idx < ARRAY_SIZE(snd_miro_controls); idx++) {
0725         err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_controls[idx], miro));
0726         if (err < 0)
0727             return err;
0728     }
0729 
0730     if ((miro->aci->aci_product == 'A') ||
0731         (miro->aci->aci_product == 'B')) {
0732         /* PCM1/PCM12 with power-amp and Line 2 */
0733         err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_line_control[0], miro));
0734         if (err < 0)
0735             return err;
0736         err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_amp_control[0], miro));
0737         if (err < 0)
0738             return err;
0739     }
0740 
0741     if ((miro->aci->aci_product == 'B') ||
0742         (miro->aci->aci_product == 'C')) {
0743         /* PCM12/PCM20 with mic-preamp */
0744         err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_preamp_control[0], miro));
0745         if (err < 0)
0746             return err;
0747         if (miro->aci->aci_version >= 176) {
0748             err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_capture_control[0], miro));
0749             if (err < 0)
0750                 return err;
0751         }
0752     }
0753 
0754     if (miro->aci->aci_product == 'C') {
0755         /* PCM20 with radio and 7 band equalizer */
0756         err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_radio_control[0], miro));
0757         if (err < 0)
0758             return err;
0759         for (idx = 0; idx < ARRAY_SIZE(snd_miro_eq_controls); idx++) {
0760             err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_eq_controls[idx], miro));
0761             if (err < 0)
0762                 return err;
0763         }
0764     }
0765 
0766     return 0;
0767 }
0768 
0769 static int snd_miro_init(struct snd_miro *chip,
0770              unsigned short hardware)
0771 {
0772     static const int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
0773 
0774     chip->hardware = hardware;
0775     strcpy(chip->name, snd_opti9xx_names[hardware]);
0776 
0777     chip->mc_base_size = opti9xx_mc_size[hardware];  
0778 
0779     spin_lock_init(&chip->lock);
0780 
0781     chip->wss_base = -1;
0782     chip->irq = -1;
0783     chip->dma1 = -1;
0784     chip->dma2 = -1;
0785     chip->mpu_port = -1;
0786     chip->mpu_irq = -1;
0787 
0788     chip->pwd_reg = 3;
0789 
0790 #ifdef CONFIG_PNP
0791     if (isapnp && chip->mc_base)
0792         /* PnP resource gives the least 10 bits */
0793         chip->mc_base |= 0xc00;
0794     else
0795 #endif
0796         chip->mc_base = 0xf8c;
0797 
0798     switch (hardware) {
0799     case OPTi9XX_HW_82C929:
0800         chip->password = 0xe3;
0801         break;
0802 
0803     case OPTi9XX_HW_82C924:
0804         chip->password = 0xe5;
0805         break;
0806 
0807     default:
0808         snd_printk(KERN_ERR "sorry, no support for %d\n", hardware);
0809         return -ENODEV;
0810     }
0811 
0812     return 0;
0813 }
0814 
0815 static unsigned char snd_miro_read(struct snd_miro *chip,
0816                    unsigned char reg)
0817 {
0818     unsigned long flags;
0819     unsigned char retval = 0xff;
0820 
0821     spin_lock_irqsave(&chip->lock, flags);
0822     outb(chip->password, chip->mc_base + chip->pwd_reg);
0823 
0824     switch (chip->hardware) {
0825     case OPTi9XX_HW_82C924:
0826         if (reg > 7) {
0827             outb(reg, chip->mc_base + 8);
0828             outb(chip->password, chip->mc_base + chip->pwd_reg);
0829             retval = inb(chip->mc_base + 9);
0830             break;
0831         }
0832         fallthrough;
0833 
0834     case OPTi9XX_HW_82C929:
0835         retval = inb(chip->mc_base + reg);
0836         break;
0837 
0838     default:
0839         snd_printk(KERN_ERR "sorry, no support for %d\n", chip->hardware);
0840     }
0841 
0842     spin_unlock_irqrestore(&chip->lock, flags);
0843     return retval;
0844 }
0845 
0846 static void snd_miro_write(struct snd_miro *chip, unsigned char reg,
0847                unsigned char value)
0848 {
0849     unsigned long flags;
0850 
0851     spin_lock_irqsave(&chip->lock, flags);
0852     outb(chip->password, chip->mc_base + chip->pwd_reg);
0853 
0854     switch (chip->hardware) {
0855     case OPTi9XX_HW_82C924:
0856         if (reg > 7) {
0857             outb(reg, chip->mc_base + 8);
0858             outb(chip->password, chip->mc_base + chip->pwd_reg);
0859             outb(value, chip->mc_base + 9);
0860             break;
0861         }
0862         fallthrough;
0863 
0864     case OPTi9XX_HW_82C929:
0865         outb(value, chip->mc_base + reg);
0866         break;
0867 
0868     default:
0869         snd_printk(KERN_ERR "sorry, no support for %d\n", chip->hardware);
0870     }
0871 
0872     spin_unlock_irqrestore(&chip->lock, flags);
0873 }
0874 
0875 static inline void snd_miro_write_mask(struct snd_miro *chip,
0876         unsigned char reg, unsigned char value, unsigned char mask)
0877 {
0878     unsigned char oldval = snd_miro_read(chip, reg);
0879 
0880     snd_miro_write(chip, reg, (oldval & ~mask) | (value & mask));
0881 }
0882 
0883 /*
0884  *  Proc Interface
0885  */
0886 
0887 static void snd_miro_proc_read(struct snd_info_entry * entry, 
0888                    struct snd_info_buffer *buffer)
0889 {
0890     struct snd_miro *miro = (struct snd_miro *) entry->private_data;
0891     struct snd_miro_aci *aci = miro->aci;
0892     char* model = "unknown";
0893 
0894     /* miroSOUND PCM1 pro, early PCM12 */
0895 
0896     if ((miro->hardware == OPTi9XX_HW_82C929) &&
0897         (aci->aci_vendor == 'm') &&
0898         (aci->aci_product == 'A')) {
0899         switch (aci->aci_version) {
0900         case 3:
0901             model = "miroSOUND PCM1 pro";
0902             break;
0903         default:
0904             model = "miroSOUND PCM1 pro / (early) PCM12";
0905             break;
0906         }
0907     }
0908 
0909     /* miroSOUND PCM12, PCM12 (Rev. E), PCM12 pnp */
0910 
0911     if ((miro->hardware == OPTi9XX_HW_82C924) &&
0912         (aci->aci_vendor == 'm') &&
0913         (aci->aci_product == 'B')) {
0914         switch (aci->aci_version) {
0915         case 4:
0916             model = "miroSOUND PCM12";
0917             break;
0918         case 176:
0919             model = "miroSOUND PCM12 (Rev. E)";
0920             break;
0921         default:
0922             model = "miroSOUND PCM12 / PCM12 pnp";
0923             break;
0924         }
0925     }
0926 
0927     /* miroSOUND PCM20 radio */
0928 
0929     if ((miro->hardware == OPTi9XX_HW_82C924) &&
0930         (aci->aci_vendor == 'm') &&
0931         (aci->aci_product == 'C')) {
0932         switch (aci->aci_version) {
0933         case 7:
0934             model = "miroSOUND PCM20 radio (Rev. E)";
0935             break;
0936         default:
0937             model = "miroSOUND PCM20 radio";
0938             break;
0939         }
0940     }
0941 
0942     snd_iprintf(buffer, "\nGeneral information:\n");
0943     snd_iprintf(buffer, "  model   : %s\n", model);
0944     snd_iprintf(buffer, "  opti    : %s\n", miro->name);
0945     snd_iprintf(buffer, "  codec   : %s\n", miro->pcm->name);
0946     snd_iprintf(buffer, "  port    : 0x%lx\n", miro->wss_base);
0947     snd_iprintf(buffer, "  irq     : %d\n", miro->irq);
0948     snd_iprintf(buffer, "  dma     : %d,%d\n\n", miro->dma1, miro->dma2);
0949 
0950     snd_iprintf(buffer, "MPU-401:\n");
0951     snd_iprintf(buffer, "  port    : 0x%lx\n", miro->mpu_port);
0952     snd_iprintf(buffer, "  irq     : %d\n\n", miro->mpu_irq);
0953 
0954     snd_iprintf(buffer, "ACI information:\n");
0955     snd_iprintf(buffer, "  vendor  : ");
0956     switch (aci->aci_vendor) {
0957     case 'm':
0958         snd_iprintf(buffer, "Miro\n");
0959         break;
0960     default:
0961         snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_vendor);
0962         break;
0963     }
0964 
0965     snd_iprintf(buffer, "  product : ");
0966     switch (aci->aci_product) {
0967     case 'A':
0968         snd_iprintf(buffer, "miroSOUND PCM1 pro / (early) PCM12\n");
0969         break;
0970     case 'B':
0971         snd_iprintf(buffer, "miroSOUND PCM12\n");
0972         break;
0973     case 'C':
0974         snd_iprintf(buffer, "miroSOUND PCM20 radio\n");
0975         break;
0976     default:
0977         snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_product);
0978         break;
0979     }
0980 
0981     snd_iprintf(buffer, "  firmware: %d (0x%x)\n",
0982             aci->aci_version, aci->aci_version);
0983     snd_iprintf(buffer, "  port    : 0x%lx-0x%lx\n", 
0984             aci->aci_port, aci->aci_port+2);
0985     snd_iprintf(buffer, "  wss     : 0x%x\n", wss);
0986     snd_iprintf(buffer, "  ide     : 0x%x\n", ide);
0987     snd_iprintf(buffer, "  solomode: 0x%x\n", aci->aci_solomode);
0988     snd_iprintf(buffer, "  amp     : 0x%x\n", aci->aci_amp);
0989     snd_iprintf(buffer, "  preamp  : 0x%x\n", aci->aci_preamp);
0990 }
0991 
0992 static void snd_miro_proc_init(struct snd_card *card,
0993                    struct snd_miro *miro)
0994 {
0995     snd_card_ro_proc_new(card, "miro", miro, snd_miro_proc_read);
0996 }
0997 
0998 /*
0999  *  Init
1000  */
1001 
1002 static int snd_miro_configure(struct snd_miro *chip)
1003 {
1004     unsigned char wss_base_bits;
1005     unsigned char irq_bits;
1006     unsigned char dma_bits;
1007     unsigned char mpu_port_bits = 0;
1008     unsigned char mpu_irq_bits;
1009     unsigned long flags;
1010 
1011     snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
1012     snd_miro_write_mask(chip, OPTi9XX_MC_REG(2), 0x20, 0x20); /* OPL4 */
1013     snd_miro_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
1014 
1015     switch (chip->hardware) {
1016     case OPTi9XX_HW_82C924:
1017         snd_miro_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
1018         snd_miro_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
1019         break;
1020     case OPTi9XX_HW_82C929:
1021         /* untested init commands for OPTi929 */
1022         snd_miro_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
1023         break;
1024     default:
1025         snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
1026         return -EINVAL;
1027     }
1028 
1029     /* PnP resource says it decodes only 10 bits of address */
1030     switch (chip->wss_base & 0x3ff) {
1031     case 0x130:
1032         chip->wss_base = 0x530;
1033         wss_base_bits = 0x00;
1034         break;
1035     case 0x204:
1036         chip->wss_base = 0x604;
1037         wss_base_bits = 0x03;
1038         break;
1039     case 0x280:
1040         chip->wss_base = 0xe80;
1041         wss_base_bits = 0x01;
1042         break;
1043     case 0x340:
1044         chip->wss_base = 0xf40;
1045         wss_base_bits = 0x02;
1046         break;
1047     default:
1048         snd_printk(KERN_ERR "WSS port 0x%lx not valid\n", chip->wss_base);
1049         goto __skip_base;
1050     }
1051     snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
1052 
1053 __skip_base:
1054     switch (chip->irq) {
1055     case 5:
1056         irq_bits = 0x05;
1057         break;
1058     case 7:
1059         irq_bits = 0x01;
1060         break;
1061     case 9:
1062         irq_bits = 0x02;
1063         break;
1064     case 10:
1065         irq_bits = 0x03;
1066         break;
1067     case 11:
1068         irq_bits = 0x04;
1069         break;
1070     default:
1071         snd_printk(KERN_ERR "WSS irq # %d not valid\n", chip->irq);
1072         goto __skip_resources;
1073     }
1074 
1075     switch (chip->dma1) {
1076     case 0:
1077         dma_bits = 0x01;
1078         break;
1079     case 1:
1080         dma_bits = 0x02;
1081         break;
1082     case 3:
1083         dma_bits = 0x03;
1084         break;
1085     default:
1086         snd_printk(KERN_ERR "WSS dma1 # %d not valid\n", chip->dma1);
1087         goto __skip_resources;
1088     }
1089 
1090     if (chip->dma1 == chip->dma2) {
1091         snd_printk(KERN_ERR "don't want to share dmas\n");
1092         return -EBUSY;
1093     }
1094 
1095     switch (chip->dma2) {
1096     case 0:
1097     case 1:
1098         break;
1099     default:
1100         snd_printk(KERN_ERR "WSS dma2 # %d not valid\n", chip->dma2);
1101         goto __skip_resources;
1102     }
1103     dma_bits |= 0x04;
1104 
1105     spin_lock_irqsave(&chip->lock, flags);
1106     outb(irq_bits << 3 | dma_bits, chip->wss_base);
1107     spin_unlock_irqrestore(&chip->lock, flags);
1108 
1109 __skip_resources:
1110     if (chip->hardware > OPTi9XX_HW_82C928) {
1111         switch (chip->mpu_port) {
1112         case 0:
1113         case -1:
1114             break;
1115         case 0x300:
1116             mpu_port_bits = 0x03;
1117             break;
1118         case 0x310:
1119             mpu_port_bits = 0x02;
1120             break;
1121         case 0x320:
1122             mpu_port_bits = 0x01;
1123             break;
1124         case 0x330:
1125             mpu_port_bits = 0x00;
1126             break;
1127         default:
1128             snd_printk(KERN_ERR "MPU-401 port 0x%lx not valid\n",
1129                    chip->mpu_port);
1130             goto __skip_mpu;
1131         }
1132 
1133         switch (chip->mpu_irq) {
1134         case 5:
1135             mpu_irq_bits = 0x02;
1136             break;
1137         case 7:
1138             mpu_irq_bits = 0x03;
1139             break;
1140         case 9:
1141             mpu_irq_bits = 0x00;
1142             break;
1143         case 10:
1144             mpu_irq_bits = 0x01;
1145             break;
1146         default:
1147             snd_printk(KERN_ERR "MPU-401 irq # %d not valid\n",
1148                    chip->mpu_irq);
1149             goto __skip_mpu;
1150         }
1151 
1152         snd_miro_write_mask(chip, OPTi9XX_MC_REG(6),
1153             (chip->mpu_port <= 0) ? 0x00 :
1154                 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
1155             0xf8);
1156     }
1157 __skip_mpu:
1158 
1159     return 0;
1160 }
1161 
1162 static int snd_miro_opti_check(struct snd_card *card, struct snd_miro *chip)
1163 {
1164     unsigned char value;
1165 
1166     chip->res_mc_base =
1167         devm_request_region(card->dev, chip->mc_base,
1168                     chip->mc_base_size, "OPTi9xx MC");
1169     if (chip->res_mc_base == NULL)
1170         return -ENOMEM;
1171 
1172     value = snd_miro_read(chip, OPTi9XX_MC_REG(1));
1173     if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
1174         if (value == snd_miro_read(chip, OPTi9XX_MC_REG(1)))
1175             return 0;
1176 
1177     devm_release_resource(card->dev, chip->res_mc_base);
1178     chip->res_mc_base = NULL;
1179 
1180     return -ENODEV;
1181 }
1182 
1183 static int snd_card_miro_detect(struct snd_card *card,
1184                 struct snd_miro *chip)
1185 {
1186     int i, err;
1187 
1188     for (i = OPTi9XX_HW_82C929; i <= OPTi9XX_HW_82C924; i++) {
1189 
1190         err = snd_miro_init(chip, i);
1191         if (err < 0)
1192             return err;
1193 
1194         err = snd_miro_opti_check(card, chip);
1195         if (err == 0)
1196             return 1;
1197     }
1198 
1199     return -ENODEV;
1200 }
1201 
1202 static int snd_card_miro_aci_detect(struct snd_card *card,
1203                     struct snd_miro *miro)
1204 {
1205     unsigned char regval;
1206     int i;
1207     struct snd_miro_aci *aci = &aci_device;
1208 
1209     miro->aci = aci;
1210 
1211     mutex_init(&aci->aci_mutex);
1212 
1213     /* get ACI port from OPTi9xx MC 4 */
1214 
1215     regval=inb(miro->mc_base + 4);
1216     aci->aci_port = (regval & 0x10) ? 0x344 : 0x354;
1217 
1218     miro->res_aci_port =
1219         devm_request_region(card->dev, aci->aci_port, 3, "miro aci");
1220     if (miro->res_aci_port == NULL) {
1221         snd_printk(KERN_ERR "aci i/o area 0x%lx-0x%lx already used.\n", 
1222                aci->aci_port, aci->aci_port+2);
1223         return -ENOMEM;
1224     }
1225 
1226         /* force ACI into a known state */
1227     for (i = 0; i < 3; i++)
1228         if (snd_aci_cmd(aci, ACI_ERROR_OP, -1, -1) < 0) {
1229             snd_printk(KERN_ERR "can't force aci into known state.\n");
1230             return -ENXIO;
1231         }
1232 
1233     aci->aci_vendor = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1);
1234     aci->aci_product = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1);
1235     if (aci->aci_vendor < 0 || aci->aci_product < 0) {
1236         snd_printk(KERN_ERR "can't read aci id on 0x%lx.\n",
1237                aci->aci_port);
1238         return -ENXIO;
1239     }
1240 
1241     aci->aci_version = snd_aci_cmd(aci, ACI_READ_VERSION, -1, -1);
1242     if (aci->aci_version < 0) {
1243         snd_printk(KERN_ERR "can't read aci version on 0x%lx.\n", 
1244                aci->aci_port);
1245         return -ENXIO;
1246     }
1247 
1248     if (snd_aci_cmd(aci, ACI_INIT, -1, -1) < 0 ||
1249         snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0 ||
1250         snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0) {
1251         snd_printk(KERN_ERR "can't initialize aci.\n"); 
1252         return -ENXIO;
1253     }
1254 
1255     return 0;
1256 }
1257 
1258 static int snd_miro_probe(struct snd_card *card)
1259 {
1260     int error;
1261     struct snd_miro *miro = card->private_data;
1262     struct snd_wss *codec;
1263     struct snd_rawmidi *rmidi;
1264 
1265     if (!miro->res_mc_base) {
1266         miro->res_mc_base = devm_request_region(card->dev,
1267                             miro->mc_base,
1268                             miro->mc_base_size,
1269                             "miro (OPTi9xx MC)");
1270         if (miro->res_mc_base == NULL) {
1271             snd_printk(KERN_ERR "request for OPTI9xx MC failed\n");
1272             return -ENOMEM;
1273         }
1274     }
1275 
1276     error = snd_card_miro_aci_detect(card, miro);
1277     if (error < 0) {
1278         snd_printk(KERN_ERR "unable to detect aci chip\n");
1279         return -ENODEV;
1280     }
1281 
1282     miro->wss_base = port;
1283     miro->mpu_port = mpu_port;
1284     miro->irq = irq;
1285     miro->mpu_irq = mpu_irq;
1286     miro->dma1 = dma1;
1287     miro->dma2 = dma2;
1288 
1289     /* init proc interface */
1290     snd_miro_proc_init(card, miro);
1291 
1292     error = snd_miro_configure(miro);
1293     if (error)
1294         return error;
1295 
1296     error = snd_wss_create(card, miro->wss_base + 4, -1,
1297                    miro->irq, miro->dma1, miro->dma2,
1298                    WSS_HW_DETECT, 0, &codec);
1299     if (error < 0)
1300         return error;
1301 
1302     error = snd_wss_pcm(codec, 0);
1303     if (error < 0)
1304         return error;
1305 
1306     error = snd_wss_mixer(codec);
1307     if (error < 0)
1308         return error;
1309 
1310     error = snd_wss_timer(codec, 0);
1311     if (error < 0)
1312         return error;
1313 
1314     miro->pcm = codec->pcm;
1315 
1316     error = snd_miro_mixer(card, miro);
1317     if (error < 0)
1318         return error;
1319 
1320     if (miro->aci->aci_vendor == 'm') {
1321         /* It looks like a miro sound card. */
1322         switch (miro->aci->aci_product) {
1323         case 'A':
1324             sprintf(card->shortname, 
1325                 "miroSOUND PCM1 pro / PCM12");
1326             break;
1327         case 'B':
1328             sprintf(card->shortname, 
1329                 "miroSOUND PCM12");
1330             break;
1331         case 'C':
1332             sprintf(card->shortname, 
1333                 "miroSOUND PCM20 radio");
1334             break;
1335         default:
1336             sprintf(card->shortname, 
1337                 "unknown miro");
1338             snd_printk(KERN_INFO "unknown miro aci id\n");
1339             break;
1340         }
1341     } else {
1342         snd_printk(KERN_INFO "found unsupported aci card\n");
1343         sprintf(card->shortname, "unknown Cardinal Technologies");
1344     }
1345 
1346     strcpy(card->driver, "miro");
1347     snprintf(card->longname, sizeof(card->longname),
1348          "%s: OPTi%s, %s at 0x%lx, irq %d, dma %d&%d",
1349          card->shortname, miro->name, codec->pcm->name,
1350          miro->wss_base + 4, miro->irq, miro->dma1, miro->dma2);
1351 
1352     if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
1353         rmidi = NULL;
1354     else {
1355         error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
1356                 mpu_port, 0, miro->mpu_irq, &rmidi);
1357         if (error < 0)
1358             snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
1359                    mpu_port);
1360     }
1361 
1362     if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
1363         struct snd_opl3 *opl3 = NULL;
1364         struct snd_opl4 *opl4;
1365 
1366         if (snd_opl4_create(card, fm_port, fm_port - 8,
1367                     2, &opl3, &opl4) < 0)
1368             snd_printk(KERN_WARNING "no OPL4 device at 0x%lx\n",
1369                    fm_port);
1370     }
1371 
1372     error = snd_set_aci_init_values(miro);
1373     if (error < 0)
1374                 return error;
1375 
1376     return snd_card_register(card);
1377 }
1378 
1379 static int snd_miro_isa_match(struct device *devptr, unsigned int n)
1380 {
1381 #ifdef CONFIG_PNP
1382     if (snd_miro_pnp_is_probed)
1383         return 0;
1384     if (isapnp)
1385         return 0;
1386 #endif
1387     return 1;
1388 }
1389 
1390 static int snd_miro_isa_probe(struct device *devptr, unsigned int n)
1391 {
1392     static const long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
1393     static const long possible_mpu_ports[] = {0x330, 0x300, 0x310, 0x320, -1};
1394     static const int possible_irqs[] = {11, 9, 10, 7, -1};
1395     static const int possible_mpu_irqs[] = {10, 5, 9, 7, -1};
1396     static const int possible_dma1s[] = {3, 1, 0, -1};
1397     static const int possible_dma2s[][2] = { {1, -1}, {0, -1}, {-1, -1},
1398                        {0, -1} };
1399 
1400     int error;
1401     struct snd_miro *miro;
1402     struct snd_card *card;
1403 
1404     error = snd_devm_card_new(devptr, index, id, THIS_MODULE,
1405                   sizeof(struct snd_miro), &card);
1406     if (error < 0)
1407         return error;
1408 
1409     miro = card->private_data;
1410 
1411     error = snd_card_miro_detect(card, miro);
1412     if (error < 0) {
1413         snd_printk(KERN_ERR "unable to detect OPTi9xx chip\n");
1414         return -ENODEV;
1415     }
1416 
1417     if (port == SNDRV_AUTO_PORT) {
1418         port = snd_legacy_find_free_ioport(possible_ports, 4);
1419         if (port < 0) {
1420             snd_printk(KERN_ERR "unable to find a free WSS port\n");
1421             return -EBUSY;
1422         }
1423     }
1424 
1425     if (mpu_port == SNDRV_AUTO_PORT) {
1426         mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2);
1427         if (mpu_port < 0) {
1428             snd_printk(KERN_ERR
1429                    "unable to find a free MPU401 port\n");
1430             return -EBUSY;
1431         }
1432     }
1433 
1434     if (irq == SNDRV_AUTO_IRQ) {
1435         irq = snd_legacy_find_free_irq(possible_irqs);
1436         if (irq < 0) {
1437             snd_printk(KERN_ERR "unable to find a free IRQ\n");
1438             return -EBUSY;
1439         }
1440     }
1441     if (mpu_irq == SNDRV_AUTO_IRQ) {
1442         mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs);
1443         if (mpu_irq < 0) {
1444             snd_printk(KERN_ERR
1445                    "unable to find a free MPU401 IRQ\n");
1446             return -EBUSY;
1447         }
1448     }
1449     if (dma1 == SNDRV_AUTO_DMA) {
1450         dma1 = snd_legacy_find_free_dma(possible_dma1s);
1451         if (dma1 < 0) {
1452             snd_printk(KERN_ERR "unable to find a free DMA1\n");
1453             return -EBUSY;
1454         }
1455     }
1456     if (dma2 == SNDRV_AUTO_DMA) {
1457         dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4]);
1458         if (dma2 < 0) {
1459             snd_printk(KERN_ERR "unable to find a free DMA2\n");
1460             return -EBUSY;
1461         }
1462     }
1463 
1464     error = snd_miro_probe(card);
1465     if (error < 0)
1466         return error;
1467 
1468     dev_set_drvdata(devptr, card);
1469     return 0;
1470 }
1471 
1472 #define DEV_NAME "miro"
1473 
1474 static struct isa_driver snd_miro_driver = {
1475     .match      = snd_miro_isa_match,
1476     .probe      = snd_miro_isa_probe,
1477     /* FIXME: suspend/resume */
1478     .driver     = {
1479         .name   = DEV_NAME
1480     },
1481 };
1482 
1483 #ifdef CONFIG_PNP
1484 
1485 static int snd_card_miro_pnp(struct snd_miro *chip,
1486                  struct pnp_card_link *card,
1487                  const struct pnp_card_device_id *pid)
1488 {
1489     struct pnp_dev *pdev;
1490     int err;
1491     struct pnp_dev *devmpu;
1492     struct pnp_dev *devmc;
1493 
1494     pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
1495     if (pdev == NULL)
1496         return -EBUSY;
1497 
1498     devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
1499     if (devmpu == NULL)
1500         return -EBUSY;
1501 
1502     devmc = pnp_request_card_device(card, pid->devs[2].id, NULL);
1503     if (devmc == NULL)
1504         return -EBUSY;
1505 
1506     err = pnp_activate_dev(pdev);
1507     if (err < 0) {
1508         snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
1509         return err;
1510     }
1511 
1512     err = pnp_activate_dev(devmc);
1513     if (err < 0) {
1514         snd_printk(KERN_ERR "MC pnp configure failure: %d\n",
1515                     err);
1516         return err;
1517     }
1518 
1519     port = pnp_port_start(pdev, 1);
1520     fm_port = pnp_port_start(pdev, 2) + 8;
1521 
1522     /*
1523      * The MC(0) is never accessed and the miroSOUND PCM20 card does not
1524      * include it in the PnP resource range. OPTI93x include it.
1525      */
1526     chip->mc_base = pnp_port_start(devmc, 0) - 1;
1527     chip->mc_base_size = pnp_port_len(devmc, 0) + 1;
1528 
1529     irq = pnp_irq(pdev, 0);
1530     dma1 = pnp_dma(pdev, 0);
1531     dma2 = pnp_dma(pdev, 1);
1532 
1533     if (mpu_port > 0) {
1534         err = pnp_activate_dev(devmpu);
1535         if (err < 0) {
1536             snd_printk(KERN_ERR "MPU401 pnp configure failure\n");
1537             mpu_port = -1;
1538             return err;
1539         }
1540         mpu_port = pnp_port_start(devmpu, 0);
1541         mpu_irq = pnp_irq(devmpu, 0);
1542     }
1543     return 0;
1544 }
1545 
1546 static int snd_miro_pnp_probe(struct pnp_card_link *pcard,
1547                   const struct pnp_card_device_id *pid)
1548 {
1549     struct snd_card *card;
1550     int err;
1551     struct snd_miro *miro;
1552 
1553     if (snd_miro_pnp_is_probed)
1554         return -EBUSY;
1555     if (!isapnp)
1556         return -ENODEV;
1557     err = snd_devm_card_new(&pcard->card->dev, index, id, THIS_MODULE,
1558                 sizeof(struct snd_miro), &card);
1559     if (err < 0)
1560         return err;
1561 
1562     miro = card->private_data;
1563 
1564     err = snd_card_miro_pnp(miro, pcard, pid);
1565     if (err)
1566         return err;
1567 
1568     /* only miroSOUND PCM20 and PCM12 == OPTi924 */
1569     err = snd_miro_init(miro, OPTi9XX_HW_82C924);
1570     if (err)
1571         return err;
1572 
1573     err = snd_miro_opti_check(card, miro);
1574     if (err) {
1575         snd_printk(KERN_ERR "OPTI chip not found\n");
1576         return err;
1577     }
1578 
1579     err = snd_miro_probe(card);
1580     if (err < 0)
1581         return err;
1582     pnp_set_card_drvdata(pcard, card);
1583     snd_miro_pnp_is_probed = 1;
1584     return 0;
1585 }
1586 
1587 static void snd_miro_pnp_remove(struct pnp_card_link *pcard)
1588 {
1589     snd_miro_pnp_is_probed = 0;
1590 }
1591 
1592 static struct pnp_card_driver miro_pnpc_driver = {
1593     .flags      = PNP_DRIVER_RES_DISABLE,
1594     .name       = "miro",
1595     .id_table   = snd_miro_pnpids,
1596     .probe      = snd_miro_pnp_probe,
1597     .remove     = snd_miro_pnp_remove,
1598 };
1599 #endif
1600 
1601 static int __init alsa_card_miro_init(void)
1602 {
1603 #ifdef CONFIG_PNP
1604     pnp_register_card_driver(&miro_pnpc_driver);
1605     if (snd_miro_pnp_is_probed)
1606         return 0;
1607     pnp_unregister_card_driver(&miro_pnpc_driver);
1608 #endif
1609     return isa_register_driver(&snd_miro_driver, 1);
1610 }
1611 
1612 static void __exit alsa_card_miro_exit(void)
1613 {
1614     if (!snd_miro_pnp_is_probed) {
1615         isa_unregister_driver(&snd_miro_driver);
1616         return;
1617     }
1618 #ifdef CONFIG_PNP
1619     pnp_unregister_card_driver(&miro_pnpc_driver);
1620 #endif
1621 }
1622 
1623 module_init(alsa_card_miro_init)
1624 module_exit(alsa_card_miro_exit)