#
# $Id: Makefile 44692 2019-02-21 03:47:58Z adrozdov $
#
# VGA2PCI driver
#
# Copyright (C) 2010-2011 Epiphan Systems Inc. All rights reserved
#

# Source directory
ifeq ($(src),)
src = $(shell pwd)
endif

# Kernel directory
ifeq ($(KDIR),)
KDIR = /lib/modules/$(shell uname -r)/build
endif

vga2pci-src = vga2pci_module.c
vga2pci-core = vga2pci_core.o
vga2pci-objs = $(vga2pci-src:.c=.o) $(vga2pci-core)
vga2pci-module = vga2pci.ko
obj-m = vga2pci.o

driver:
	make -C $(KDIR) M=$(src) modules

clean:
	@rm -fr .tmp_versions
	@rm -f Module.symvers Module.markers modules.order *~
	@rm -f $(vga2pci-src:.c=.o) $(vga2pci-module) $(obj-m)
	@rm -f vga2pci.mod.* .*.cmd

# Convenience targets

load:	driver
	sudo /sbin/modprobe -a `/sbin/modinfo $(vga2pci-module) | grep -i ^depends: | cut -f2 -d: | sed 'y/,/ /'`
	sudo /sbin/insmod $(vga2pci-module)
	@sleep 0.5
	sudo chmod 664   /dev/vga2usb* /dev/video* /dev/epiphanboard*
	sudo chgrp video /dev/vga2usb* /dev/video* /dev/epiphanboard*

unload:
	-sudo /sbin/rmmod vga2pci

reload: unload load

preinstall: driver
	$(MAKE) -C $(KDIR) SUBDIRS=$(src) modules_install
	[ -d "/lib/udev/rules.d" ] && install -m 0644 extra/40-epiphan.rules /lib/udev/rules.d/
	depmod -ae
	sudo install -o root -m 0755 extra/v2u_flash /usr/bin/

install: preinstall load
	@echo "Ugrading Grabber firmware..."
	@/usr/bin/v2u_flash list ; if [ $$? -ne 0 ] ; then \
		echo "No Grabber firmware update is required" ;\
		true ;\
	else \
		/usr/bin/v2u_flash -v ;\
		echo "Immediate soft reboot is required to finalize the upgrade!" ;\
		true ;\
	fi

install_nodepmod: driver
	$(MAKE) -C $(KDIR) SUBDIRS=$(src) modules_install
