# File: drivers/net/Makefile
#
# Makefile for the Linux LXT1001 device driver, v1.0.
#  
# To create a debuggable driver with emulation, do
#   make clean
#   make depend
#   make all
#

#-------------------------------------------------------------
# Compilation Tools
#-------------------------------------------------------------
CC := gcc
LD := ld

# The adjust-vma value may need to be changed if listing file addresses
# don't match addresses when loaded in the kernel.
# The value was 4, then changed to 10 in kernel version 2.0.35
LISTING := objdump -S 
LISTINGFLAGS := --adjust-vma=10

RELOCATESTABS := ./relocate_stabs.pl

#-------------------------------------------------------------
# Object files
#-------------------------------------------------------------
DRIVER_MODULE  := jt1lin.o
DEBUG_MODULE   := jt1dbg.o
#EMU_MODULE     := jt1emu.o

DRIVER_OBJ     := jt1lin.o
DEBUG_OBJ      := $(DRIVER_OBJ:.o=_d.o)
#EMU_OBJ        := $(DRIVER_OBJ:.o=_e.o)
#EMU_OTHER_OBJS := decinit_e.o decintr_e.o decrecv_e.o decsend_e.o \
#emuinit_e.o emuio_e.o emurecv_e.o emusend_e.o linsvcs_e.o  
#EMU_ALL_OBJS   := $(EMU_OBJ) $(EMU_OTHER_OBJS)

TARGET_MODULE := $(DEBUG_MODULE)

#-------------------------------------------------------------
# Compilation flags
#-------------------------------------------------------------
ifndef TOPDIR
TOPDIR := /usr/src/linux
endif

INCLUDES := -I $(TOPDIR)/include
CFLAGS := -D__KERNEL__ -DMODULE -O2 -m486 -Wall -finline-functions $(INCLUDES) 
CFLAGS := $(CFLAGS) -DMODVERSIONS
CFLAGS := $(CFLAGS) -DWORKAROUND_ERROR_HEADERS -DWORKAROUND_MWI \
	-DWORKAROUND_LATENCY -DWORKAROUND_WRITE_COMBINE

# Memory base should gives better performance 
CFLAGS := $(CFLAGS) -DUSE_MEMORY_BASE

# Performance_tx enhances performance 
#CFLAGS := $(CFLAGS) -DSTANDARD_TX 
CFLAGS := $(CFLAGS) -DPERFORMANCE_TX
CFLAGS := $(CFLAGS) -DPERFORMANCE_LXT1002

# Safer transmission
CFLAGS := $(CFLAGS) -DSERIALIZE_TX

# this is for SMP support, be sure to uncomment on multiprocessor boxes 
#CFLAGS := $(CFLAGS) -D__SMP__

# Diagnostics 
CFLAGS := $(CFLAGS) -DNOS_LINUX

# A bug in the default linker script destroys address information 
# during linking, making symbolic debugging impossible. 
# So, we supply our own linker script copied from  
# /usr/lib/ldscripts/elf_i386.xr and modified. 
#LDFLAGS := -T ./elf_i386.xr.mod

#------------------------------------------------------------
# Common rules
#------------------------------------------------------------

%.o: %.c
	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<

%_d.o: %.c
	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -g -DJT_DEBUG -c -o $@ $<

#%_e.o: %.c
#	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -g -DJT_DEBUG -DEMULATION -DNOS_LINUX /
#-DGIGEMACONE -DDAL_INLINE -c -o $@ $<

# Our own rule to make source/assembly listing files
%.lst: %.o
	$(LISTING) $(LISTINGFLAGS) $< > $@

#------------------------------------------------------------
# Default target
#------------------------------------------------------------
.PHONY: all
all: $(DRIVER_MODULE) $(DEBUG_MODULE) # $(EMU_MODULE)

#------------------------------------------------------------
# Include dependency files they exist
#------------------------------------------------------------
ifeq (.depend,$(wildcard .depend))
include .depend
endif

#------------------------------------------------------------
# Targets
#------------------------------------------------------------

# Target to make the plain driver
$(DRIVER_MODULE) : $(DRIVER_OBJ:.o=.c)
	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
	$(LISTING) $(LISTINGFLAGS) $@ > $(@:.o=.lst)

# Target to make a debuggable driver
# Must do objdump before relocate
$(DEBUG_OBJ) : $(DRIVER_OBJ:.o=.c)

$(DEBUG_MODULE) $(DEBUG_MODULE:.o=.lst) : $(DEBUG_OBJ)
	$(LD) $(LDFLAGS) -r -o $@ $(DEBUG_OBJ)
	$(LISTING) $(LISTINGFLAGS) $@ > $(@:.o=.lst)

# This only to use kernel debugger
	#$(RELOCATESTABS) $@

# Link all object files to make relocatable module
# Must do objdump before relocate
$(EMU_OBJ) : $(DRIVER_OBJ:.o=.c) jt1emu.h 

$(EMU_MODULE) $(EMU_MODULE:.o=.lst) : $(EMU_ALL_OBJS)
	$(LD) $(LDFLAGS) -r -o $@ $(EMU_ALL_OBJS)
	$(LISTING) $(LISTINGFLAGS) $@ > $(@:.o=.lst)
	$(RELOCATESTABS) $@

# Remove all temporary and generated files
.PHONY: clean
clean:
	rm -f core *.o *.a *.s *.lst

# Compute the source file dependencies
#.PHONY: depend
#depend:
#	gcc -M $(EMU_OTHER_OBJS:_e.o=.c) $(CFLAGS) > .depend
#	perl -p -i -e 's/\.o/_e\.o/g' .depend

# Converts the source files from Windows
.PHONY: convert
convert: 
	dos2unix *.c makefile

# Install the driver for testing
.PHONY: test-eth0
test-eth0: all
	sync
	- ifconfig eth0 down
	- rmmod $(TARGET_MODULE:.o=)
	insmod $(TARGET_MODULE:.o=) $(TESTFLAGS)
	- klogd -i
	sync
	sleep 1
	ifup eth0

.PHONY: test-eth1
test-eth1: all
	sync
	- ifconfig eth1 down
	- rmmod $(TARGET_MODULE:.o=)
	insmod $(TARGET_MODULE:.o=) $(TESTFLAGS)
	- klogd -i
	sync
	sleep 1
	ifconfig eth1 hw ether 00:e0:83:00:00:71
	ifup eth1
#	ifconfig eth1 hw ether 00:e0:83:00:00:`ifconfig eth1|perl -ne 'm/inet addr:\d+.\d+.\d+.(\d+)/ && print $$1'`


