# Edit this path to match with your system (it should point to the root
# directory of the Linux kernel source)
KERNEL_PATH=/usr/src/linux

# Systems using external pcmcia-cs package (instead of kernel-tree pcmcia
# modules):
#
# hostap_cs.o can be compiled either by copying driver/* into pcmcia-cs root
# directory and compiling pcmcia-cs package or by configuring PCMCIA_PATH
# and using this Makefile with 'make pccard; make install_pccard'.
# Leave this blank for kernel-tree PCMCIA compilations (CONFIG_PCMCIA):
PCMCIA_PATH=

CC=gcc
CFLAGS=-O2 -D__KERNEL__ -DMODULE -Wall -c $(EXTRA_CFLAGS)

VERFILE := $(KERNEL_PATH)/include/linux/version.h
KERNELRELEASE := $(shell if [ -r $(VERFILE) ]; \
	then (cat $(VERFILE); echo UTS_RELEASE) | cpp | tail -1 | xargs echo; \
	else uname -r; fi)
KERNELVER := $(shell echo "$(KERNELRELEASE)" | \
	sed "s/\([0-9]*\.[0-9]*\.[0-9]*\).*/\1/")
MODPATH := /lib/modules/$(KERNELRELEASE)
MODULE_CS := hostap_cs.o
MODULE_PLX := hostap_plx.o
MODULE_PCI := hostap_pci.o
MODPATH_CS := $(MODPATH)/pcmcia
MODPATH_PLX := $(MODPATH)/net
MODPATH_PCI := $(MODPATH)/net

MSRC=driver/modules


all:
	@echo "Select the version to be build:"
	@echo "make pccard      PC Card (PCMCIA) driver (Prism2 or Prism2.5)"
	@echo "make plx         PLX-based PCI card with PC Card"
	@echo "make pci         Prism 2.5 PCI Card"
	@echo "make crypt       Make hostap_crypt*.o"
	@echo "make hostap      Make hostap.o"


include $(KERNEL_PATH)/.config

ifdef CONFIG_ALPHA
CFLAGS += -mno-fp-regs -ffixed-8
endif

ifdef CONFIG_MODVERSIONS
CFLAGS += -DMODVERSIONS -include $(KERNEL_PATH)/include/linux/modversions.h
endif

INCLUDES=-I$(KERNEL_PATH)/include
ifdef PCMCIA_PATH
ifndef CONFIG_PCMCIA
INCLUDES=-I$(PCMCIA_PATH)/include -I$(KERNEL_PATH)/include
endif
endif

ifdef CONFIG_SMP
CFLAGS += -D__SMP__ -DSMP
endif

ifndef CONFIG_FRAME_POINTER
CFLAGS += -fomit-frame-pointer
endif


ifdef CONFIG_MODVERSIONS

VERFILES=$(MSRC)/hostap.ver $(MSRC)/hostap_crypt.ver
$(MSRC)/hostap_crypt_wep.o: $(VERFILES)
$(MSRC)/hostap.o: $(VERFILES)
$(MSRC)/hostap_cs.o: $(VERFILES)
$(MSRC)/hostap_plx.o: $(VERFILES)
$(MSRC)/hostap_pci.o: $(VERFILES)
INCLUDES_MODVER=-include $(MSRC)/hostap_crypt.ver -include $(MSRC)/hostap.ver

GENKSYMS=/sbin/genksyms
ifdef CONFIG_SMP
        GENKSYMS += -p smp_
endif

$(MSRC)/hostap_crypt.ver: $(MSRC)/hostap_crypt.c
	$(CC) $(INCLUDES) $(CFLAGS) -E -D__GENKSYMS__ $^ | \
		$(GENKSYMS) -k $(KERNELVER) > $@
$(MSRC)/hostap.ver: $(MSRC)/hostap.c
	$(CC) $(INCLUDES) $(CFLAGS) -E -D__GENKSYMS__ $^ | \
		$(GENKSYMS) -k $(KERNELVER) > $@

endif


.c.o:
	$(CC) $(INCLUDES) $(INCLUDES_MODVER) $(CFLAGS) -o $@ $<


pccard:	$(MSRC)/$(MODULE_CS) hostap crypt install_info
plx:	$(MSRC)/$(MODULE_PLX) hostap crypt install_info
pci:	$(MSRC)/$(MODULE_PCI) hostap crypt install_info
crypt:  $(MSRC)/hostap_crypt.o $(MSRC)/hostap_crypt_wep.o
hostap: $(MSRC)/hostap.o

SRC=$(MSRC)/hostap_hw.c $(MSRC)/hostap_wlan.h $(MSRC)/hostap_config.h \
	$(MSRC)/hostap_download.c \
	$(MSRC)/hostap_ap.h \
	$(MSRC)/hostap_crypt.h $(MSRC)/hostap.h $(MSRC)/hostap_compat.h

$(MSRC)/hostap_crypt.o: $(MSRC)/hostap_crypt.c $(MSRC)/hostap_crypt.h

$(MSRC)/hostap_crypt_wep.o: $(MSRC)/hostap_crypt_wep.c $(MSRC)/hostap_crypt.h

$(MSRC)/hostap.o: $(MSRC)/hostap.c $(MSRC)/hostap.h \
	$(MSRC)/hostap_ap.h $(MSRC)/hostap_ap.c \
	$(MSRC)/hostap_ioctl.c $(MSRC)/hostap_proc.c \
	$(MSRC)/hostap_wlan.h $(MSRC)/hostap_config.h \
	$(MSRC)/hostap_compat.h

$(MSRC)/$(MODULE_CS): $(MSRC)/hostap_cs.c $(SRC)

$(MSRC)/$(MODULE_PLX): $(MSRC)/hostap_plx.c $(SRC)

$(MSRC)/$(MODULE_PCI): $(MSRC)/hostap_pci.c $(SRC)


install_info:
	@echo
	@echo "Run 'make install_pccard' as a root to install $(MODULE_CS)"
	@echo "Run 'make install_plx' as a root to install $(MODULE_PLX)"
	@echo "Run 'make install_pci' as a root to install $(MODULE_PCI)"
	@echo

install: install_info

install_check:
	@if [ ! -d "$(MODPATH)" ]; then \
		echo "Module installation directory not valid: '$(MODPATH)'"; \
		echo -n "Most probable cause for this is modified kernel "; \
		echo "headers."; \
		echo -n "You will need to install module file "; \
		echo "($(MSRC)/hostap_*.o) manually."; \
		exit 1; \
	fi

install_hostap: $(MSRC)/hostap.o
	@echo "Installing hostap.o to $(MODPATH_PCI)"
	mkdir -p $(MODPATH_PCI)
	cp $(MSRC)/hostap.o $(MODPATH_PCI)

install_crypt: $(MSRC)/hostap_crypt.o $(MSRC)/hostap_crypt_wep.o
	@echo "Installing hostap_crypt*.o to $(MODPATH_PCI)"
	mkdir -p $(MODPATH_PCI)
	cp $(MSRC)/hostap_crypt*.o $(MODPATH_PCI)

install_pccard: install_check install_crypt install_hostap
	@echo "Installing $(MODULE_CS) to $(MODPATH_CS)"
	mkdir -p $(MODPATH_CS)
	cp $(MSRC)/$(MODULE_CS) $(MODPATH_CS)
	/sbin/depmod -a
	@if [ ! -r /etc/pcmcia/hostap_cs.conf ]; then \
		echo "Installing /etc/pcmcia/hostap_cs.conf"; \
		cp driver/etc/hostap_cs.conf /etc/pcmcia/hostap_cs.conf; \
	fi
	@if [ -r /etc/pcmcia/prism2.conf ]; then \
		echo "You may need to remove old /etc/pcmcia/prism2.conf"; \
	fi

install_plx: install_check install_crypt install_hostap
	@echo "Installing $(MODULE_PLX) to $(MODPATH_PLX)"
	mkdir -p $(MODPATH_PLX)
	cp $(MSRC)/$(MODULE_PLX) $(MODPATH_PLX)
	/sbin/depmod -a

install_pci: install_check install_crypt install_hostap
	@echo "Installing $(MODULE_PCI) to $(MODPATH_PCI)"
	mkdir -p $(MODPATH_PCI)
	cp $(MSRC)/$(MODULE_PCI) $(MODPATH_PCI)
	/sbin/depmod -a


clean:
	rm -f $(MSRC)/{$(MODULE_CS),$(MODULE_PLX),$(MODULE_PCI)}
	rm -f $(MSRC)/{hostap.o,hostap_crypt.o,hostap_crypt_wep.o}
	rm -f $(MSRC)/*.ver
