|
| 1 | +ifndef TP_MODULES |
| 2 | +# This part runs as a normal, top-level Makefile: |
| 3 | +X:=$(shell false) |
| 4 | +KVER := $(shell uname -r) |
| 5 | +KBASE := /lib/modules/$(KVER) |
| 6 | +KSRC := $(KBASE)/source |
| 7 | +KBUILD := $(KBASE)/build |
| 8 | +MOD_DIR := $(KBASE)/kernel |
| 9 | +PWD := $(shell pwd) |
| 10 | +IDIR := include/linux |
| 11 | +TP_DIR := drivers/misc |
| 12 | +TP_MODULES := thinkpad_ec.o tp_smapi.o |
| 13 | +SHELL := /bin/bash |
| 14 | + |
| 15 | +ifeq ($(HDAPS),1) |
| 16 | +TP_MODULES += hdaps.o |
| 17 | +LOAD_HDAPS := insmod ./hdaps.ko |
| 18 | +else |
| 19 | +LOAD_HDAPS := : |
| 20 | +endif |
| 21 | + |
| 22 | +ifeq ($(FORCE_IO),1) |
| 23 | +THINKPAD_EC_PARAM := force_io=1 |
| 24 | +else |
| 25 | +THINKPAD_EC_PARAM := |
| 26 | +endif |
| 27 | + |
| 28 | +DEBUG := 0 |
| 29 | + |
| 30 | +ifneq ($(shell [ -f $(KBUILD)/include/linux/aio_abi.h ] && echo 1),1) |
| 31 | +$(warning Building tp_smapi requires Linux kernel 2.6.19 or newer, and matching kernel headers.) |
| 32 | +$(warning You may need to override the following Make variables:) |
| 33 | +$(warning . KVER=$(KVER)) |
| 34 | +$(warning . KBUILD=$(KBUILD)) |
| 35 | +$(warning . MOD_DIR=$(MOD_DIR)) |
| 36 | +$(warning For "make patch", you may also need the full kernel sources, and may need to override:) |
| 37 | +$(warning . KSRC=$(KSRC)) |
| 38 | +$(error Missing kernel headers) |
| 39 | +endif |
| 40 | + |
| 41 | +.PHONY: default clean modules load unload install patch check_hdaps mk-hdaps.diff |
| 42 | +export TP_MODULES |
| 43 | + |
| 44 | +##################################################################### |
| 45 | +# Main targets |
| 46 | + |
| 47 | +default: modules |
| 48 | + |
| 49 | +# Build the modules thinkpad_ec.ko, tp_smapi.ko and (if HDAPS=1) hdaps.ko |
| 50 | +modules: $(KBUILD) $(patsubst %.o,%.c,$(TP_MODULES)) |
| 51 | + $(MAKE) -C $(KBUILD) M=$(PWD) O=$(KBUILD) modules |
| 52 | + |
| 53 | +clean: |
| 54 | + rm -f tp_smapi.mod.* tp_smapi.o tp_smapi.ko .tp_smapi.*.cmd |
| 55 | + rm -f thinkpad_ec.mod.* thinkpad_ec.o thinkpad_ec.ko .thinkpad_ec.*.cmd |
| 56 | + rm -f hdaps.mod.* hdaps.o hdaps.ko .hdaps.*.cmd |
| 57 | + rm -f *~ diff/*~ *.orig diff/*.orig *.rej diff/*.rej |
| 58 | + rm -f tp_smapi-*-for-*.patch |
| 59 | + rm -fr .tmp_versions Modules.symvers diff/hdaps.diff.tmp |
| 60 | + |
| 61 | +load: check_hdaps unload modules |
| 62 | + @( [ `id -u` == 0 ] || { echo "Must be root to load modules"; exit 1; } ) |
| 63 | + { insmod ./thinkpad_ec.ko $(THINKPAD_EC_PARAM) && insmod ./tp_smapi.ko debug=$(DEBUG) && $(LOAD_HDAPS); }; : |
| 64 | + @echo -e '\nRecent dmesg output:' ; dmesg | tail -10 |
| 65 | + |
| 66 | +unload: |
| 67 | + @( [ `id -u` == 0 ] || { echo "Must be root to unload modules"; exit 1; } ) |
| 68 | + if lsmod | grep -q '^hdaps '; then rmmod hdaps; fi |
| 69 | + if lsmod | grep -q '^tp_smapi '; then rmmod tp_smapi; fi |
| 70 | + if lsmod | grep -q '^thinkpad_ec '; then rmmod thinkpad_ec; fi |
| 71 | + if lsmod | grep -q '^tp_base '; then rmmod tp_base; fi # old thinkpad_ec |
| 72 | + |
| 73 | +check_hdaps: |
| 74 | +ifneq ($(HDAPS),1) |
| 75 | + @if lsmod | grep -q '^hdaps '; then \ |
| 76 | + echo 'The hdaps driver is loaded. Use "make HDAPS=1 ..." to'\ |
| 77 | + 'patch hdaps for compatibility with tp_smapi.'\ |
| 78 | + 'This requires a kernel source tree.'; exit 1; fi |
| 79 | +endif |
| 80 | + |
| 81 | +install: modules |
| 82 | + @( [ `id -u` == 0 ] || { echo "Must be root to install modules"; exit 1; } ) |
| 83 | + rm -f $(MOD_DIR)/$(TP_DIR)/{thinkpad_ec,tp_smapi,tp_base}.ko |
| 84 | + rm -f $(MOD_DIR)/drivers/firmware/{thinkpad_ec,tp_smapi,tp_base}.ko |
| 85 | + rm -f $(MOD_DIR)/extra/{thinkpad_ec,tp_smapi,tp_base}.ko |
| 86 | +ifeq ($(HDAPS),1) |
| 87 | + rm -f $(MOD_DIR)/drivers/hwmon/hdaps.ko |
| 88 | + rm -f $(MOD_DIR)/extra/hdaps.ko |
| 89 | +endif |
| 90 | + $(MAKE) -C $(KBUILD) M=$(PWD) O=$(KBUILD) modules_install |
| 91 | + depmod -a |
| 92 | + |
| 93 | + |
| 94 | +##################################################################### |
| 95 | +# Generate a stand-alone kernel patch |
| 96 | + |
| 97 | +TP_VER := ${shell sed -ne 's/^\#define TP_VERSION \"\(.*\)\"/\1/gp' tp_smapi.c} |
| 98 | +ORG := a |
| 99 | +NEW := b |
| 100 | +PATCH := tp_smapi-$(TP_VER)-for-$(KVER).patch |
| 101 | + |
| 102 | +BASE_IN_PATCH := 1 |
| 103 | +SMAPI_IN_PATCH := 1 |
| 104 | +HDAPS_IN_PATCH := 1 |
| 105 | + |
| 106 | +patch: $(KSRC) |
| 107 | + @TMPDIR=`mktemp -d /tmp/tp_smapi-patch.XXXXXX` &&\ |
| 108 | + echo "Working directory: $$TMPDIR" &&\ |
| 109 | + cd $$TMPDIR &&\ |
| 110 | + mkdir -p $(ORG)/$(TP_DIR) &&\ |
| 111 | + mkdir -p $(ORG)/$(IDIR) &&\ |
| 112 | + mkdir -p $(ORG)/drivers/hwmon &&\ |
| 113 | + cp $(KSRC)/$(TP_DIR)/{Kconfig,Makefile} $(ORG)/$(TP_DIR) &&\ |
| 114 | + cp $(KSRC)/drivers/hwmon/{Kconfig,hdaps.c} $(ORG)/drivers/hwmon/ &&\ |
| 115 | + cp -r $(ORG) $(NEW) &&\ |
| 116 | + \ |
| 117 | + if [ "$(BASE_IN_PATCH)" == 1 ]; then \ |
| 118 | + cp $(PWD)/thinkpad_ec.c $(NEW)/$(TP_DIR)/thinkpad_ec.c &&\ |
| 119 | + cp $(PWD)/thinkpad_ec.h $(NEW)/$(IDIR)/thinkpad_ec.h &&\ |
| 120 | + perl -i -pe 'print `cat $(PWD)/diff/Kconfig-thinkpad_ec.add` if m/^(endmenu|endif # MISC_DEVICES)$$/' $(NEW)/$(TP_DIR)/Kconfig &&\ |
| 121 | + sed -i -e '$$aobj-$$(CONFIG_THINKPAD_EC) += thinkpad_ec.o' $(NEW)/$(TP_DIR)/Makefile \ |
| 122 | + ; fi &&\ |
| 123 | + \ |
| 124 | + if [ "$(HDAPS_IN_PATCH)" == 1 ]; then \ |
| 125 | + cp $(PWD)/hdaps.c $(NEW)/drivers/hwmon/ &&\ |
| 126 | + perl -i -0777 -pe 's/(config SENSORS_HDAPS\n\ttristate [^\n]+\n\tdepends [^\n]+\n)/$$1\tselect THINKPAD_EC\n/' $(NEW)/drivers/hwmon/Kconfig \ |
| 127 | + ; fi &&\ |
| 128 | + \ |
| 129 | + if [ "$(SMAPI_IN_PATCH)" == 1 ]; then \ |
| 130 | + sed -i -e '$$aobj-$$(CONFIG_TP_SMAPI) += tp_smapi.o' $(NEW)/$(TP_DIR)/Makefile &&\ |
| 131 | + perl -i -pe 'print `cat $(PWD)/diff/Kconfig-tp_smapi.add` if m/^(endmenu|endif # MISC_DEVICES)$$/' $(NEW)/$(TP_DIR)/Kconfig &&\ |
| 132 | + cp $(PWD)/tp_smapi.c $(NEW)/$(TP_DIR)/tp_smapi.c &&\ |
| 133 | + mkdir -p $(NEW)/Documentation &&\ |
| 134 | + perl -0777 -pe 's/\n(Installation\n---+|Conflict with HDAPS\n---+|Files in this package\n---+|Setting and getting CD-ROM speed:\n).*?\n(?=[^\n]*\n-----)/\n/gs' $(PWD)/README > $(NEW)/Documentation/tp_smapi.txt \ |
| 135 | + ; fi &&\ |
| 136 | + \ |
| 137 | + { diff -dNurp $(ORG) $(NEW) > patch \ |
| 138 | + || [ $$? -lt 2 ]; } &&\ |
| 139 | + { echo "Generated for $(KVER) in $(KSRC)"; echo; diffstat patch; echo; echo; cat patch; } \ |
| 140 | + > $(PWD)/${PATCH} &&\ |
| 141 | + rm -r $$TMPDIR |
| 142 | + @echo |
| 143 | + @diffstat ${PATCH} |
| 144 | + @echo -e "\nPatch file created:\n ${PATCH}" |
| 145 | + @echo -e "To apply, use:\n patch -p1 -d ${KSRC} < ${PATCH}" |
| 146 | + |
| 147 | +##################################################################### |
| 148 | +# Tools for preparing a release. Ignore these. |
| 149 | + |
| 150 | +set-version: |
| 151 | + perl -i -pe 's/^(tp_smapi version ).*/$${1}$(VER)/' README |
| 152 | + perl -i -pe 's/^(#define TP_VERSION ").*/$${1}$(VER)"/' thinkpad_ec.c tp_smapi.c |
| 153 | + |
| 154 | +TGZ=../tp_smapi-$(VER).tgz |
| 155 | +create-tgz: |
| 156 | + git-archive --format=tar --prefix=tp_smapi-$(VER)/ HEAD | gzip -c > $(TGZ) |
| 157 | + tar tzvf $(TGZ) |
| 158 | + echo "Ready: $(TGZ)" |
| 159 | + |
| 160 | +else |
| 161 | +##################################################################### |
| 162 | +# This part runs as a submake in kernel Makefile context: |
| 163 | + |
| 164 | +EXTRA_CFLAGS := $(CFLAGS) -I$(M)/include |
| 165 | +obj-m := $(TP_MODULES) |
| 166 | + |
| 167 | +endif |
0 commit comments