#
#	$Id: Makefile,v 2.1.0.1 1995/04/11 14:46:40 mummert Exp mummert $
#
#	Makefile 
#	========
#
#	for the EXPERIMENTAL Linux MultiSound driver REPLACING official
#       sound driver `VoxWare' from Hannu Savolainen.
#	USE Makefile.sysv instead on SysV386.
#       Author: Markus Mummert <mum@mmk.e-technik.tu-muenchen.de>
#
#
#	Relation to VoxWare
#	-------------------
#	Concerning the Driver-Installation-Interface we take on
#	an appearance as if we were VoxWare itself. We have
#	to reside in drivers/sound instead of the VoxWare code.
#	The package should largely behave as VoxWare during a
#	Linux kernel configuration procedure. That is, we have
#	`make config' and a 'make (all)' delivering the library
#	`sound.a' to be linked to the kernel. The `make config'
#	is still partly an request to edit the following top
#	section of this makefile.
#
#	If we wanted to coexist with VoxWare without being
#	integrated we'd have to go for a different major number,
#	get (a) differently named hook(s) into patched into the
#	kernel code and, of course, exist in our own directory,
#	e.g. `drivers/msnd'.
#	(futher info in kiflinux.c)
#
#	
#	Targets of interest
#	-------------------
#	make config	configuration (autodetects CPU speed
#			just issues request to edit this file)
#	make (all)	make library sound.a to take VoxWare's
#			place (implies `config' and `msndctl')
#	make msndctl	make ioctl-utility executables msndctl,play,rec
#	make node	create Device-file (/dev/msnd)
#	make dist	pack DISTFILES up to send (tar, gzip)
#			see what VERSION gives you as a name tag
#	make uuencode	make dist and uu-encode it into file `uu'
#			usefull to comunicate between developers
#


#
# CHECK THE FOLLOWING SETTINGS ...
# --------------------------------
#
#    Hardware:
#    --------
#
# 	I/O-base in hex as determined by DIP-switches on MultiSound-card
#	If they don't match the driver can not access the hardware!
# 	(3e0, 290, 260, 250, 240, 230, 220, 210 - no leading `0x'!)
#
MSNDSIOA = 290

#	MEM-base in hex to be configured
#	(b0000, c8000, d0000, d4000, d8000, e0000, e8000 - no leading '0x'!)
#
MSNDSCMA = e0000

#	IRQ-number to be configured
#	(5, 7, 9, 10, 11, 12, 15)
#
MSNDVECT = 9

#	VoxWare-Driver Major Number as defined in <linux/major.h>
#	MultiSound device minor number and device file
#	(to be changed conforming to /dev/dsp)
#
MSNDMAJOR = 14
MSNDMINOR = 0
MSNDDEVICE = /dev/msnd

#
#    Software:
#    ---------
#

#	comment this out if compiler aborts on request_irq() (see kiflinux.c)
#	older kernel versions (e.g. 1.0.9) don't want this
#
KERNEL_VERSION=-DNEW_LINUX_KERNEL

#	driver compiler
#
KERNELCC = cc
KERNELCFLAGS = -D__KERNEL__=1 -O2 -fomit-frame-pointer -I../../include

#	Compiler for utility program(s)
#
CC = cc
CFLAGS = -O2 -I../../include

#
# ... NOTHING TO BE CHANGED BELOW, PLEASE CONTINUE TO CHECK Space.c
# -----------------------------------------------------------------

SHELL = bash	# /bin/bash
# VERSION =  `date +'%y%m%d'`
VERSION = `echo $$Revision: 2.1.0.1 $$Revision | sed 's/: //g'`
MSNDDIR = drivers/sound
OBJS   =  Driver.o kiflinux.o Space.o
MSNDLPSMS = "`cut -d= -f2 .lpsms`"
HWCONF = -DMSND_0_VECT=$(MSNDVECT) \
	 -DMSND_0_SIOA=0x$(MSNDSIOA) \
	 -DMSND_0_SCMA=0x$(MSNDSCMA) \
	 -DMSND_MAJOR=$(MSNDMAJOR) \
	 -DMSND_LPSMS=$(MSNDLPSMS) $(KERNEL_VERSION)

all: config sound.a msndctl rec play 
.PHONEY: all

.c.o:
	$(KERNELCC) $(KERNELCFLAGS) $(HWCONF) -c $<

sound.a: $(OBJS) 
	-rm -f sound.a
	$(AR) rcs sound.a $(OBJS)
	sync

node:
	echo "--- Creating device file $(MSNDDEVICE) ---" 
	@if [ -e $(MSNDDEVICE) ]; then ( \
		rm -f $(MSNDDEVICE) \
	); fi
	mknod -m 666 $(MSNDDEVICE) c $(MSNDMAJOR) $(MSNDMINOR) 
	@echo "    Maybe you want to link $(MSNDDEVICE) to /dev/dsp, but"
	@echo "    remember, we don't have VoxWare compatible ioctl()-calls."
.PHONEY: node

$(OBJS): Makefile osdep.h
config: .lpsms $(MSNDDEVICE) /usr/include/sys/msndio.h
	@echo "--- WARNING!"
	@echo "    0.) If this is not on Linux, better try Makefile.sysv ..."
	@echo "    1.) This is EXPERIMENTAL software"
	@echo "    2.) Did you EDIT settings in $(MSNDDIR)/Makefile?"
	@echo "        If not do so and make again!"
	@echo "    3.) Did you a \`make config' for the whole kernel already?"
	@echo "        (This is only applies, if you haven't configuered for"
	@echo "        sound yet) If not do so and make again!"
	@echo "    (yes, this looks like a catch 22 situation)"
	sleep 3

.PHONEY: config

/usr/include/sys/msndio.h:
	@echo "--- Note: /usr/include/sys/msndio.h nonexistent."
	@echo "    Maybe later you want to create /usr/include/sys/msndio.h"
	@echo "    containing just the line #include <linux/msndio.h>"
	@echo "    and link $(MSNDDIR)/msndio.h to include/linux/msndio.h"

$(MSNDDEVICE):
	@echo "--- Note: device file $(MSNDDEVICE) nonexistent."
	@echo "    Just type \`make node' in $(MSNDDIR)"

.lpsms: forloops.c
	@echo "--- Determining paramter m_lpsms needed by Space.c ..."; 
	@echo "    (this may take up to a minute)"; 
	$(KERNELCC) $(KERNELCFLAGS) forloops.c -o forloops
	( time forloops 100000000 ) 2>&1 | sed -n '/us/p' | \
	    awk '{ printf "--- m_lpsms = %d\n", 100000/$$1 }' | tee .lpsms
#
# ON BASH USE $1 instead of $2 !
#
	@echo "--- Please check the result above for sanity. It should be"
	@echo "    around 6000 +/- 50percent for an 486DX33 if compiled"
	@echo "    with -O, increasing with CPU-performance. If this looks" 
	@echo "    strange your time command may have an unexpected format"
	@echo "    or your compiler yields unexpected results."
.PHONEY: lpsms

Driver.c: bin2init msndpp11.ini msndpr11.ini mdiag.ini msnd.c msndio.h
	@echo "--- Patching Turtle Beach Systems' DSP-Code into source ..."; 
	@echo "/************************************************/" >Driver.c 
	@echo "/* GENERATED AUTOMATICALLY BY MAKE - DON'T EDIT */" >>Driver.c 
	@echo "/************************************************/" >>Driver.c 
	@sed    -e 's/\$$Revision: /V/1' \
		-e 's/\$$//1' \
		-e '/^### msndpp11.bin .*###$$/r msndpp11.ini' \
		-e '/^### msndpp11.bin .*###$$/d' \
		\
		-e '/^### msndpr11.bin .*###$$/r msndpr11.ini' \
		-e '/^### msndpr11.bin .*###$$/d' \
		\
		-e '/^### mdiag.bin .*###$$/r mdiag.ini' \
		-e '/^### mdiag.bin .*###$$/d' \
	<msnd.c >>Driver.c
	@echo "--- OK ---"; 
	@echo "";

msndctl rec play: msndctl.c msndio.h Makefile
	@echo "--- Making utility programs ..."; 
	rm -f msndctl rec play
	$(CC) $(CFLAGS) '-DMULTISOUNDDEVICE="$(MSNDDEVICE)"' \
		msndctl.c -o msndctl
	-strip msndctl
	ln msndctl rec
	ln msndctl play
	@echo "--- OK ---"; 
	@echo "";

bin2init: bin2init.c
	@echo "--- Making patcher ..."; 
	$(CC) $(CFLAGS) bin2init.c -o bin2init

.SUFFIXES: .ini .bin
.bin.ini:
	@echo "--- Generate patch file from Turtle Beach Systems' DSP-Code $*.bin ..."; 
	./bin2init <$*.bin >$*.ini

msndpp11.bin msndpr11.bin mdiag.bin:
	@if [ ! -f $*.bin ]; then ( \
		echo "--- COULD NOT FIND DSP-PGM ./$*.bin - SEE FILE README ---"; \
		exit 1; \
	); fi
.PHONEY: msndpp11.bin msndpr11.bin mdiag.bin

clean:
	@echo "--- Forcing cleanup  ..."; 
	rm -f *.o Driver.? System Node Master bin2init forloops msndctl rec play core *.ini .lpsms sound.a
	@echo "--- done ---"
.PHONEY: clean

dep:
# to be worked on later ...
#	$(CPP) -M *.c > .depend
.PHONEY: clean


#
# only distribuition-administration stuff below ...
# -------------------------------------------------

DISTFILES = TODO HISTORY README COPYING INSTALL Makefile.sysv Makefile Space.c kiflinux.c osdep.h bin2init.c forloops.c msnd.c msndctl.c msndio.h msndctl.man msnd.man
DISTDIR = msnd-$(VERSION)
TARFILE = msnd-$(VERSION).tz
TARCMD = tar cvhzf
ALTTARCMD = gtar cvhzf

dist: $(DISTFILES) distclean 
	@( id | grep '(mummert)' >/dev/null ) || ( echo; echo "--- YOU ARE NOT mummert - PLEASE CHECK FILE COPYING ---"; echo && exit 0 )
	mkdir $(DISTDIR)
	-@( echo $(DISTFILES) | grep -v '[.]bin' >/dev/null ) || ( echo; echo "---  REDISTRIBUTING COPYRIGHTED" *bin "---"; echo && exit 1 )
	ln $(DISTFILES) $(DISTDIR)/.
	( echo $(DISTFILES) | grep -v 'msndctl.man' >/dev/null ) || nroff -man msndctl.man >$(DISTDIR)/msndctl.1
	( echo $(DISTFILES) | grep -v 'msnd.man' >/dev/null ) || nroff -man msnd.man >$(DISTDIR)/msnd.7
	( cd $(DISTDIR); gls -l > MANIFEST ) || ( cd $(DISTDIR); ls -l > MANIFEST )
	echo
	($(TARCMD) $(TARFILE) $(DISTDIR) || $(ALTTARCMD) $(TARFILE) $(DISTDIR))
	rm -rf  $(DISTDIR)
.PHONEY: dist

distclean: 
	rm -rf $(DISTDIR) $(TARFILE) uu
.PHONEY: distclean

realclean: distclean clean
	rm -f *.bin 
.PHONEY: realclean

uuencode: dist
	uuencode $(TARFILE) $(TARFILE) >uu
.PHONEY: uuencode

#
# EOT
#

