#-----------------------------------------------------------------------------
# 	Name     : Makefile #	Function : Makefile for the Linux USB device driver
#
#   Redistribution and use in source and binary forms, with or without
#   modification, are permitted provided that the following conditions
#   are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in
#       the documentation and/or other materials provided with the
#       distribution.
#     * Neither the name of Sony Corporation nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#-----------------------------------------------------------------------------

KVER  := $(shell uname -r)
KSRC := /lib/modules/$(KVER)/build
KMISC := /lib/modules/$(KVER)/kernel/drivers/usb/serial

LKVER=$(shell uname -r|sed 's/-.*$$//g')
CKSRC=/usr/src/redhat/BUILD/kernel-$(LKVER)

DEPMOD=: /sbin/depmod

list-m :=
list-m += sonyrw

obj-m += sonyrw.o
sonyrw-objs := sonyrw-serial.o
usbserial-objs := usbserial.o

all: modules

clean:
	rm -f *.mod.c *.mod *.o *.ko .*.cmd *.list Module.*
	rm -rf .tmp_versions

modules:
	@if [ ! -d $(CKSRC) ]; then \
		echo 'If compile was fail, it might be necessary to install kernel source !!'; \
		echo 'And set CKSRC parameter in Makefile !!' && echo; \
		echo 'make -C $(KSRC) M=$(PWD) modules'; \
		make -C $(KSRC) M=$(PWD) modules; \
	else \
		if [ -d $(CKSRC)/linux-$(LKVER) ]; then \
			extra=`echo '-I$(CKSRC)/linux-$(LKVER)/drivers'`; \
			echo "make -C $(KSRC) M=$(PWD) \""EXTRA_CFLAGS=$(EXTRA_CFLAGS) $$extra"\" modules"; \
			make -C $(KSRC) M=$(PWD) "EXTRA_CFLAGS=$(EXTRA_CFLAGS) $$extra" modules; \
		elif [ -d $(CKSRC)/linux-$(LKVER).$(shell uname -m) ]; then \
			extra=`echo '-I$(CKSRC)/linux-$(LKVER).$(shell uname -m)/drivers'`; \
			echo "make -C $(KSRC) M=$(PWD) \""EXTRA_CFLAGS=$(EXTRA_CFLAGS) $$extra"\" modules"; \
			make -C $(KSRC) M=$(PWD) "EXTRA_CFLAGS=$(EXTRA_CFLAGS) $$extra" modules; \
		else \
			echo 'If compile was fail, it might be necessary to install kernel source !!'; \
			echo 'Could not find kernel source directory, $(CKSRC)/linux-$(LKVER)'; \
			echo 'and $(CKSRC)/linux-$(LKVER).$(shell uname -m) !!' && echo; \
			echo 'make -C $(KSRC) M=$(PWD) modules'; \
			make -C $(KSRC) M=$(PWD) modules; \
		fi; \
	fi;
	cp sonyrw.ko ../kobj
	cp install-sonyrw ../kobj


install: modules
	install -d $(KMISC);
	install -m 744 -c $(addsuffix .ko,$(list-m)) $(KMISC).
	@echo 'call ./install-sonyrw'

uninstall:
	@if [ -n "$(shell /sbin/lsmod |grep $(list-m))" ]; then \
		echo '/sbin/rmmod $(list-m)'; \
		/sbin/rmmod $(list-m); \
	fi;
	rm -rf $(KMISC)/$(addsuffix .ko,$(list-m));
	: /sbin/depmod -a;

