#
# Makefile for the Linux Wireless network device drivers.
#
# Original makefile by Peter Johanson
#
# NOTE: This make file can serve as both an external Makefile (launched
#       directly by the user), or as the sub-dir Makefile used by the kernel
# 	build system.

# If CONFIG_IPW* isn't set, we'll assume the user has never configured
# their kernel to include this module and set up some defaults.
#
# NOTE: If you have previously added the IPW project to your kernel 
# 	and configured it for inclusion, these settings will be 
#	overridden by your kernel configuration.
ifndef CONFIG_IPW3945
EXTERNAL_BUILD=y
CONFIG_IPW3945=m
CONFIG_IPW3945_DEBUG=y

# NOTE:  QoS support is currently broken in the build.  DO NOT ENABLE.
# CONFIG_IPW3945_QOS=y

# If you are not interested in using monitor mode, simply comment out:
#
# NOTE:  If you have problems compiling due to IW_MODE_MONITOR not being
#        defined then you need to update the wireless extension version
#	 installed in your kernel, or comment this line out.
# CONFIG_IPW3945_MONITOR=y

# If you are interested in using radiotap headers in monitor mode,
# simply uncomment:
#
# NOTE:  To use RADIOTAP you must also enable MONITOR above.
# CONFIG_IEEE80211_RADIOTAP=y

# The above monitor mode provides standard monitor mode.  The following
# will create a new interface (named raw%d) which will be sent all
# 802.11 frames received on the interface
#
# NOTE:  To use PROMISCUOUS you must also enable MONITOR above.
# CONFIG_IPW3945_PROMISCUOUS=y

# The following, if enabled, will add a sysfs entry 'rx' that raw
# 802.11 radiotap formatted packets can be written to.  Those packets
# will be passed to the driver as if they were received from over the
# air.  This is useful in debugging features not supported by your AP.
# CONFIG_IPW3945_SIM_RX=y

endif

ifneq ('$(notdir $(if $(realpath /), $(realpath ${SHELL}), ${SHELL}))','bash')
$(warning )
$(warning WARNING: $$SHELL not set to bash.)
$(warning If you experience build errors, try)
$(warning 'make SHELL=/bin/bash'.)
$(warning )
endif

ifneq ($(M),)
	DIR=$(M)
else
	DIR=$(shell pwd)
endif

# If IEEE80211_INC has not been specified by the user then we try and 
# determine if the current kernel contains a subsystem.
ifeq ($(IEEE80211_INC),)
	# If the user specified a KSRC target, then ignore the default
	# search locations and look *ONLY* in KSRC and KSRC/build
	ifeq ($(KSRC),)
		# We check both /lib/modules/$(shell uname -r)/build as 
		# well as /lib/modules/ since the later is where 'make 
		# install' places the ieee80211 files with the 
		# out-of-tree ieee80211 subsystem.
		IEEE80211_RES := $(shell $(DIR)/snapshot/find_ieee80211 \
			/lib/modules/$(shell uname -r) \
			/lib/modules/$(shell uname -r)/build)
	else
		IEEE80211_RES := $(shell $(DIR)/snapshot/find_ieee80211 \
			$(KSRC) \
			$(KSRC)/build)
	endif
else
	IEEE80211_RES := $(shell $(DIR)/snapshot/find_ieee80211 \
		$(IEEE80211_INC))
endif

IEEE80211_BASE := $(shell var=($(IEEE80211_RES)) ; echo $${var[1]})
IEEE80211_PATH := $(shell var=($(IEEE80211_RES)) ; echo $${var[0]})

ifeq ($(IEEE80211_INC),)
	IEEE80211_SYM=${IEEE80211_BASE}/net/ieee80211
else
	IEEE80211_SYM=${IEEE80211_BASE}
endif
# If the ieee80211 subsystem is not found in the default kernel
# build location then we need to add the include path and 
# verify that the running kernel is not built using conflicting module
# version information
ifneq ($(strip $(IEEE80211_BASE)), /lib/modules/$(shell uname -r)/build/)
	IEEE80211_DUPLICATE := $(shell \
		[ -e /lib/modules/$$(uname -r)/build/Module.symvers ] && \
		grep -q ieee80211 /lib/modules/$$(uname -r)/build/Module.symvers && \
		echo y)
	EXTRA_CFLAGS += -I$(IEEE80211_PATH)
endif

# Parse out the ieee80211 subsystem version from the ieee80211.h header
IEEE80211_API := $(shell $(DIR)/snapshot/check_ieee80211_compat $(IEEE80211_PATH))
EXTRA_CFLAGS += -DIPW3945_COMPAT=$(IEEE80211_API)

ifeq ($(CONFIG_IPW3945_DEBUG),y)
	EXTRA_CFLAGS += -g -Wa,-adhlms=$@.lst
endif

ifdef KBUILD_EXTMOD
	EXTRA_CFLAGS += -I$(KBUILD_EXTMOD)
endif

list-m :=
list-$(CONFIG_IPW3945) += ipw3945
obj-$(CONFIG_IPW3945) += ipw3945.o 

#
# Begin dual Makefile mode here.  First we provide support for when we
# are being invoked by the kernel build system
#
ifneq ($(KERNELRELEASE),)

# If you receive a compile message about multiple definitions of
# CONFIG_IPW3945_DEBUG, then you have IPW* into the full kernel build, and 
# these definitions are now being set up by the kernel build system.  
#
#
# To correct this, remove any CONFIG_{IPW3945,IEEE80211}* lines from 
# $(KSRC)/.config and $(KSRC)/include/linux/autoconf.h

EXTRA_CFLAGS += -DCONFIG_PM

ifeq ($(EXTERNAL_BUILD),y)
ifeq ($(CONFIG_IPW3945_DEBUG),y)
EXTRA_CFLAGS += -DCONFIG_IPW3945_DEBUG
endif
ifeq ($(CONFIG_IPW3945_MONITOR),y)
EXTRA_CFLAGS += -DCONFIG_IPW3945_MONITOR
ifeq ($(CONFIG_IEEE80211_RADIOTAP),y)
EXTRA_CFLAGS += -DCONFIG_IEEE80211_RADIOTAP
endif
ifeq ($(CONFIG_IPW3945_PROMISCUOUS),y)
EXTRA_CFLAGS += -DCONFIG_IPW3945_PROMISCUOUS
endif
endif
ifeq ($(CONFIG_IPW3945_QOS),y)
EXTRA_CFLAGS += -DCONFIG_IPW3945_QOS
endif
ifeq ($(CONFIG_IPW3945_SIM_RX),y)
EXTRA_CFLAGS += -DCONFIG_IPW3945_SIM_RX
endif
endif

else 
# Here we begin the portion that is executed if the user invoked this Makefile
# directly.

# KSRC should be set to the path to your sources
# modules are installed into KMISC
KVER  := $(shell uname -r)
KSRC := /lib/modules/$(KVER)/build
KMISC := /lib/modules/$(KVER)/kernel/drivers/net/wireless/
KMISC_INC := /lib/modules/$(KVER)/include

# KSRC_OUTPUT should be overridden if you are using a 2.6 kernel that
# has it's output sent elsewhere via KBUILD_OUTPUT= or O=
KSRC_OUTPUT := $(KSRC)

VERFILE := $(KSRC_OUTPUT)/include/linux/version.h
KERNELRELEASE := $(shell \
	if [ -r $(VERFILE) ]; then \
		(cat $(VERFILE); echo UTS_RELEASE) | \
		$(CC) -I$(KSRC_OUTPUT) $(CFLAGS) -E - | \
		tail -n 1 | \
		xargs echo; \
        else \
		uname -r; \
	fi)

MODPATH := $(DESTDIR)/lib/modules/$(KERNELRELEASE)

all: check_inc modules utils 

utils:
	@[ ! -d util ] || make -C util IEEE80211_PATH=$(IEEE80211_PATH)

check_inc:
	@( [ "$(IEEE80211_DUPLICATE)" ] && echo -e \
"\n WARNING: Your kernel contains ieee80211 symbol definitions and you\n"\
"are not using the kernel's default ieee80211 subsystem.  (Perhaps you\n"\
"used the out-of-tree ieee80211 subsystem's 'make install' or have\n"\
"provided a path to the ieee80211 subsystem via IEEE80211_INC.)\n\n"\
"If you wish to use the out-of-tree ieee80211 subsystem then it is\n"\
"recommended to use that projects' \"make patch_kernel\" facility\n"\
"and rebuild your kernel to update the Module symbol version information.\n"\
"\n"\
"Failure to do this may result in build warnings and unexpected\n"\
"behavior when running modules which rely on the ieee80211 subsystem.\n\n"\ || \
	exit 0)

	@( [ "$(IEEE80211_DUPLICATE)" ] && \
	   [ ! "$(IEEE80211_IGNORE_DUPLICATE)" ] && echo -e \
" Aborting the build.  You can force the build to continue by adding:\n\n"\
"\tIEEE80211_IGNORE_DUPLICATE=y\n\n"\
"to your make command line.\n\n" && exit 1 || exit 0)

	@( [ ! "$(IEEE80211_API)" ] && echo -e \
"\n ERROR: A compatible subsystem was not found in the following path[s]:\n\n"\
"\t$(IEEE80211_RES)\n\n"\
"You need to install the ieee80211 subsystem from http://ieee80211.sf.net\n"\
"and point this build to the location where you installed those sources, eg.:\n\n"\
"\t% make IEEE80211_INC=/usr/src/ieee80211/\n\n"\
"or use the 'make patch_kernel' within the ieee80211 subsystem to patch your\n"\
"kernel sources.\n" && exit 1 || exit 0)

	@echo -e \
" Using ieee80211 subsystem version API v$(IEEE80211_API) from:\n\n" \
"\tBase: $(IEEE80211_BASE)\n" \
"\tPath: $(IEEE80211_PATH)\n\n" \
"EXTRA_CFLAGS = $(EXTRA_CFLAGS)\n"

snapshot:
	snapshot/make_snapshot

legacy:
	snapshot/make_legacy

codingstyle:
	for file in ipw3945.{c,h}; do \
		indent -npro -kr -i8 -ts8 -sob -l80 -ss -ncs "$$file"; \
		sed -i -e "s:\ *\$$::g" -e "s:\t*\$$::g" $$file; \
	done

clean:
	@[ ! -d util ] || make -C util clean || true
	rm -f *.{mod.c,mod,o,ko,rej,orig} .*.{stamp,flags,cmd} .lst *.lst *~ 
	rm -rf $(DIR)/tmp $(DIR)/.tmp_versions Module.symvers Modules.symvers
	for file in *.{c,h}; do \
		sed -i -e "s:\ *$$::g" -e "s:\t*$$::g" $$file; \
	done

distclean: clean
	rm -f tags TAGS

MODVERDIR=$(DIR)/tmp/.tmp_versions
modules:
	mkdir -p $(MODVERDIR)
	-@([ -f $(IEEE80211_SYM)/Module.symvers ] && \
	    cp $(IEEE80211_SYM)/Module.symvers $(DIR)) || \
	  ([ -f $(IEEE80211_SYM)/Modules.symvers ] && \
	    cp $(IEEE80211_SYM)/Modules.symvers $(DIR)) || \
	  ([ -f $(IEEE80211_SYM)/.tmp_versions/ieee80211.mod ] && \
	    cp $(IEEE80211_SYM)/.tmp_versions/*.mod $(MODVERDIR)) || true
	$(MAKE) -C $(KSRC) M=$(shell pwd) MODVERDIR=$(MODVERDIR) modules

add_radiotap:
	@snapshot/add_radiotap $(KSRC)

patch_kernel:
	@snapshot/patch_kernel $(KSRC) $(IEEE80211_VER)

install:
	install -d $(KMISC)
	install -m 644 -c $(addsuffix .ko,$(list-m)) $(KMISC)
	/sbin/depmod -a ${KVER}
	@echo "Don't forget to copy firmware to your hotplug's firmware directory "
	@echo "and have the hotplug tools in place."
	@echo "See INSTALL for more information."
	@echo ""
	@echo "NOTE:  This driver is for development and validation purposes only "
	@echo "and has not been tested for regulatory compliance.  By using this "
	@echo "driver you assume responsibility for any compliance issues that may "
	@echo "arise."
	@echo ""
	@echo "Please see the README.ipw3495 for information on regulatory compliance."

uninstall:
	rm -rf $(addprefix $(KMISC),$(addsuffix .ko,$(list-m)))
	/sbin/depmod -a ${KVER}

endif # End of internal build


.PHONY: TAGS tags check_inc

RCS_FIND_IGNORE := \( -name SCCS -o -name BitKeeper -o -name .svn -o -name CVS \) -prune -o
define all-sources
	( find . $(RCS_FIND_IGNORE) -name '*.[chS]' -print )
endef

TAGS:
	$(all-sources) | etags -
tags:
	rm -f $@
	CTAGSF=`ctags --version | grep -i exuberant >/dev/null && echo "-I __initdata,__exitdata,EXPORT_SYMBOL,EXPORT_SYMBOL_NOVERS"`; \
	$(all-sources) | xargs ctags $$CTAGSF -a

