# HG changeset patch
# Parent a611642de7f291e4749a7f44027f9ebaa0cde75c
Move the interrupt handler init/cleanup out of e1000.blt

This is not device dependant and can be done from the Ethernet subsystem
right now, (plus the interrupt handler itself was already in the
Ethernet subsytem).

diff --git a/rathaxes/samples/e1000/e1000.blt b/rathaxes/samples/e1000/e1000.blt
--- a/rathaxes/samples/e1000/e1000.blt
+++ b/rathaxes/samples/e1000/e1000.blt
@@ -249,7 +249,6 @@
         {
             int                         bars;
             unsigned char /* __iomem */ *ioaddr;
-            int                         irq;
 
             ${e1000::RxRing}    rx_ring;
             ${e1000::TxRing}    tx_ring;
@@ -267,14 +266,12 @@
 
         chunk   Ethernet::adapter_init_context(Ethernet::Device rtx_ether_ctx,
                                                Builtin::number bars,
-                                               Builtin::symbol ioaddr,
-                                               Builtin::number irq)
+                                               Builtin::symbol ioaddr)
         {
             {
                 ${e1000::Context} *hw_ctx = &${rtx_ether_ctx}->hw_ctx;
                 hw_ctx->bars = ${bars};
                 hw_ctx->ioaddr = ${ioaddr};
-                hw_ctx->irq = ${irq};
             }
         }
 
@@ -606,68 +603,6 @@
         }
     }
 
-    template sequence   e1000::setup_interrupt_handler(Ethernet::Device rtx_ether_ctx)
-    {
-        chunk   LKM::includes()
-        {
-            #include <linux/interrupt.h>
-        }
-
-        chunk   LKM::prototypes()
-        {
-            static int e1000_setup_interrupt_handler(${Ethernet::Device} *);
-        }
-
-        chunk   LKM::code()
-        {
-            static int e1000_setup_interrupt_handler(${Ethernet::Device} *rtx_ether_ctx)
-            {
-                int error;
-
-                error = request_irq(rtx_ether_ctx->hw_ctx.irq,
-                        rtx_ethernet_interrupt_handler,
-                        IRQF_SHARED,
-                        ${config.name},
-                        rtx_ether_ctx);
-
-                if (error)
-                    ${Log::info("cannot register the interrupt handler")};
-
-                return error;
-            }
-        }
-
-        chunk   ::CALL()
-        {
-                int error = e1000_setup_interrupt_handler(${rtx_ether_ctx});
-                if (error)
-                {
-                        return error;
-                }
-        }
-    }
-
-    template sequence   free_interrupt_handler(Ethernet::Device rtx_ether_ctx)
-    {
-        chunk   prototypes()
-        {
-            static void e1000_free_interrupt_handler(${Ethernet::Device} *rtx_ether_ctx);
-        }
-
-        chunk code()
-        {
-            static void e1000_free_interrupt_handler(${Ethernet::Device} *rtx_ether_ctx)
-            {
-                free_irq(rtx_ether_ctx->hw_ctx.irq, rtx_ether_ctx);
-            }
-        }
-
-        chunk ::CALL()
-        {
-            e1000_free_interrupt_handler(${rtx_ether_ctx});
-        }
-    }
-
     template sequence   activate_device_interruption(Ethernet::Device rtx_ether_ctx)
     {
         chunk  ::CALL()
diff --git a/rathaxes/samples/e1000/e1000.rti b/rathaxes/samples/e1000/e1000.rti
--- a/rathaxes/samples/e1000/e1000.rti
+++ b/rathaxes/samples/e1000/e1000.rti
@@ -13,8 +13,7 @@
         /* Callbacks/Hooks which should probably be in the front-end: */
         chunk       Ethernet::adapter_init_context(Ethernet::Device,
                                                    Builtin::number,
-                                                   Builtin::symbol,
-                                                   Builtin::number);
+                                                   Builtin::symbol);
         chunk       Ethernet::adapter_reset(Ethernet::Device);
         chunk       Ethernet::adapter_load_mac_address(Ethernet::Device);
     }
@@ -85,21 +84,6 @@
         provided chunk  ::CALL();
     }
 
-    provided sequence   setup_interrupt_handler(Ethernet::Device)
-    {
-        provided chunk  LKM::includes(); // works without this one
-        provided chunk  LKM::prototypes();
-        provided chunk  LKM::code();
-        provided chunk  ::CALL();
-    }
-
-    provided sequence   free_interrupt_handler(Ethernet::Device)
-    {
-        provided chunk  LKM::prototypes();
-        provided chunk  LKM::code();
-        provided chunk  ::CALL();
-    }
-
     provided sequence   activate_device_interruption(Ethernet::Device)
     {
         provided chunk  ::CALL();
diff --git a/rathaxes/samples/e1000/ethernet.blt b/rathaxes/samples/e1000/ethernet.blt
--- a/rathaxes/samples/e1000/ethernet.blt
+++ b/rathaxes/samples/e1000/ethernet.blt
@@ -1,4 +1,4 @@
-with Ethernet, PCI, LKM, Log
+with Ethernet, PCI, LKM, Log, Builtin
 {
     template type   Ethernet::ProtocolId()
     {
@@ -61,7 +61,9 @@
 
         map
         {
-            netdev:   ${self}->ndev;
+            netdev: ${self}->ndev;
+            /* This could be another init method for Ethernet::Device: */
+            rtx_ether_ctx: netdev_priv(&${self}->ndev);
         }
     }
 
@@ -106,11 +108,17 @@
             net_device: ${self}->net_dev;
             perm_addr: ${self}->net_dev->ndev.perm_addr;
             dev_addr: ${self}->net_dev->ndev.dev_addr;
+            irq: ${self}->pci_dev->data.irq;
         }
     }
 
     template sequence   Ethernet::open(Ethernet::Device dev)
     {
+        chunk   LKM::includes()
+        {
+            #include <linux/interrupt.h>
+        }
+
         chunk LKM::prototypes()
         {
             static int  rtx_ethernet_open(struct net_device *);
@@ -123,9 +131,28 @@
                 ${Ethernet::Device} *rtx_ether_ctx = netdev_priv(dev);
 
                 ${cast local.rtx_ether_ctx as Ethernet::Device};
+
+                int error;
+                {
+                    ${Log::info("installing the interrupt handler")};
+                }
+                error = request_irq(${local.rtx_ether_ctx.irq},
+                                    rtx_ethernet_interrupt_handler,
+                                    IRQF_SHARED,
+                                    ${config.name},
+                                    dev);
+                if (error)
+                {
+                    ${Log::info("Cannot register the interrupt handler")};
+                    goto error;
+                }
+
                 ${pointcut ::IMPLEMENTATION(local.rtx_ether_ctx)};
 
                 return 0;
+
+            error:
+                return error;
             }
         }
     }
@@ -165,7 +192,16 @@
                 ${Ethernet::Device} *rtx_ether_ctx = netdev_priv(dev);
 
                 ${cast local.rtx_ether_ctx as Ethernet::Device};
-                ${pointcut ::IMPLEMENTATION(local.rtx_ether_ctx)};
+
+                /* TODO: change this pointcut into a pointcut/adapter/callback: */
+                {
+                    ${pointcut ::IMPLEMENTATION(local.rtx_ether_ctx)};
+                }
+
+                free_irq(${local.rtx_ether_ctx.irq}, dev);
+                {
+                    ${Log::info("interrupt handler free'ed")};
+                }
 
                 return 0;
             }
@@ -187,9 +223,12 @@
         {
             static enum irqreturn   rtx_ethernet_interrupt_handler(int irq, void *dev_id)
             {
-                ${Ethernet::Device} *rtx_ether_ctx = dev_id;
+                ${Ethernet::AbstractDevice} *rtx_net_dev = dev_id;
+                ${cast local.rtx_net_dev as Ethernet::AbstractDevice};
+                ${Ethernet::Device} *rtx_ether_ctx;
+                ${cast local.rtx_ether_ctx as Ethernet::Device};
+                rtx_ether_ctx = ${local.rtx_net_dev.rtx_ether_ctx};
 
-                ${cast local.rtx_ether_ctx as Ethernet::Device};
                 ${pointcut ::IMPLEMENTATION(local.rtx_ether_ctx)};
 
                 return IRQ_NONE;
@@ -267,14 +306,11 @@
              */
             int bars = ${rtx_pci_dev.bars};
             unsigned char /* __iomem */ *ioaddr = ${rtx_pci_dev.ioaddr};
-            int irq = ${rtx_pci_dev.irq};
             ${cast local.bars as Builtin::number};
-            ${cast local.irq as Builtin::number};
             ${cast local.rtx_ether_ctx as Ethernet::Device};
             ${pointcut Ethernet::adapter_init_context(local.rtx_ether_ctx,
                                                       local.bars,
-                                                      local.ioaddr,
-                                                      local.irq)};
+                                                      local.ioaddr)};
             ${pointcut Ethernet::adapter_reset(local.rtx_ether_ctx)};
             ${pointcut Ethernet::adapter_load_mac_address(local.rtx_ether_ctx)};
             memcpy(${local.rtx_ether_ctx.perm_addr},
diff --git a/rathaxes/samples/e1000/ethernet.rti b/rathaxes/samples/e1000/ethernet.rti
--- a/rathaxes/samples/e1000/ethernet.rti
+++ b/rathaxes/samples/e1000/ethernet.rti
@@ -18,6 +18,11 @@
         chunk       LKM::includes();
         decl        data_types();
         attribute   Builtin::symbol.scalar netdev;
+        /*
+         * XXX: should be a Ethernet::Device, but that causes a circular
+         * dependency:
+         */
+        attribute   Builtin::symbol rtx_ether_ctx;
     }
 
     provided type   Device
@@ -36,10 +41,12 @@
          */
         attribute   Builtin::symbol.scalar      perm_addr;
         attribute   Builtin::symbol.scalar      dev_addr;
+        attribute   Builtin::symbol.scalar      irq;
     }
 
     required sequence   open(Ethernet::Device)
     {
+        provided chunk  LKM::includes();
         provided chunk  LKM::prototypes();
         provided chunk  LKM::code();
     }
@@ -69,8 +76,7 @@
 
         provided pointcut   Ethernet::adapter_init_context(Ethernet::Device,
                                                            Builtin::number,
-                                                           Builtin::symbol,
-                                                           Builtin::number);
+                                                           Builtin::symbol);
         provided pointcut   Ethernet::adapter_reset(Ethernet::Device);
         provided pointcut   Ethernet::adapter_load_mac_address(Ethernet::Device);
     }
diff --git a/rathaxes/samples/e1000/lkm.rtx b/rathaxes/samples/e1000/lkm.rtx
--- a/rathaxes/samples/e1000/lkm.rtx
+++ b/rathaxes/samples/e1000/lkm.rtx
@@ -4,13 +4,6 @@
     {
         Log::info("opening the device");
 
-        /*
-         * Maybe e1000::create_device should be called from here, to be
-         * more coherent.
-         */
-
-        e1000::setup_interrupt_handler(dev);
-        Log::info("interrupt handler installed");
 
         e1000::set_up_device(dev);
         Log::info("device activated");
@@ -32,9 +25,6 @@
          */
         e1000::free_rx_tx(dev);
         Log::info("free'ed up rx/tx resources");
-
-        e1000::free_interrupt_handler(dev);
-        Log::info("interrupt handler free'ed");
     }
 
     Ethernet::interrupt_handler(Ethernet::Device dev)
