#
#       Equinox SuperSerial Technology (SST) driver for Linux
#
#       Copyright (C) 1989-2002 Equinox Systems Inc.  

include ../version.include

HERE=$(shell pwd)
INC = $(HERE)/../include
.EXPORT ALL VARIABLES:

HFILES = ../include/icp.h  ../include/ticp.h  ../include/sst.h \
	../include/eqnx.h  ../include/eqn.h  ../include/ist.h \
	../include/version.h ../include/mra.h ./linux.h

INCLUDES = -I$(INC) -I./ 
MODOBJS = sst.o linux.o rampadmn.o rampsrvc.o
MCFLAGS = -D__KERNEL__ -Wall -Wstrict-prototypes -O2 -pipe \
	-DMODULE -DLINUX  -DRAMP_LINUX -D_LOOSE_KERNEL_NAMES
NO_OPT_MCFLAGS = -D__KERNEL__ -Wall -Wstrict-prototypes \
	-pipe -DRAMP_LINUX

########################################################################
# Environment tests
 
# Kernel Search Path
# These are all the places where kernel source might exist
KSP :=  /usr/src/linux-$(shell uname -r) \
        /usr/src/linux-$(shell uname -r | sed 's/-.*//') \
        /usr/src/linux-$(shell uname -r | sed 's/\([0-9]*\.[0-9]*\)\..*/\1/') \
        /usr/src/linux
			  
# Only look at paths that exist and have an include/linux sub-directory
test_dir = $(shell [ -e $(dir)/include/linux ] && echo $(dir))
KSP := $(foreach dir, $(KSP), $(test_dir))
			   
# Kernel Source Location
# we will use this first valid entry in the search path
KSRC := $(firstword $(KSP))
KINC := $(KSRC)/include        

ifeq (,$(KSRC))
   $(error The Linux kernel source could not be found)
endif
   
# Check for version.h, config.h and modversions.h files
VERSION_FILE := $(KSRC)/include/linux/version.h
CONFIG_FILE  := $(KSRC)/include/linux/config.h
MODV_FILE    := $(KSRC)/include/linux/modversions.h
     
ifeq (,$(wildcard $(VERSION_FILE)))
   $(error Linux kernel source not configured - missing include/linux/version.h)
endif
	 
ifeq (,$(wildcard $(CONFIG_FILE)))
   $(error Linux kernel source not configured - missing include/linux/config.h)
endif
	     
ifeq (,$(wildcard $(MODV_FILE)))
   $(warning - missing include/linux/modversions.h)
endif
	     
# get the kernel version - we use this to find the correct install path
KVER := $(shell $(CC) $(CFLAGS) -E -dM -I $(KINC) $(VERSION_FILE) | grep UTS_RELEASE | \
	awk '{ print $$3 }' | sed 's/\"//g')

ifneq ($(KVER),$(shell uname -r))
   $(warning ***)
   $(warning *** Warning: kernel source version ($(KVER)))
   $(warning *** does not match running kernel  ($(shell uname -r)).)
   $(warning *** Its likely that the Equinox SST driver cannot)
   $(warning *** be built or cannot be loaded.)
   $(warning ***)
endif

# pick the appropriate install path
ifneq (,$(wildcard /lib/modules/$(KVER)/kernel))
   MODDIR := $(OBJROOT)/lib/modules/$(KVER)/kernel/drivers/char
else
   MODDIR := $(OBJROOT)/lib/modules/$(KVER)/char
endif  

# check the SMP flag - first from config.h
SMP := $(shell $(CC) $(CFLAGS) -E -dM -I $(KINC) $(CONFIG_FILE) | \
	grep CONFIG_SMP | awk '{ print $$3 }')
ifneq ($(SMP),1)
   SMP := 0
endif
			        
# check that running kernel matches SMP configuration from config.h
ifneq ($(SMP),$(shell uname -a | grep SMP > /dev/null 2>&1 && echo 1 || echo 0))
   $(warning ***)
ifeq ($(SMP),1)
      $(warning *** Warning: kernel source configuration (SMP))
      $(warning *** does not match running kernel (UP).)
else
      $(warning *** Warning: kernel source configuration (UP))
      $(warning *** does not match running kernel (SMP).)
endif
      $(warning *** Its likely that the Equinox SST driver cannot)
      $(warning *** be built or cannot be loaded.)
      $(warning ***)
endif          

ifeq ($(SMP), 1)
   MCFLAGS += -D__SMP__
endif

# check MODVERSIONS configuration from running kernel
MODV := $(shell cat /proc/ksyms | grep tty_register_driver | cut -d" " -f2)
ifneq ($(MODV), tty_register_driver)
   MODV := 1
else
   MODV := 0
endif

# check the MODVERSIONS configuration, if modversions.h is available
ifeq (,$(wildcard $(MODV_FILE)))
   MODVER := $(MODV)
else
   MODVER := $(shell $(CC) $(CFLAGS) -E -dM -I $(KINC) $(MODV_FILE) | \
	grep tty_register_driver)
   ifeq ($(MODVER),)
      MODVER := 0
   else
      MODVER := 1
   endif
endif

# check that MODVERSIONS configuration matches
ifneq ($(MODV), $(MODVER))
   $(warning ***)
ifeq ($(MODV),1)
   $(warning *** Warning: module versions configuration option: MODVERSIONS)
   $(warning *** [disabled] does not match running kernel [MODVERSIONS enabled].)
else
   $(warning *** Warning: module versions configuration option: MODVERSIONS)
   $(warning *** [enabled] does not match running kernel [MODVERSIONS disabled].)
endif
   $(warning *** Its likely that the Equinox SST driver cannot)
   $(warning *** be loaded.)
   $(warning ***)
endif        

ifeq "$(findstring 86, $(shell uname -m))" "86"
   MCFLAGS += -DIX86 -m486 -DISA_ENAB -DMCA_ENAB
   NO_OPT_MCFLAGS += -DIX86 -m486 -DISA_ENAB -DMCA_ENAB
endif
ifeq "$(findstring ia64, $(shell uname -m))" "ia64"
   MCFLAGS += -DIA64
   NO_OPT_MCFLAGS += -DIA64
endif
MCFLAGS2 = -DVERSNUM=\"$(REL)\"

# Completed Environment tests
########################################################################

build:	eqnx.o

install: eqnx.o
	@mkdir -p $(MODDIR) 
	@install -o root -m 664 eqnx.o $(MODDIR)

eqnx.o: $(MODOBJS)
	$(LD) -r -o eqnx.o $(MODOBJS)

sst.o: sst.c ifns.c pci.c makefile $(HFILES)
	$(CC) $(MCFLAGS) $(MCFLAGS2) -DMIDNIGHT $(INCLUDES) -I $(KINC) -c sst.c

rampadmn.o: rampadmn.c makefile $(HFILES)
	$(CC) $(NO_OPT_MCFLAGS) $(INCLUDES) -I $(KINC) -c rampadmn.c

rampsrvc.o: rampadmn.c makefile $(HFILES)
	$(CC) $(NO_OPT_MCFLAGS) $(INCLUDES) -I $(KINC) -c rampsrvc.c
	
linux.o: linux.c makefile $(HFILES)
	$(CC) $(MCFLAGS) $(INCLUDES) -I $(KINC) -c linux.c

clean:
	@rm -f eqnx.o
	@rm -f $(MODOBJS)

clobber: clean
	@rm -f $(MODDIR)/eqnx.o
