#!/usr/bin/make
# Makefile for building Linux Broadcom Gigabit ethernet driver as a module.
# $id$

# PREFIX may be set by the RPM build to set the effective root.
PREFIX=
LINUX=/lib/modules/$(shell uname -r)/build
ifeq ($(shell ls /lib/modules/$(shell uname -r)/source > /dev/null 2>&1 && echo source),)
  LINUXSRC=$(LINUX)
else
  LINUXSRC=/lib/modules/$(shell uname -r)/source
endif

ifneq ($(shell grep netdump_mode $(LINUXSRC)/include/linux/kernel.h > /dev/null 2>&1 && echo rh),)
  NETDUMP_CFLAG = -DRED_HAT_LINUX_KERNEL
else
  NETDUMP_CFLAG =
endif

ifeq ($(shell grep netif_poll_disable $(LINUXSRC)/include/linux/netdevice.h > /dev/null 2>&1 && echo newnetif),)
  CFLAG+=-DOLD_NETIF
endif

ifeq ($(shell grep dma_sync_single_range $(LINUXSRC)/include/asm/dma-mapping.h > /dev/null 2>&1 && echo syncsingle),)
  CFLAGS+=-DOLD_DMA
endif

ifeq ($(shell grep kzalloc $(LINUXSRC)/include/linux/slab.h > /dev/null 2>&1 && echo kzalloc_slab),)
  CFLAGS+=-DOLD_MALLOC
endif

ifeq ($(shell grep dma_mapping_error $(LINUXSRC)/include/asm/dma-mapping.h > /dev/null 2>&1 && echo dmamaperr),)
ifeq ($(shell grep "asm-generic/dma-mapping.h" $(LINUXSRC)/include/asm/dma-mapping.h > /dev/null 2>&1 && echo dmamapr1),)
ifeq ($(shell grep dma_mapping_error $(LINUXSRC)/include/asm-generic/dma-mapping.h > /dev/null 2>&1 && echo dmamapr2),)
	  CFLAGS+=-DNO_DMA_MAPPING_ERROR
endif
endif
endif

# check if 2.4 kernel or 2.5+ kernel
BCM_KVER:=$(shell uname -r | cut -c1-3 | sed 's/2\.[56]/2\.6/')

ifeq ($(BCM_KVER), 2.6)
# Makefile for 2.5+ kernel

BCM_DRV = b44.ko

ifneq ($(KERNELRELEASE),)

obj-m += b44.o

EXTRA_CFLAGS = $(NETDUMP_CFLAG)

else

default:
	make -C $(LINUX) SUBDIRS=$(shell pwd) modules

endif

else # ifeq ($(BCM_KVER),2.6)
# Makefile for 2.4 kernel

BCM_DRV = b44.o

ifeq ($(LINUX),)
  $(error Linux kernel source tree not found)
endif

CC = gcc

CFLAGS=-DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -I$(LINUX)/include $(NETDUMP_CFLAG)

ifeq ($(shell grep netif_poll_disable $(LINUXSRC)/include/linux/netdevice.h > /dev/null 2>&1 && echo newnetif),)
  CFLAGS+=-DOLD_NETIF
endif

ifeq ($(shell grep dma_sync_single_range $(LINUXSRC)/include/asm/dma-mapping.h > /dev/null 2>&1 && echo syncsingle),)
  CFLAGS+=-DOLD_DMA
endif

ifeq ($(shell grep kzalloc $(LINUXSRC)/include/linux/slab.h > /dev/null 2>&1 && echo kzalloc_slab),)
  CFLAGS+=-DOLD_MALLOC
endif

ifeq ($(shell grep dma_mapping_error $(LINUXSRC)/include/asm/dma-mapping.h > /dev/null 2>&1 && echo dmamaperr),)
ifeq ($(shell grep "asm-generic/dma-mapping.h" $(LINUXSRC)/include/asm/dma-mapping.h > /dev/null 2>&1 && echo dmamapr1),)
ifeq ($(shell grep dma_mapping_error $(LINUXSRC)/include/asm-generic/dma-mapping.h > /dev/null 2>&1 && echo dmamapr2),)
	  CFLAGS+=-DNO_DMA_MAPPING_ERROR
endif
endif
endif


ifeq ($(wildcard ~/rpmdir),)
  rpmdir = /usr/src/redhat
else
  rpmdir = $(wildcard ~/rpmdir)
endif

ARCH:=$(shell uname -m)

ifeq ($(ARCH),x86_64)
  CFLAGS+=-mno-red-zone -mcmodel=kernel -pipe -finline-limit=2000
endif

ifeq ($(ARCH),ia64)
  CFLAGS+=-pipe -ffixed-r13 -mfixed-range=f10-f15,f32-f127 -falign-functions=32
endif

ifeq ($(ARCH),ppc64)
  ifneq ($(shell ls /opt/cross/bin/powerpc64-linux-gcc > /dev/null 2>&1 && echo ppcgcc),)
    CC=/opt/cross/bin/powerpc64-linux-gcc
  endif

  CFLAGS+=-fno-strict-aliasing -fno-common -fomit-frame-pointer -msoft-float -pipe -mminimal-toc -fno-builtin
endif

CFLAGS += -O2

all: b44.o

endif # ifeq ($(BCM_KVER),2.6)

b44.4.gz:
	gzip -c b44.4 > b44.4.gz


ifeq ($(BCM_KVER), 2.6)
install: default b44.4.gz
else
install: $(BCM_DRV) b44.4.gz
endif
	mkdir -p $(PREFIX)/lib/modules/$(shell uname -r)/kernel/drivers/net;
	install -m 444 $(BCM_DRV) $(PREFIX)/lib/modules/`uname -r`/kernel/drivers/net;
	@if [ "$(PREFIX)" = "" ]; then /sbin/depmod -a ;\
	else echo " *** Run '/sbin/depmod -a' to update the module database.";\
	fi
	install -m 444 b44.4.gz $(PREFIX)/usr/share/man/man4;\

.PHONEY: all clean install tar

clean:
	-rm -f b44.o b44.ko b44.mod.c b44.mod.o b44.4.gz

