From 426c0a829d21616c810ac4a3612fd17eb3a2cf38 Mon Sep 17 00:00:00 2001 From: gatieme Date: Wed, 10 May 2017 22:14:50 +0800 Subject: [PATCH] =?UTF-8?q?=20init=5FMUTEX=E8=A2=AB=E5=BA=9F=E9=99=A4(?= =?UTF-8?q?=E8=A7=A3=E5=86=B3rror:=20implicit=20declaration=20of=20functio?= =?UTF-8?q?n=20=E2=80=98init=5FMUTEX=E2=80=99)--http://blog.csdn.net/gatie?= =?UTF-8?q?me/article/details/71598127?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../07/含并发控制的globalmem驱动/Makefile | 75 +++++++++++++++++ .../globalmem_lock.c | 25 +++++- study/driver/00-code/hello/Makefile | 15 ++-- study/problem/port/init_MUTEX/README.md | 83 +++++++++++++++++++ 4 files changed, 186 insertions(+), 12 deletions(-) create mode 100644 books/21cnbao/code/07/含并发控制的globalmem驱动/Makefile create mode 100644 study/problem/port/init_MUTEX/README.md diff --git a/books/21cnbao/code/07/含并发控制的globalmem驱动/Makefile b/books/21cnbao/code/07/含并发控制的globalmem驱动/Makefile new file mode 100644 index 0000000..dfdf4cb --- /dev/null +++ b/books/21cnbao/code/07/含并发控制的globalmem驱动/Makefile @@ -0,0 +1,75 @@ +# ------------------------------------------------------------------------------ +# +# Makefile for the LDD-LinuxDeviceDrivers. +# +# Author: gatieme +# Create: 2016-07-29 15:50:46 +# Last modified: 2016-07-29 16:10:29 +# Description: +# This program is loaded as a kernel(v2.6.18 or later) module. +# Use "make install" to load it into kernel. +# Use "make remove" to remove the module out of kernel. +# +# ------------------------------------------------------------------------------ + + +# my driver description +DRIVER_VERSION := "1.0.0" +DRIVER_AUTHOR := "Gatieme @ AderStep Inc..." +DRIVER_DESC := "Linux input module for Elo MultiTouch(MT) devices" +DRIVER_LICENSE := "Dual BSD/GPL" + + +MODULE_NAME := globalmem_lock + + + +ifneq ($(KERNELRELEASE),) # kernelspace + +obj-m := $(MODULE_NAME).o + +else # userspace + + +LINUX_KERNEL ?= $(shell uname -r) +LINUX_KERNEL_PATH ?= /lib/modules/$(LINUX_KERNEL)/build + +CURRENT_PATH ?= $(shell pwd) +CFG_INC = $(CURRENT_PATH) +MODCFLAGS:=-O2 -Wall -DMODULE -D__KERNEL__ -DLINUX -std=c99 +EXTRA_CFLAGS += $(MODULE_FLAGS) -I $(CFG_INC) + + +modules: + make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules + +modules_install: + make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules_install + +insmod: + sudo insmod $(MODULE_NAME).ko + +reinsmod: + sudo rmmod $(MODULE_NAME) + sudo insmod $(MODULE_NAME).ko + +github: + cd $(ROOT) && make github + +rmmod: + sudo rmmod $(MODULE_NAME) + +test : + sudo ../injector/memInjector -l stack -m random -t word_0 --time 1 --timeout 3 -p 1 + +clean: + make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean + rm -f modules.order Module.symvers Module.markers + +.PHNOY: + modules modules_install clean + + + +endif + diff --git a/books/21cnbao/code/07/含并发控制的globalmem驱动/globalmem_lock.c b/books/21cnbao/code/07/含并发控制的globalmem驱动/globalmem_lock.c index 713dfb4..48780e1 100644 --- a/books/21cnbao/code/07/含并发控制的globalmem驱动/globalmem_lock.c +++ b/books/21cnbao/code/07/含并发控制的globalmem驱动/globalmem_lock.c @@ -13,15 +13,18 @@ #include #include #include +#include +#include + #include -#include +//#include #include #define GLOBALMEM_SIZE 0x1000 /*ȫڴ4Kֽ*/ #define MEM_CLEAR 0x1 /*0ȫڴ*/ #define GLOBALMEM_MAJOR 254 /*Ԥglobalmem豸*/ -static globalmem_major = GLOBALMEM_MAJOR; +static int globalmem_major = GLOBALMEM_MAJOR; /* globalmem豸ṹ */ struct globalmem_dev @@ -48,6 +51,7 @@ int globalmem_release(struct inode *inode, struct file *filp) return 0; } +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36) /* ioctl豸ƺ */ static int globalmem_ioctl( struct inode *inodep, @@ -55,6 +59,17 @@ static int globalmem_ioctl( unsigned int cmd, unsigned long arg) { +#else +//long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); +//long (*compat_ioctl) (struct file *file, unsigned int cmd, unsigned long arg) +static long globalmem_unlocked_ioctl( + struct file *filp, + unsigned int cmd, + unsigned long arg) +{ + struct inode *inode = inode = file_inode(filp); + +#endif struct globalmem_dev *dev = filp->private_data; /*豸ṹָ*/ switch (cmd) @@ -206,7 +221,11 @@ static const struct file_operations globalmem_fops = .llseek = globalmem_llseek, .read = globalmem_read, .write = globalmem_write, +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36) .ioctl = globalmem_ioctl, +#else + .unlocked_ioctl = globalmem_unlocked_ioctl, +#endif .open = globalmem_open, .release = globalmem_release, }; @@ -251,7 +270,9 @@ int globalmem_init(void) memset(globalmem_devp, 0, sizeof(struct globalmem_dev)); globalmem_setup_cdev(globalmem_devp, 0); +//2.6.25 init_MUTEX(&globalfifo_devp->sem); /*ʼź*/ + //init_MUTEX(&globalfifo_devp->sem); /*ʼź*/ return 0; fail_malloc: unregister_chrdev_region(devno, 1); diff --git a/study/driver/00-code/hello/Makefile b/study/driver/00-code/hello/Makefile index dd6b061..180084b 100644 --- a/study/driver/00-code/hello/Makefile +++ b/study/driver/00-code/hello/Makefile @@ -13,10 +13,6 @@ # ------------------------------------------------------------------------------ -ROOT=.. -#PLATFORM=$(shell $(ROOT)/systype.sh) -#include $(ROOT)/Make.defines.$(PLATFORM) - # my driver description DRIVER_VERSION := "1.0.0" DRIVER_AUTHOR := "Gatieme @ AderStep Inc..." @@ -25,8 +21,6 @@ DRIVER_LICENSE := "Dual BSD/GPL" MODULE_NAME := hello -#MODCFLAGS:=-O2 -Wall -DMODULE -D__KERNEL__ -DLINUX -std=c99 -#EXTRA_CFLAGS += $(MODULE_FLAGS) $(CFG_INC) $(CFG_INC) @@ -37,11 +31,14 @@ obj-m := $(MODULE_NAME).o else # userspace -CURRENT_PATH ?= $(shell pwd) LINUX_KERNEL ?= $(shell uname -r) LINUX_KERNEL_PATH ?= /lib/modules/$(LINUX_KERNEL)/build -CURRENT_PATH := $(shell pwd) +CURRENT_PATH ?= $(shell pwd) +CFG_INC = $(CURRENT_PATH) +MODCFLAGS:=-O2 -Wall -DMODULE -D__KERNEL__ -DLINUX -std=c99 +EXTRA_CFLAGS += $(MODULE_FLAGS) -I $(CFG_INC) + modules: make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules @@ -49,8 +46,6 @@ modules: modules_install: make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules_install - - insmod: sudo insmod $(MODULE_NAME).ko diff --git a/study/problem/port/init_MUTEX/README.md b/study/problem/port/init_MUTEX/README.md new file mode 100644 index 0000000..92783f6 --- /dev/null +++ b/study/problem/port/init_MUTEX/README.md @@ -0,0 +1,83 @@ +init_MUTEX被废除(解决rror: implicit declaration of function ‘init_MUTEX’) +======= + +| CSDN | GitHub | +|:----:|:------:| +| [init_MUTEX被废除
解决rror: implicit declaration of function ‘init_MUTEX’](http://blog.csdn.net/gatieme/article/details/71598127) | [`LDD/problem/port/init_MUTEX`](https://github.com/gatieme/LDD-LinuxDeviceDrivers/tree/master/study/problem/port/init_MUTEX) | + + +
+知识共享许可协议 +本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可, 转载请注明出处 +
+ + + +#1 问题 +------- + + +近期在移植驱动的时候, 提示了如下错误 + +>error: implicit declaration of function ‘init_MUTEX’ [-Werror=implicit-function-declaration] + + +#2 原因分析 +------- + +在 `2.6.37` 之后的 `Linux` 内核中, `init_mutex` 已经被废除了, 新版本使用 `sema_init` 函数 + + + +查了一下早期版本的定义, 参见[include/linux/semaphore.h, version 2.6.36.4, line 42](http://elixir.free-electrons.com/linux/v2.6.36.4/source/include/linux/semaphore.h#L42) + +```cpp +static inline void sema_init(struct semaphore *sem, int val) +{ + static struct lock_class_key __key; + *sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val); + lockdep_init_map(&sem->lock.dep_map, "semaphore->lock", &__key, 0); +} + +#define init_MUTEX(sem) sema_init(sem, 1) +#define init_MUTEX_LOCKED(sem) sema_init(sem, 0) +``` + + + + +#3 解决方案 +------- + +修改 `init_MUTEX` 为 `sema_init`即可, 也可以在驱动中定义 `init_MUTEX` + +* 修改 `init_MUTEX` 为 `sema_init` + + +```cpp +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36) && !defined(init_MUTEX) + sema_init(&sem); +#else + init_MUTEX(&sem); +#endif +``` + +* 定义 `init_MUTEX` 为 `sema_init` + +```cpp +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36) && !defined(init_MUTEX) +#define init_MUTEX(sem) sema_init(sem, 1) +#endif +``` + +其实早期的内核中, 定义了 `sema_init`, 因此其实可以不需要添加 `#if #endif` 宏, 直接修改 `init_MUTEX` 为 `sema_init` 是没有什么问题的. + +
+ +* 本作品/博文 ( [AderStep-紫夜阑珊-青伶巷草 Copyright ©2013-2017](http://blog.csdn.net/gatieme) ), 由 [成坚(gatieme)](http://blog.csdn.net/gatieme) 创作, + +* 采用知识共享许可协议知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可. 欢迎转载、使用、重新发布, 但务必保留文章署名[成坚gatieme](http://blog.csdn.net/gatieme) ( 包含链接: http://blog.csdn.net/gatieme/article/details/71598127 ), 不得用于商业目的, + +* 基于本文修改后的作品务必以相同的许可发布. 如有任何疑问,请与我联系. + +