diff --git a/hw/pcmcia.c b/hw/pcmcia.c
index 17a49b6..d661663 100644
--- a/hw/pcmcia.c
+++ b/hw/pcmcia.c
@@ -86,16 +86,59 @@ static int pcmcia_device_init(DeviceState *dev, DeviceInfo *info)
 {
     PCMCIACardState *state = DO_UPCAST(PCMCIACardState, dev, dev);
     PCMCIACardInfo *pinfo = DO_UPCAST(PCMCIACardInfo, qdev, info);
+    PCMCIASocket *socket = DO_UPCAST(PCMCIASocket, qbus, dev->parent_bus);
     int rc;
 
+    if (socket->attached) {
+        return -1;
+    }
+
     state->info = pinfo;
     rc = pinfo->init(state);
-    return rc;
+    if (rc) {
+        return rc;
+    }
+
+    socket->attached = 1;
+    state->slot = socket;
+
+    rc = socket->attach(socket, state);
+    if (rc) {
+        return rc;
+    }
+
+    rc = state->info->attach(state);
+    if (rc) {
+        socket->detach(socket);
+        return rc;
+    }
+
+    return 0;
+}
+
+static int pcmcia_device_exit(DeviceState *dev)
+{
+    PCMCIACardState *state = DO_UPCAST(PCMCIACardState, dev, dev);
+    PCMCIASocket *socket = DO_UPCAST(PCMCIASocket, qbus, dev->parent_bus);
+
+    if (!socket->attached) {
+        return -ENOENT;
+    }
+
+    state->info->detach(state);
+    state->slot = NULL;
+
+    socket->detach(socket);
+
+    socket->attached = 0;
+
+    return 0;
 }
 
 void pcmcia_card_register(PCMCIACardInfo *info)
 {
     info->qdev.init = pcmcia_device_init;
+    info->qdev.exit = pcmcia_device_exit;
     info->qdev.bus_info = &pcmcia_bus_info;
     assert(info->qdev.size >= sizeof(PCMCIACardState));
     qdev_register(&info->qdev);
diff --git a/hw/pcmcia.h b/hw/pcmcia.h
index 2c012d9..4f90af7 100644
--- a/hw/pcmcia.h
+++ b/hw/pcmcia.h
@@ -11,6 +11,8 @@ struct PCMCIASocket {
     int attached;
     const char *slot_string;
     const char *card_string;
+    int (*attach)(PCMCIASocket *socket, PCMCIACardState *card);
+    int (*detach)(PCMCIASocket *socket);
 };
 
 void pcmcia_socket_register(PCMCIASocket *socket, DeviceState *parent);
diff --git a/hw/pxa.h b/hw/pxa.h
index 25176ef..c145029 100644
--- a/hw/pxa.h
+++ b/hw/pxa.h
@@ -89,10 +89,6 @@ PXA2xxMMCIState *pxa2xx_mmci_init(target_phys_addr_t base,
 void pxa2xx_mmci_handlers(PXA2xxMMCIState *s, qemu_irq readonly,
                 qemu_irq coverswitch);
 
-/* pxa2xx_pcmcia.c */
-int pxa2xx_pcmcia_attach(void *opaque, PCMCIACardState *card);
-int pxa2xx_pcmcia_dettach(void *opaque);
-
 /* pxa2xx_keypad.c */
 struct  keymap {
     int column;
diff --git a/hw/pxa2xx_pcmcia.c b/hw/pxa2xx_pcmcia.c
index ae7d01a..efd4c09 100644
--- a/hw/pxa2xx_pcmcia.c
+++ b/hw/pxa2xx_pcmcia.c
@@ -150,6 +150,37 @@ DeviceState *pxa2xx_pcmcia_init(target_phys_addr_t base, uint8_t id)
     return &dev->qdev;
 }
 
+/* Insert a new card into a slot */
+static int pxa2xx_pcmcia_attach(PCMCIASocket *socket, PCMCIACardState *card)
+{
+    PXA2xxPCMCIAState *s = container_of(socket, PXA2xxPCMCIAState, slot);
+
+    if (s->cd_irq) {
+        qemu_irq_raise(s->cd_irq);
+    }
+
+    s->card = card;
+
+    return 0;
+}
+
+/* Eject card from the slot */
+static int pxa2xx_pcmcia_detach(PCMCIASocket *socket)
+{
+    PXA2xxPCMCIAState *s = container_of(socket, PXA2xxPCMCIAState, slot);
+
+    s->card = NULL;
+
+    if (s->irq) {
+        qemu_irq_lower(s->irq);
+    }
+    if (s->cd_irq) {
+        qemu_irq_lower(s->cd_irq);
+    }
+
+    return 0;
+}
+
 static int pxa2xx_pcmcia_initfn(SysBusDevice *dev)
 {
     int iomemtype;
@@ -186,48 +217,10 @@ static int pxa2xx_pcmcia_initfn(SysBusDevice *dev)
     snprintf(str, 256, "PXA PC Card Socket %d", s->id);
     s->slot.slot_string = str;
 
+    s->slot.attach = pxa2xx_pcmcia_attach;
+    s->slot.detach = pxa2xx_pcmcia_detach;
     s->slot.irq = qemu_allocate_irqs(pxa2xx_pcmcia_set_irq, s, 1)[0];
     pcmcia_socket_register(&s->slot, &s->busdev.qdev);
-    return 0;
-}
-
-/* Insert a new card into a slot */
-int pxa2xx_pcmcia_attach(void *opaque, PCMCIACardState *card)
-{
-    PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
-    if (s->slot.attached)
-        return -EEXIST;
-
-    if (s->cd_irq) {
-        qemu_irq_raise(s->cd_irq);
-    }
-
-    s->card = card;
-
-    s->slot.attached = 1;
-    s->card->slot = &s->slot;
-    s->card->info->attach(s->card);
-
-    return 0;
-}
-
-/* Eject card from the slot */
-int pxa2xx_pcmcia_dettach(void *opaque)
-{
-    PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
-    if (!s->slot.attached)
-        return -ENOENT;
-
-    s->card->info->detach(s->card);
-    s->card->slot = NULL;
-    s->card = NULL;
-
-    s->slot.attached = 0;
-
-    if (s->irq)
-        qemu_irq_lower(s->irq);
-    if (s->cd_irq)
-        qemu_irq_lower(s->cd_irq);
 
     return 0;
 }
diff --git a/hw/spitz.c b/hw/spitz.c
index 51cc08c..cf2ee82 100644
--- a/hw/spitz.c
+++ b/hw/spitz.c
@@ -718,7 +718,6 @@ static void spitz_microdrive_attach(PXA2xxState *cpu, int slot)
                         DO_UPCAST(PCMCIASocket, qbus,
                             qdev_get_child_bus(cpu->pcmcia[slot], "pcmcia")),
                         dinfo);
-        pxa2xx_pcmcia_attach(cpu->pcmcia[slot], md);
     }
 }
 
diff --git a/hw/tosa.c b/hw/tosa.c
index f00555b..e867dc4 100644
--- a/hw/tosa.c
+++ b/hw/tosa.c
@@ -63,7 +63,6 @@ static void tosa_microdrive_attach(PXA2xxState *cpu)
                         DO_UPCAST(PCMCIASocket, qbus,
                             qdev_get_child_bus(cpu->pcmcia[0], "pcmcia")),
                         dinfo);
-        pxa2xx_pcmcia_attach(cpu->pcmcia[0], md);
     }
 }
 
