#	Copyright (C) 2004 - 2006 rt2x00 SourceForge Project
#	<http://rt2x00.serialmonkey.com>
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the
#	Free Software Foundation, Inc.,
#	59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#	Module: Makefile
#	Abstract: Makefile for rt2x00 kernel module

#
# Set the enviroment variables.
#
ifndef SUBDIRS
	SUBDIRS=$(shell pwd)
endif

ifdef KERNDIR
	KERNEL_SOURCES := $(KERNDIR)
else
	KERNEL_SOURCES := /lib/modules/$(shell uname -r)/build
endif

ifdef KERNOUT
	KERNEL_OUTPUT := KBUILD_OUTPUT=$(KERNOUT)
else
	KERNEL_OUTPUT :=
endif

ifdef KBUILD_EXTMOD
	IEEE80211_DIR := $(KBUILD_EXTMOD)/ieee80211
	RFKILL_DIR := $(KBUILD_EXTMOD)/rfkill
	CRC_ITU_T_DIR := $(KBUILD_EXTMOD)/crc-itu-t
else
	IEEE80211_DIR := ieee80211
	RFKILL_DIR := rfkill
	CRC_ITU_T_DIR := /crc-itu-t
endif

#
# Include kernel and rt2x00 config.
#
include $(KERNEL_SOURCES)/.config
include $(SUBDIRS)/config

#
# Determine if and with what options the dscape stack should be build
#
80211-objs := \
	ieee80211/ieee80211.o \
	ieee80211/ieee80211_ioctl.o \
	ieee80211/ieee80211_scan.o \
	ieee80211/ieee80211_sta.o \
	ieee80211/ieee80211_dev.o \
	ieee80211/ieee80211_iface.o \
	ieee80211/ieee80211_sysfs.o \
	ieee80211/ieee80211_sysfs_sta.o \
	ieee80211/sta_info.o \
	ieee80211/wme.o \
	ieee80211/wep.o \
	ieee80211/wpa.o \
	ieee80211/michael.o \
	ieee80211/tkip.o \
	ieee80211/aes_ccm.o

ifeq ($(CONFIG_D80211_LEDS),y)
	80211-objs += ieee80211/ieee80211_led.o
endif

ifeq ($(CONFIG_NET_SCHED),)
	80211-objs += ieee80211/fifo_qdisc.o
endif

rate_control-objs := ieee80211/rate_control.o

ifeq ($(CONFIG_D80211),y)
	obj-m += 80211.o rate_control.o
ifeq ($(CONFIG_D80211_ASM),y)
	always += $(80211-objs:.o=.s) $(rate_control-objs:.o=.s)
	EXTRA_CFLAGS := $(EXTRA_CFLAGS) -fverbose-asm
endif
endif

#
# Determine if and with what options the crc-itu-t driver should be build
#
crc-itu-t-objs := crc-itu-t/crc-itu-t.o

ifeq ($(CONFIG_CRC_ITU_T),y)
	obj-m += crc-itu-t.o
ifeq ($(CONFIG_CRC_ITU_T_ASM),y)
	always += $(crc-itu-t-objs:.o=.s)
	EXTRA_CFLAGS := $(EXTRA_CFLAGS) -fverbose-asm
endif
endif

#
# Determine if and with what options the rfkill driver should be build
#
rfkill-objs := rfkill/rfkill.o

ifeq ($(CONFIG_RFKILL),y)
	obj-m += rfkill.o
ifeq ($(CONFIG_RFKILL_ASM),y)
	always += $(rfkill-objs:.o=.s)
	EXTRA_CFLAGS := $(EXTRA_CFLAGS) -fverbose-asm
endif
endif

#
# Determine which and with what options the drivers should be build
#
ifeq ($(CONFIG_RT2400PCI),y)
	obj-m += rt2400pci.o
ifeq ($(CONFIG_RT2400PCI_ASM),y)
	always += rt2400pci.s
	EXTRA_CFLAGS := $(EXTRA_CFLAGS) -fverbose-asm
endif
endif

ifeq ($(CONFIG_RT2500PCI),y)
	obj-m += rt2500pci.o
ifeq ($(CONFIG_RT2500PCI_ASM),y)
	always += rt2500pci.s
	EXTRA_CFLAGS := $(EXTRA_CFLAGS) -fverbose-asm
endif
endif

ifeq ($(CONFIG_RT2500USB),y)
	obj-m += rt2500usb.o
ifeq ($(CONFIG_RT2500USB_ASM),y)
	always += rt2500usb.s
	EXTRA_CFLAGS := $(EXTRA_CFLAGS) -fverbose-asm
endif
endif

ifeq ($(CONFIG_RT61PCI),y)
	obj-m += rt61pci.o
ifeq ($(CONFIG_RT61PCI_ASM),y)
	always += rt61pci.s
	EXTRA_CFLAGS := $(EXTRA_CFLAGS) -fverbose-asm
endif
endif

ifeq ($(CONFIG_RT73USB),y)
	obj-m += rt73usb.o
ifeq ($(CONFIG_RT73USB_ASM),y)
	always += rt73usb.s
	EXTRA_CFLAGS := $(EXTRA_CFLAGS) -fverbose-asm
endif
endif

MAKEFLAGS += --no-print-directory
CFLAGS := -I$(IEEE80211_DIR) -I$(RFKILL_DIR) -I$(CRC_ITU_T_DIR) \
	-include $(SUBDIRS)/rt2x00_compat.h $(CFLAGS)
CPPFLAGS := -I$(IEEE80211_DIR) -I$(RFKILL_DIR) -I$(CRC_ITU_T_DIR) \
	-include $(SUBDIRS)/rt2x00_compat.h $(CPPFLAGS)

all: default

config_header:
	@if [ ! -f "rt2x00_config.h" ] || [ "rt2x00_config.h" -ot "config" ]; \
	then \
		awk -F = > rt2x00_config.h < config '/^CONFIG.*$\/ { \
			if($$2 == "y") { \
				print "#ifndef " $$1; \
				print "#define " $$1; \
				print "#endif"; \
				print "" \
			} else { \
				print "#undef " $$1; \
				print ""; \
			} \
		}'; \
	fi

default: config_header
	@$(MAKE) -C $(KERNEL_SOURCES) SUBDIRS=$(SUBDIRS) $(KERNEL_OUTPUT) \
		modules

install: config_header
	@$(MAKE) -C $(KERNEL_SOURCES) SUBDIRS=$(SUBDIRS) $(KERNEL_OUTPUT) \
		INSTALL_MOD_DIR=rt2x00 $(KERNEL_OUTPUT) modules_install
	/sbin/depmod -a

clean:
	@rm -f rt2x00_config.h
	@rm -f Modules.symvers
	@rm -f *.o ieee80211/*.o rfkill/*.o crc-itu-t/*.o
	@rm -f *.ko ieee80211/*.ko rfkill/*.ko crc-itu-t/*.ko
	@rm -f *.s ieee80211/*.s rfkill/*.s crc-itu-t/*.s
	@rm -f *.mod.c ieee80211/*.mod.c rfkill/*.mod.c crc-itu-t/*.mod.c
	@rm -f *~ ieee80211/*~ ieee80211/net/*~ rfkill/*~ rfkill/linux/*~ crc-itu-t/*~ crc-itu-t/linux/*~
	@rm -f .*.cmd ieee80211/.*.cmd rfkill/.*.cmd crc-itu-t/.*.cmd
	@rm -f .*.flags ieee80211/.*.flags rfkill/.*.flags crc-itu-t/.*.flags
	@rm -f .*.o.d ieee80211/.*.o.d rfkill/.*.o.d crc-itu-t/.*.o.d
	@rm -f .*.s.d ieee80211/.*.s.d rfkill/.*.s.d crc-itu-t/.*.s.d
	@rm -fr .tmp_versions ieee80211/.tmp_versions rfkill/.tmp_versions crc-itu-t/.tmp_versions
