ide: sanitize ide_unregister() usage
[pandora-kernel.git] / drivers / ide / legacy / ide-cs.c
index 03715c0..867fe14 100644 (file)
@@ -2,8 +2,6 @@
 
     A driver for PCMCIA IDE/ATA disk cards
 
-    ide-cs.c 1.3 2002/10/26 05:45:31
-
     The contents of this file are subject to the Mozilla Public
     License Version 1.1 (the "License"); you may not use this file
     except in compliance with the License. You may obtain a copy of
@@ -53,6 +51,8 @@
 #include <pcmcia/cisreg.h>
 #include <pcmcia/ciscode.h>
 
+#define DRV_NAME "ide-cs"
+
 /*====================================================================*/
 
 /* Module parameters */
@@ -74,16 +74,11 @@ static char *version =
 
 /*====================================================================*/
 
-static const char ide_major[] = {
-    IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR,
-    IDE4_MAJOR, IDE5_MAJOR
-};
-
 typedef struct ide_info_t {
        struct pcmcia_device    *p_dev;
+       ide_hwif_t              *hwif;
     int                ndev;
     dev_node_t node;
-    int                hd;
 } ide_info_t;
 
 static void ide_release(struct pcmcia_device *);
@@ -138,22 +133,71 @@ static int ide_probe(struct pcmcia_device *link)
 
 static void ide_detach(struct pcmcia_device *link)
 {
+    ide_info_t *info = link->priv;
+    ide_hwif_t *hwif = info->hwif;
+
     DEBUG(0, "ide_detach(0x%p)\n", link);
 
     ide_release(link);
 
-    kfree(link->priv);
+    release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
+    release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
+
+    kfree(info);
 } /* ide_detach */
 
-static int idecs_register(unsigned long io, unsigned long ctl, unsigned long irq, struct pcmcia_device *handle)
+static const struct ide_port_ops idecs_port_ops = {
+       .quirkproc              = ide_undecoded_slave,
+};
+
+static ide_hwif_t *idecs_register(unsigned long io, unsigned long ctl,
+                               unsigned long irq, struct pcmcia_device *handle)
 {
+    ide_hwif_t *hwif;
     hw_regs_t hw;
+    int i;
+    u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
+
+    if (!request_region(io, 8, DRV_NAME)) {
+       printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
+                       DRV_NAME, io, io + 7);
+       return NULL;
+    }
+
+    if (!request_region(ctl, 1, DRV_NAME)) {
+       printk(KERN_ERR "%s: I/O resource 0x%lX not free.\n",
+                       DRV_NAME, ctl);
+       release_region(io, 8);
+       return NULL;
+    }
+
     memset(&hw, 0, sizeof(hw));
-    ide_init_hwif_ports(&hw, io, ctl, NULL);
+    ide_std_init_ports(&hw, io, ctl);
     hw.irq = irq;
     hw.chipset = ide_pci;
     hw.dev = &handle->dev;
-    return ide_register_hw(&hw, &ide_undecoded_slave, 0, NULL);
+
+    hwif = ide_find_port();
+    if (hwif == NULL)
+       goto out_release;
+
+    i = hwif->index;
+
+    ide_init_port_data(hwif, i);
+    ide_init_port_hw(hwif, &hw);
+    hwif->port_ops = &idecs_port_ops;
+
+    idx[0] = i;
+
+    ide_device_add(idx, NULL);
+
+    if (hwif->present)
+       return hwif;
+
+out_release:
+    release_region(ctl, 1);
+    release_region(io, 8);
+    return NULL;
 }
 
 /*======================================================================
@@ -178,8 +222,9 @@ static int ide_config(struct pcmcia_device *link)
        cistpl_cftable_entry_t dflt;
     } *stk = NULL;
     cistpl_cftable_entry_t *cfg;
-    int i, pass, last_ret = 0, last_fn = 0, hd, is_kme = 0;
+    int i, pass, last_ret = 0, last_fn = 0, is_kme = 0;
     unsigned long io_base, ctl_base;
+    ide_hwif_t *hwif;
 
     DEBUG(0, "ide_config(0x%p)\n", link);
 
@@ -275,14 +320,15 @@ static int ide_config(struct pcmcia_device *link)
        outb(0x81, ctl_base+1);
 
     /* retry registration in case device is still spinning up */
-    for (hd = -1, i = 0; i < 10; i++) {
-       hd = idecs_register(io_base, ctl_base, link->irq.AssignedIRQ, link);
-       if (hd >= 0) break;
+    for (i = 0; i < 10; i++) {
+       hwif = idecs_register(io_base, ctl_base, link->irq.AssignedIRQ, link);
+       if (hwif)
+           break;
        if (link->io.NumPorts1 == 0x20) {
            outb(0x02, ctl_base + 0x10);
-           hd = idecs_register(io_base + 0x10, ctl_base + 0x10,
-                               link->irq.AssignedIRQ, link);
-           if (hd >= 0) {
+           hwif = idecs_register(io_base + 0x10, ctl_base + 0x10,
+                                 link->irq.AssignedIRQ, link);
+           if (hwif) {
                io_base += 0x10;
                ctl_base += 0x10;
                break;
@@ -291,7 +337,7 @@ static int ide_config(struct pcmcia_device *link)
        msleep(100);
     }
 
-    if (hd < 0) {
+    if (hwif == NULL) {
        printk(KERN_NOTICE "ide-cs: ide_register() at 0x%3lx & 0x%3lx"
               ", irq %u failed\n", io_base, ctl_base,
               link->irq.AssignedIRQ);
@@ -299,10 +345,10 @@ static int ide_config(struct pcmcia_device *link)
     }
 
     info->ndev = 1;
-    sprintf(info->node.dev_name, "hd%c", 'a' + (hd * 2));
-    info->node.major = ide_major[hd];
+    sprintf(info->node.dev_name, "hd%c", 'a' + hwif->index * 2);
+    info->node.major = hwif->major;
     info->node.minor = 0;
-    info->hd = hd;
+    info->hwif = hwif;
     link->dev_node = &info->node;
     printk(KERN_INFO "ide-cs: %s: Vpp = %d.%d\n",
           info->node.dev_name, link->conf.Vpp / 10, link->conf.Vpp % 10);
@@ -333,13 +379,14 @@ failed:
 void ide_release(struct pcmcia_device *link)
 {
     ide_info_t *info = link->priv;
+    ide_hwif_t *hwif = info->hwif;
 
     DEBUG(0, "ide_release(0x%p)\n", link);
 
     if (info->ndev) {
        /* FIXME: if this fails we need to queue the cleanup somehow
           -- need to investigate the required PCMCIA magic */
-       ide_unregister(info->hd);
+       ide_unregister(hwif->index);
     }
     info->ndev = 0;