From fa7d24167fbe01386b1faaf45301a3f56a017813 Mon Sep 17 00:00:00 2001
From: patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>
Date: Wed, 16 Mar 2011 17:17:58 +0000
Subject: [PATCH] Fix QEMU timer interrupts

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3387 42af7a65-404d-4744-a932-0658087f49c3
---
 ChangeLog                         |  2 ++
 TODO                              | 25 +++----------------------
 arch/x86/include/i486/arch.h      |  6 +++---
 arch/x86/include/i486/irq.h       | 14 +++++++++++---
 arch/x86/src/qemu/qemu_handlers.c |  6 +++---
 drivers/net/slip.c                | 21 ++++++++++++++++++---
 6 files changed, 40 insertions(+), 34 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index c0bae9e060..1b9b774b77 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1573,3 +1573,5 @@
 	* drivers/net/slip.c -- SLIP is now basically functional on the
 	  LPC17xx with some caveats as described in the TODO list under
 	  LPC17xx.
+	* arch/x86/include/i486/irq.h -- Fix irqrestore() macro... it was not
+	  correctly re-enabling interrupts.
\ No newline at end of file
diff --git a/TODO b/TODO
index 815a3e301d..ee9638a0b1 100644
--- a/TODO
+++ b/TODO
@@ -1,4 +1,4 @@
-NuttX TODO List (Last updated March 15, 2011)
+NuttX TODO List (Last updated March 16 2011)
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
   (5)  Task/Scheduler (sched/)
@@ -8,7 +8,7 @@ NuttX TODO List (Last updated March 15, 2011)
   (1)  pthreads (sched/)
   (1)  C++ Support
   (5)  Binary loaders (binfmt/)
- (17)  Network (net/, drivers/net)
+ (16  Network (net/, drivers/net)
   (5)  Network Utilities (netutils/)
   (2)  USB (drivers/usbdev, drivers/usbhost)
   (5)  Libraries (lib/)
@@ -30,7 +30,7 @@ NuttX TODO List (Last updated March 15, 2011)
   (3)  ARM/STR71x (arch/arm/src/str71x/)
   (4)  ARM/LM3S6918 (arch/arm/src/lm3s/)
   (4)  ARM/STM32 (arch/arm/src/stm32/)
-  (1)  Intel x86 (arch/x86)
+  (0)  Intel x86 (arch/x86)
   (4)  8051 / MCS51 (arch/8051/)
   (2)  Hitachi/Renesas SH-1 (arch/sh/src/sh1)
   (4)  Renesas M16C/26 (arch/sh/src/m16c)
@@ -308,21 +308,6 @@ o Network (net/, drivers/net)
                the mechanism for leaving and joining groups is hidden behind a wrapper
                function so that little of this incompatibilities need be exposed.
 
-  Description: The SLIP driver (drivers/net/slip.c) has had only the most superficial
-               testing.  Issues/Notes:
-               (1) A minor issue is that SLIP requires hardware handshake and
-                   that has not been implemented in the UART drivers on most platforms.
-                   You can use the slattach' option -L which enables "3-wire
-                   operation."  NOTE: This may also result in data overrun errors.
-               (2) Current State:  The driver connects and responds to PINGs (ICMP
-                   ECHO) commands.  However, when used with the TCP stack and THTTPD,
-                   there is some condition that causes a hang.  So the SLIP driver
-                   is still not ready for prime-time.
-               (3) Finally, the Linux slip driver is hard-coded to use an MTU of 296
-                   so setting CONFIG_NET_BUFSIZE to a 296 is also advised.
-  Status:      Open
-  Priority:    Medium
-
 o Network Utilities (netutils/)
 
   Description: One critical part of netutils/ apps is untested: The uIP
@@ -949,10 +934,6 @@ o ARM/STM32 (arch/arm/src/stm32/)
 o Intel x86 (arch/x86)
   ^^^^^^^^^^^^^^^^^^^^
 
-  Description:  No timer interrupts in the QEMU configuration.
-  Status:       Open
-  Priorty:      Medium-High
-
 o 8051 / MCS51 (arch/8051/)
   ^^^^^^^^^^^^^^^^^^^^^^^^^
 
diff --git a/arch/x86/include/i486/arch.h b/arch/x86/include/i486/arch.h
index c5e4adad55..077c766261 100755
--- a/arch/x86/include/i486/arch.h
+++ b/arch/x86/include/i486/arch.h
@@ -121,7 +121,7 @@
 #define PIC2_OCW2                0xa0
 
 #  define PIC_OCW2_ACT_SHIFT     (0)
-#  define PIC_OCW2_ACT_SHIFT     (7 << PIC_OCW2_ACT_SHIFT)
+#  define PIC_OCW2_ACT_MASK      (7 << PIC_OCW2_ACT_SHIFT)
 #    define PIC1_OCW2_ACT_IRQ0   (0 << PIC_OCW2_ACT_SHIFT) /* Act on IRQ 0 */
 #    define PIC1_OCW2_ACT_IRQ1   (1 << PIC_OCW2_ACT_SHIFT) /* Act on IRQ 1 */
 #    define PIC1_OCW2_ACT_IRQ2   (2 << PIC_OCW2_ACT_SHIFT) /* Act on IRQ 2 */
@@ -141,7 +141,7 @@
 #    define PIC2_OCW2_ACT_IRQ15  (7 << PIC_OCW2_ACT_SHIFT) /* Act on IRQ 15 */
 
 #  define PIC_OCW2_EOI_SHIFT     (5)
-#  define PIC_OCW2_EOI_SHIFT     (7 << PIC_OCW2_EOI_SHIFT)
+#  define PIC_OCW2_EOI_MASK      (7 << PIC_OCW2_EOI_SHIFT)
 #    define PIC_OCW2_EOI_AUTO    (0 << PIC_OCW2_EOI_SHIFT) /* Rotate in Auto EOI Mode (Clear) */
 #    define PIC_OCW2_EOI_NONSPEC (1 << PIC_OCW2_EOI_SHIFT) /* Non Specific EOI */
 #    define PIC_OCW2_EOI_SPEC    (3 << PIC_OCW2_EOI_SHIFT) /* Specific EOI */
@@ -171,7 +171,7 @@
 #  define PIC_OCW3_POLLCMD       (1 << 2) /* Poll command */
 #  define PIC_OCW3_ONE           (1 << 3) /* Must be set to 1 */
 #  define PIC_OCW3_SM_SHIFT      (5)
-#  define PIC_OCW3_SM_SHIFT      (3 << PIC_OCW3_SM_SHIFT)
+#  define PIC_OCW3_SM_MASK       (3 << PIC_OCW3_SM_SHIFT)
 #    define PIC_OCW3_RSM         (2 << PIC_OCW3_SM_SHIFT) /* Reset Special Mask */
 #    define PIC_OCW3_SSM         (3 << PIC_OCW3_SM_SHIFT) /* Set Special Mask */
 
diff --git a/arch/x86/include/i486/irq.h b/arch/x86/include/i486/irq.h
index cb78e4d814..0abf384eae 100755
--- a/arch/x86/include/i486/irq.h
+++ b/arch/x86/include/i486/irq.h
@@ -135,9 +135,17 @@ static inline irqstate_t irqflags()
   return flags;
 }
 
-/* Get a sample of the FLAGS register, determine if interrupts are disabled */
+/* Get a sample of the FLAGS register, determine if interrupts are disabled.
+ * If the X86_FLAGS_IF is cleared by cli, then interrupts are disabled.  If
+ * if the X86_FLAGS_IF is set by sti, then interrupts are enable.
+ */
 
 static inline bool irqdisabled(irqstate_t flags)
+{
+  return ((flags & X86_FLAGS_IF) == 0);
+}
+
+static inline bool irqenabled(irqstate_t flags)
 {
   return ((flags & X86_FLAGS_IF) != 0);
 }
@@ -169,9 +177,9 @@ static inline irqstate_t irqsave(void)
 
 static inline void irqrestore(irqstate_t flags)
 {
-  if (irqdisabled(flags))
+  if (irqenabled(flags))
     {
-      irqdisable();
+      irqenable();
     }
 }
 
diff --git a/arch/x86/src/qemu/qemu_handlers.c b/arch/x86/src/qemu/qemu_handlers.c
index 05b5dee3f1..fbcb327aa0 100644
--- a/arch/x86/src/qemu/qemu_handlers.c
+++ b/arch/x86/src/qemu/qemu_handlers.c
@@ -178,16 +178,16 @@ uint32_t *irq_handler(uint32_t *regs)
    * involved the slave.
    */
 
-  if (irq >= 40)
+  if (irq >= IRQ8)
     {
       /* Send reset signal to slave */
 
-      idt_outb(0x20, 0xa0);
+      idt_outb(PIC_OCW2_EOI_NONSPEC, PIC2_OCW2);
     }
 
   /* Send reset signal to master */
 
-  idt_outb(0x20, 0x20);
+  idt_outb(PIC_OCW2_EOI_NONSPEC, PIC1_OCW2);
 
   /* Dispatch the interrupt */
 
diff --git a/drivers/net/slip.c b/drivers/net/slip.c
index 94baab5755..725d7f8469 100644
--- a/drivers/net/slip.c
+++ b/drivers/net/slip.c
@@ -68,7 +68,8 @@
 
 /* NOTE:  Slip requires UART hardware handshake.  If hardware handshake is
  * not available with your UART, then you might try the 'slattach' option
- * -L which is supposed to enable "3-wire operation."
+ * -L which enable "3-wire operation."  That allows operation without the
+ * hardware handshake (but with the possibility of data overrun).
  */
 
 /* Configuration ************************************************************/
@@ -93,8 +94,15 @@
 #  define CONFIG_SLIP_DEFPRIO 128
 #endif
 
-/* The Linux slip module hard-codes its MTU size to 296.  So you might as
- * well set CONFIG_NET_BUFSIZE to 296 as well.
+/* The Linux slip module hard-codes its MTU size to 296 (40 bytes for the
+ * IP+TPC headers plus 256 bytes of data).  So you might as well set
+ * CONFIG_NET_BUFSIZE to 296 as well.
+ *
+ * There may be an issue with this setting, however.  I see that Linux uses
+ * a MTU of 296 and window of 256, but actually only sends 168 bytes of data:
+ * 40 + 128.  I believe that is to allow for the 2x worst cast packet
+ * expansion.  Ideally we would like to advertise the 256 MSS, but restrict
+ * uIP to 128 bytes (possibly by modifying the uip_mss() macro).
  */
 
 #if CONFIG_NET_BUFSIZE < 296
@@ -995,6 +1003,13 @@ int slip_initialize(int intf, const char *devname)
   /* Register the device with the OS so that socket IOCTLs can be performed */
 
   (void)netdev_register(&priv->dev);
+
+  /* When the RX and TX tasks were created, the TTY file descriptor was
+   * dup'ed for each task.  This task no longer needs the file descriptor
+   * and we can safely close it.
+   */
+
+  close(priv->fd);
   return OK;
 }
 
-- 
GitLab