vserver 2.0 rc7
[linux-2.6.git] / sound / pci / azt3328.c
index 6127942..b8ae534 100644 (file)
@@ -160,19 +160,19 @@ MODULE_SUPPORTED_DEVICE("{{Aztech,AZF3328}}");
 #endif
            
 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;     /* Index 0-MAX */
-static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;      /* ID for this card */
-static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;     /* Enable this card */
-#ifdef SUPPORT_JOYSTICK
-static int joystick[SNDRV_CARDS];
-#endif
-
 module_param_array(index, int, NULL, 0444);
 MODULE_PARM_DESC(index, "Index value for AZF3328 soundcard.");
+
+static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;      /* ID for this card */
 module_param_array(id, charp, NULL, 0444);
 MODULE_PARM_DESC(id, "ID string for AZF3328 soundcard.");
+
+static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;     /* Enable this card */
 module_param_array(enable, bool, NULL, 0444);
 MODULE_PARM_DESC(enable, "Enable AZF3328 soundcard.");
+
 #ifdef SUPPORT_JOYSTICK
+static int joystick[SNDRV_CARDS];
 module_param_array(joystick, bool, NULL, 0444);
 MODULE_PARM_DESC(joystick, "Enable joystick for AZF3328 soundcard.");
 #endif
@@ -189,8 +189,7 @@ struct _snd_azf3328 {
        unsigned long mixer_port;
 
 #ifdef SUPPORT_JOYSTICK
-       struct gameport gameport;
-       struct resource *res_joystick;
+       struct gameport *gameport;
 #endif
 
        struct pci_dev *pci;
@@ -1222,6 +1221,63 @@ static int __devinit snd_azf3328_pcm(azf3328_t *chip, int device)
 
 /******************************************************************/
 
+#ifdef SUPPORT_JOYSTICK
+static int __devinit snd_azf3328_config_joystick(azf3328_t *chip, int dev)
+{
+       struct gameport *gp;
+       struct resource *r;
+
+       if (!joystick[dev])
+               return -ENODEV;
+
+       if (!(r = request_region(0x200, 8, "AZF3328 gameport"))) {
+               printk(KERN_WARNING "azt3328: cannot reserve joystick ports\n");
+               return -EBUSY;
+       }
+
+       chip->gameport = gp = gameport_allocate_port();
+       if (!gp) {
+               printk(KERN_ERR "azt3328: cannot allocate memory for gameport\n");
+               release_resource(r);
+               kfree_nocheck(r);
+               return -ENOMEM;
+       }
+
+       gameport_set_name(gp, "AZF3328 Gameport");
+       gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
+       gameport_set_dev_parent(gp, &chip->pci->dev);
+       gp->io = 0x200;
+       gameport_set_port_data(gp, r);
+
+       snd_azf3328_io2_write(chip, IDX_IO2_LEGACY_ADDR,
+                             snd_azf3328_io2_read(chip, IDX_IO2_LEGACY_ADDR) | LEGACY_JOY);
+
+       gameport_register_port(chip->gameport);
+
+       return 0;
+}
+
+static void snd_azf3328_free_joystick(azf3328_t *chip)
+{
+       if (chip->gameport) {
+               struct resource *r = gameport_get_port_data(chip->gameport);
+
+               gameport_unregister_port(chip->gameport);
+               chip->gameport = NULL;
+               /* disable gameport */
+               snd_azf3328_io2_write(chip, IDX_IO2_LEGACY_ADDR,
+                                     snd_azf3328_io2_read(chip, IDX_IO2_LEGACY_ADDR) & ~LEGACY_JOY);
+               release_resource(r);
+               kfree_nocheck(r);
+       }
+}
+#else
+static inline int snd_azf3328_config_joystick(azf3328_t *chip, int dev) { return -ENOSYS; }
+static inline void snd_azf3328_free_joystick(azf3328_t *chip) { }
+#endif
+
+/******************************************************************/
+
 static int snd_azf3328_free(azf3328_t *chip)
 {
         if (chip->irq < 0)
@@ -1236,16 +1292,7 @@ static int snd_azf3328_free(azf3328_t *chip)
 
         synchronize_irq(chip->irq);
       __end_hw:
-#ifdef SUPPORT_JOYSTICK
-       if (chip->res_joystick) {
-               gameport_unregister_port(&chip->gameport);
-               /* disable gameport */
-               snd_azf3328_io2_write(chip, IDX_IO2_LEGACY_ADDR,
-                                     snd_azf3328_io2_read(chip, IDX_IO2_LEGACY_ADDR) & ~LEGACY_JOY);
-               release_resource(chip->res_joystick);
-               kfree_nocheck(chip->res_joystick);
-       }
-#endif
+       snd_azf3328_free_joystick(chip);
         if (chip->irq >= 0)
                free_irq(chip->irq, (void *)chip);
        pci_release_regions(chip->pci);
@@ -1373,28 +1420,6 @@ static int __devinit snd_azf3328_create(snd_card_t * card,
        return 0;
 }
 
-#ifdef SUPPORT_JOYSTICK
-static void __devinit snd_azf3328_config_joystick(azf3328_t *chip, int joystick)
-{
-       unsigned char val;
-
-       if (joystick == 1) {
-               if ((chip->res_joystick = request_region(0x200, 8, "AZF3328 gameport")) != NULL)
-                       chip->gameport.io = 0x200;
-       }
-
-       val = inb(chip->io2_port + IDX_IO2_LEGACY_ADDR);
-       if (chip->res_joystick)
-               val |= LEGACY_JOY;
-       else
-               val &= ~LEGACY_JOY;
-
-       outb(val, chip->io2_port + IDX_IO2_LEGACY_ADDR);
-       if (chip->res_joystick)
-               gameport_register_port(&chip->gameport);
-}
-#endif
-
 static int __devinit snd_azf3328_probe(struct pci_dev *pci,
                                          const struct pci_device_id *pci_id)
 {
@@ -1465,9 +1490,9 @@ static int __devinit snd_azf3328_probe(struct pci_dev *pci,
 "azt3328: Feel free to contact hw7oshyuv3001@sneakemail.com for bug reports etc.!\n");
 #endif
 
-#ifdef SUPPORT_JOYSTICK
-       snd_azf3328_config_joystick(chip, joystick[dev]);
-#endif
+       if (snd_azf3328_config_joystick(chip, dev) < 0)
+               snd_azf3328_io2_write(chip, IDX_IO2_LEGACY_ADDR,
+                             snd_azf3328_io2_read(chip, IDX_IO2_LEGACY_ADDR) & ~LEGACY_JOY);
 
        pci_set_drvdata(pci, card);
        dev++;