From 42729ca2560fc04703fbfa81f936e0a818b21ee1 Mon Sep 17 00:00:00 2001
From: jepler <jepler@wide.(none)>
Date: Thu, 27 Sep 2007 11:39:42 -0500
Subject: [PATCH] make hal_parport able to run

---
 src/Makefile                  |   17 +++--
 src/hal/drivers/hal_parport.c |  162 +---------------------------------------
 src/rtapi/rtapi.h             |    7 ++
 src/rtapi/sim_common.h        |   30 ++++++++
 src/rtapi/sim_rtapi_app.cc    |    1 +
 5 files changed, 54 insertions(+), 163 deletions(-)

diff --git a/src/Makefile b/src/Makefile
index 3df8145..7872a4a 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -52,9 +52,9 @@ ifeq ($(KERNELRELEASE),)
 # The "modules" target is the gateway to the kernel module build.
 default: entering configs userspace modules
 ifeq ($(RUN_IN_PLACE),yes)
-ifneq ($(BUILD_SYS),sim)
-	@if [ -f ../bin/emc_module_helper ]; then if ! [ `stat -c %u ../bin/emc_module_helper` -eq 0 -a -u ../bin/emc_module_helper ]; then $(VECHO) "You now need to run 'sudo make setuid' in order to run in place."; fi; fi
-endif
+	@for file in rtapi_app emc_module_helper bfload pci_write; do \
+	if [ -f ../bin/$$file -a \( `stat -c %u ../bin/$$file` -eq 0 -a -u ../bin/$$file \) ]; then \
+		$(VECHO) "You now need to run 'sudo make setuid' in order to run in place"; break; fi; done
 endif
 
 # trick dumb editors into knowing what's going on
@@ -589,9 +589,9 @@ obj-$(CONFIG_SAMPLER) += sampler.o
 sampler-objs := hal/components/sampler.o $(MATHSTUB)
 
 # Subdirectory: hal/drivers
-ifneq ($(BUILD_SYS),sim)
 obj-$(CONFIG_HAL_PARPORT) += hal_parport.o
 hal_parport-objs := hal/drivers/hal_parport.o $(MATHSTUB)
+ifneq ($(BUILD_SYS),sim)
 obj-$(CONFIG_PCI_8255) += pci_8255.o
 pci_8255-objs := hal/drivers/pci_8255.o
 obj-$(CONFIG_HAL_TIRO) += hal_tiro.o
@@ -618,12 +618,12 @@ obj-$(CONFIG_HAL_SPEAKER) += hal_speaker.o
 hal_speaker-objs := hal/drivers/hal_speaker.o $(MATHSTUB)
 obj-$(CONFIG_HAL_SKELETON) += hal_skeleton.o
 hal_skeleton-objs := hal/drivers/hal_skeleton.o $(MATHSTUB)
+endif
 
 ifneq "$(filter 2.6.%, $(kernelvers))" ""
 obj-$(CONFIG_PROBE_PARPORT) += probe_parport.o
 probe_parport-objs := hal/drivers/probe_parport.o $(MATHSTUB)
 endif
-endif
 
 obj-$(CONFIG_CLASSICLADDER_RT) += classicladder_rt.o
 classicladder_rt-objs := hal/classicladder/module_hal.o $(MATHSTUB)
@@ -808,7 +808,12 @@ tags:
 .PHONY: swish
 swish:
 	swish-e -c .swish_config -v 0 -i $(BASEPWD) \
-		$(dir $(BASEPWD)){tcl,share/axis/tcl,scripts,configs,docs/src,docs/man/man1}
+		$(dir $(BASEPWD))tcl \
+		$(dir $(BASEPWD))share/axis/tcl \
+		$(dir $(BASEPWD))scripts \
+		$(dir $(BASEPWD))configs \
+		$(dir $(BASEPWD))docs/src \
+		$(dir $(BASEPWD))docs/man/man1
         
 # When you depend on objects/var-ZZZ you are depending on the contents of the
 # variable ZZZ, which is assumed to depend on a Makefile, a Submakefile, or
diff --git a/src/hal/drivers/hal_parport.c b/src/hal/drivers/hal_parport.c
index 27bbcb3..7cf04f5 100644
--- a/src/hal/drivers/hal_parport.c
+++ b/src/hal/drivers/hal_parport.c
@@ -104,15 +104,9 @@
 
 #include "rtapi.h"		/* RTAPI realtime OS API */
 #include "rtapi_ctype.h"	/* isspace() */
-#ifdef RTAPI			/* realtime */
+#include "rtapi_errno.h"
 #include "rtapi_app.h"		/* RTAPI realtime module decls */
-#else /* user space */
-#include <signal.h>		/* signal() */
-#include <sys/time.h>		/* stuct timeval */
-#include <sys/types.h>		/* for select() */
-#include <unistd.h>		/* for select() */
-#include <sys/io.h>		/* iopl() */
-#endif
+
 
 #include "hal.h"		/* HAL public API decls */
 
@@ -125,21 +119,19 @@
 #ifdef FASTIO
 #define rtapi_inb inb
 #define rtapi_outb outb
-#ifdef RTAPI			/* for ULAPI, sys/io.h defines these functs */
+#ifdef SIM
+#include <sys/io.h>
+#else
 #include <asm/io.h>
 #endif
 #endif
 
-#ifdef RTAPI			/* realtime */
-#ifdef MODULE
 /* module information */
 MODULE_AUTHOR("John Kasunich");
 MODULE_DESCRIPTION("Parallel Port Driver for EMC HAL");
 MODULE_LICENSE("GPL");
 static char *cfg = "0x0278";	/* config string, default 1 output port at 278 */
 RTAPI_MP_STRING(cfg, "config string");
-#endif /* MODULE */
-#endif /* RTAPI */
 
 /***********************************************************************
 *                STRUCTURES AND GLOBAL VARIABLES                       *
@@ -219,8 +211,6 @@ static int export_output_pin(int portnum, int pin, hal_bit_t ** dbase,
 
 #define MAX_PORTS 8
 
-#ifdef RTAPI			/* realtime */
-
 #define MAX_TOK ((MAX_PORTS*2)+3)
 
 int rtapi_app_main(void)
@@ -362,148 +352,6 @@ void rtapi_app_exit(void)
     hal_exit(comp_id);
 }
 
-#else /* user space */
-
-static int done = 0;
-static void quit(int sig)
-{
-    done = 1;
-}
-
-int main(int argc, char *argv[])
-{
-    char name[HAL_NAME_LEN + 2];
-    int n, retval;
-    hal_s32_t *read_funct_flags;
-    hal_s32_t *write_funct_flags;
-    struct timeval tv;
-
-    /* ask linux for permission to use the I/O ports */
-    retval = iopl(3);
-    if (retval != 0) {
-	rtapi_print_msg(RTAPI_MSG_ERR,
-	    "PARPORT: ERROR: could not get I/O permission\n");
-	return -1;
-    }
-    /* parse command line, set up pins and parameters */
-    retval = pins_and_params(&(argv[1]));
-    if (retval != 0) {
-	return retval;
-    }
-    /* allocate space for function run/stop parameters */
-    read_funct_flags = hal_malloc((num_ports + 1) * sizeof(hal_s32_t) * 2);
-    if (read_funct_flags == 0) {
-	rtapi_print_msg(RTAPI_MSG_ERR,
-	    "PARPORT: ERROR: hal_malloc() failed\n");
-	hal_exit(comp_id);
-	return -1;
-    }
-    write_funct_flags = read_funct_flags + (num_ports + 1);
-    /* export function run/stop parameters for each port */
-    for (n = 0; n < num_ports; n++) {
-	/* export read function parameter */
-	retval =
-	    hal_param_s32_newf(HAL_RW, &read_funct_flags[n + 1],
-                    comp_id, "parport.%d.read", n);
-	if (retval != 0) {
-	    rtapi_print_msg(RTAPI_MSG_ERR,
-		"PARPORT: ERROR: port %d read funct param failed\n", n);
-	    hal_exit(comp_id);
-	    return -1;
-	}
-	/* make write function name */
-	/* export read function parameter */
-	retval =
-	    hal_param_s32_newf(name, HAL_RW, &write_funct_flags[n + 1],
-                    comp_id, "parport.%d.write", n);
-	if (retval != 0) {
-	    rtapi_print_msg(RTAPI_MSG_ERR,
-		"PARPORT: ERROR: port %d write funct param failed\n", n);
-	    hal_exit(comp_id);
-	    return -1;
-	}
-    }
-    /* export parameters for read/write all port functuons */
-    retval =
-	hal_param_s32_new("parport.read_all", HAL_RW, &read_funct_flags[0],
-	comp_id);
-    if (retval != 0) {
-	rtapi_print_msg(RTAPI_MSG_ERR,
-	    "PARPORT: ERROR: read all funct param failed\n");
-	hal_exit(comp_id);
-	return -1;
-    }
-    retval =
-	hal_param_s32_new("parport.write_all", HAL_RW,
-	&write_funct_flags[0], comp_id);
-    if (retval != 0) {
-	rtapi_print_msg(RTAPI_MSG_ERR,
-	    "PARPORT: ERROR: write all funct param failed\n");
-	hal_exit(comp_id);
-	return -1;
-    }
-    rtapi_print_msg(RTAPI_MSG_INFO,
-	"PARPORT: installed driver for %d ports\n", num_ports);
-    /* capture INT (ctrl-C) and TERM signals */
-    signal(SIGINT, quit);
-    signal(SIGTERM, quit);
-
-    /*************************************/
-    /* main loop - loops forever until */
-    /* SIGINT (ctrl-C) or SIGTERM (kill) */
-    /*************************************/
-    while (!done) {
-	if (read_funct_flags[0]) {
-	    /* run the function */
-	    read_all(port_data_array, 0);
-	    /* if flag is positive, reset it */
-	    if (read_funct_flags[0] > 0) {
-		read_funct_flags[0] = 0;
-	    }
-	}
-	for (n = 0; n < num_ports; n++) {
-	    if (read_funct_flags[n + 1]) {
-		/* run the function */
-		read_port(&(port_data_array[n]), 0);
-		/* if flag is positive, reset it */
-		if (read_funct_flags[n + 1] > 0) {
-		    read_funct_flags[n + 1] = 0;
-		}
-	    }
-	}
-	if (write_funct_flags[0]) {
-	    /* run the function */
-	    write_all(port_data_array, 0);
-	    /* if flag is positive, reset it */
-	    if (write_funct_flags[0] > 0) {
-		write_funct_flags[0] = 0;
-	    }
-	}
-	for (n = 0; n < num_ports; n++) {
-	    if (write_funct_flags[n + 1]) {
-		/* run the function */
-		write_port(&(port_data_array[n]), 0);
-		/* if flag is positive, reset it */
-		if (write_funct_flags[n + 1] > 0) {
-		    write_funct_flags[n + 1] = 0;
-		}
-	    }
-	}
-	/* set timeout to 0.05 seconds (20 Hz update rate if nothing else is
-	   running) */
-	tv.tv_sec = 0;
-	tv.tv_usec = 50000;
-	/* call select() with no file descriptors and a timeout to yield the
-	   CPU for (at least) 0,05 seconds - see NOTES section of man 2
-	   select for details */
-	select(0, 0, 0, 0, &tv);
-    }
-    hal_exit(comp_id);
-    return 0;
-}
-
-#endif
-
 /***********************************************************************
 *                  REALTIME PORT READ AND WRITE FUNCTIONS              *
 ************************************************************************/
diff --git a/src/rtapi/rtapi.h b/src/rtapi/rtapi.h
index 0812e5f..08cfb94 100644
--- a/src/rtapi/rtapi.h
+++ b/src/rtapi/rtapi.h
@@ -742,6 +742,11 @@ extern "C" {			/* Need this when the header is included in a
 */
     extern unsigned char rtapi_inb(unsigned int port);
 
+#if defined(RTAPI) && defined(SIM)
+static __inline__ void *rtapi_request_region(unsigned long size, unsigned long n, const char *name) { return (void*)1; }
+static __inline__ void rtapi_release_region(unsigned long size, unsigned long n) {}
+#endif
+
 #if defined(RTAPI) && !defined(SIM)
 /** 'rtapi_request_region() reserves I/O memory starting at 'base',
     going for 'n' bytes, for component 'name'.
@@ -900,6 +905,8 @@ static const char __module_license[] __attribute__((section(".modinfo"))) =   \
 
 #if defined(SIM)
 extern long int simple_strtol(const char *nptr, char **endptr, int base);
+extern int rtapi_cpu_khz(void);
+#define cpu_khz (rtapi_cpu_khz())
 #endif
 
 #ifdef __cplusplus
diff --git a/src/rtapi/sim_common.h b/src/rtapi/sim_common.h
index cbba419..5390671 100644
--- a/src/rtapi/sim_common.h
+++ b/src/rtapi/sim_common.h
@@ -24,6 +24,36 @@ typedef struct {
 
 static rtapi_shmem_handle shmem_array[MAX_SHM] = {{0},};
 
+#define startswith(s,p) (strncmp((s), (p), strlen((p))) == 0)
+
+static int _cpu_khz = 0;
+int rtapi_cpu_khz(void) {
+    if(_cpu_khz == 0) {
+        FILE *f;
+        f = fopen("/sys/devices/system/cpu/cpu0/cpuinfo_max_freq", "r");
+        if(f) {
+            fscanf(f, "%d", &_cpu_khz);
+            fclose(f);
+        } else {
+            double d;
+            char buf[80];
+            f = fopen("/proc/cpuinfo", "r");
+            if(f) {
+                while(fgets(buf, 80, f)) {
+                    if(startswith(buf, "cpu MHz")) {
+                        char *ptr = strchr(buf, ':') + 1;
+                        sscanf(ptr, "%lf", &d);
+                        _cpu_khz = 1000 * d;
+                    }
+                }
+                fclose(f);
+            } else {
+                _cpu_khz = 1000000;
+            }
+        }
+    }
+    return _cpu_khz;
+}
 int rtapi_shmem_new(int key, int module_id, unsigned long int size)
 {
   rtapi_shmem_handle *shmem;
diff --git a/src/rtapi/sim_rtapi_app.cc b/src/rtapi/sim_rtapi_app.cc
index b3a22cc..c77ba1b 100644
--- a/src/rtapi/sim_rtapi_app.cc
+++ b/src/rtapi/sim_rtapi_app.cc
@@ -308,6 +308,7 @@ static int slave(int fd, vector<string> args) {
 }
 
 static int master(int fd, vector<string> args) {
+    rtapi_cpu_khz();
     dlopen(NULL, RTLD_GLOBAL);
     do_load_cmd("hal_lib", vector<string>()); instance_count = 0;
     if(args.size()) { 
-- 
1.5.2.5

