This commit is contained in:
gatieme
2016-08-14 17:01:07 +08:00
parent 72c274f4f9
commit 6ca07b6ce7
9 changed files with 1432 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
cmd_/home/gatieme/Work/GitHub/LDD-LinuxDeviceDrivers/study/driver/cma/cma.ko := aarch64-linux-gnu-ld -EL -r -T /usr/src/linux-headers-3.14.0-20150821.kylin.3.desktop/scripts/module-common.lds --build-id -o /home/gatieme/Work/GitHub/LDD-LinuxDeviceDrivers/study/driver/cma/cma.ko /home/gatieme/Work/GitHub/LDD-LinuxDeviceDrivers/study/driver/cma/cma.o /home/gatieme/Work/GitHub/LDD-LinuxDeviceDrivers/study/driver/cma/cma.mod.o
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+2
View File
@@ -0,0 +1,2 @@
/home/gatieme/Work/GitHub/LDD-LinuxDeviceDrivers/study/driver/cma/cma.ko
/home/gatieme/Work/GitHub/LDD-LinuxDeviceDrivers/study/driver/cma/cma.o
+80
View File
@@ -0,0 +1,80 @@
# ------------------------------------------------------------------------------
#
# 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.
#
# ------------------------------------------------------------------------------
ROOT=../../../../../
#PLATFORM=$(shell $(ROOT)/systype.sh)
#include $(ROOT)/Make.defines.$(PLATFORM)
# my driver description
DRIVER_VERSION := "1.0.0"
DRIVER_AUTHOR := "Gatieme @ AderStep Inc..."
DRIVER_DESC := "Demo module for LDD-LinuxDeviceDrivers devices"
DRIVER_LICENSE := "Dual BSD/GPL"
DEV_FILE := /dev/cmd_test
MODULE_NAME := cma
ifneq ($(KERNELRELEASE),)
#CFG_FLAGS += -O2 -I./
#EXTRA_CFLAGS += $(C_FLAGS) $(CFG_INC) $(CFG_INC)
obj-m := $(MODULE_NAME).o #smodule.o
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
make -C $(KERNELDIR) M=$(PWD) modules
modules_install:
make -C $(KERNELDIR) M=$(PWD) modules_install
insmod:
sudo insmod $(MODULE_NAME).ko
#sudo mknod $(DEV_FILE) c 224 0
reinsmod:
sudo rmmod $(MODULE_NAME)
sudo insmod $(MODULE_NAME).ko
rmmod:
sudo rmmod $(MODULE_NAME)
sudo /bin/rm $(DEV_FILE)
clean:
make -C $(KERNELDIR) M=$(PWD) clean
rm -f modules.order Module.symvers Module.markers
COMMIT="[1.1.4 带参数的可加载模块](http://book.51cto.com/art/201205/337663.htm)"
github:
cd $(ROOT) && make github
.PHNOY:
modules modules_install clean
endif
View File
+106
View File
@@ -0,0 +1,106 @@
/*
* kernel module helper for testing CMA
*
* Licensed under GPLv2 or later.
*/
#include <linux/module.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/dma-mapping.h>
#define CMA_NUM 10
static struct device *cma_dev;
static dma_addr_t dma_phys[CMA_NUM];
static void *dma_virt[CMA_NUM];
/* any read request will free coherent memory, eg.
* cat /dev/cma_test
*/
static ssize_t
cma_test_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
int i;
for (i = 0; i < CMA_NUM; i++) {
if (dma_virt[i]) {
dma_free_coherent(cma_dev, (i + 1) * SZ_1M, dma_virt[i], dma_phys[i]);
_dev_info(cma_dev, "free virt: %p phys: %p\n", dma_virt[i], (void *)dma_phys[i]);
dma_virt[i] = NULL;
break;
}
}
return 0;
}
/*
* any write request will alloc coherent memory, eg.
* echo 0 > /dev/cma_test
*/
static ssize_t
cma_test_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
int i;
int ret;
for (i = 0; i < CMA_NUM; i++) {
if (!dma_virt[i]) {
dma_virt[i] = dma_alloc_coherent(cma_dev, (i + 1) * SZ_1M, &dma_phys[i], GFP_KERNEL);
if (dma_virt[i]) {
void *p;
/* touch every page in the allocated memory */
for (p = dma_virt[i]; p < dma_virt[i] + (i + 1) * SZ_1M; p += PAGE_SIZE)
*(u32 *)p = 0;
_dev_info(cma_dev, "alloc virt: %p phys: %p\n", dma_virt[i], (void *)dma_phys[i]);
} else {
dev_err(cma_dev, "no mem in CMA area\n");
ret = -ENOMEM;
}
break;
}
}
return count;
}
static const struct file_operations cma_test_fops = {
.owner = THIS_MODULE,
.read = cma_test_read,
.write = cma_test_write,
};
static struct miscdevice cma_test_misc = {
.name = "cma_test",
.fops = &cma_test_fops,
};
static int __init cma_test_init(void)
{
int ret = 0;
ret = misc_register(&cma_test_misc);
if (unlikely(ret)) {
pr_err("failed to register cma test misc device!\n");
return ret;
}
cma_dev = cma_test_misc.this_device;
cma_dev->coherent_dma_mask = ~0;
_dev_info(cma_dev, "registered.\n");
return ret;
}
module_init(cma_test_init);
static void __exit cma_test_exit(void)
{
misc_deregister(&cma_test_misc);
}
module_exit(cma_test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
MODULE_DESCRIPTION("kernel module to help the test of CMA");
MODULE_ALIAS("CMA test");
+35
View File
@@ -0,0 +1,35 @@
#include <linux/module.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>
MODULE_INFO(vermagic, VERMAGIC_STRING);
__visible struct module __this_module
__attribute__((section(".gnu.linkonce.this_module"))) = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};
static const struct modversion_info ____versions[]
__used
__attribute__((section("__versions"))) = {
{ 0x146be775, __VMLINUX_SYMBOL_STR(module_layout) },
{ 0x15625c2c, __VMLINUX_SYMBOL_STR(misc_deregister) },
{ 0x27e1a049, __VMLINUX_SYMBOL_STR(printk) },
{ 0x4bc5f446, __VMLINUX_SYMBOL_STR(misc_register) },
{ 0x92c17db0, __VMLINUX_SYMBOL_STR(dma_release_from_coherent) },
{ 0xf19146ad, __VMLINUX_SYMBOL_STR(dev_err) },
{ 0xd6b7c3a5, __VMLINUX_SYMBOL_STR(dma_ops) },
{ 0x9134613b, __VMLINUX_SYMBOL_STR(_dev_info) },
{ 0xc31e44bc, __VMLINUX_SYMBOL_STR(dma_alloc_from_coherent) },
};
static const char __module_depends[]
__used
__attribute__((section(".modinfo"))) =
"depends=";
+1
View File
@@ -0,0 +1 @@
kernel//home/gatieme/Work/GitHub/LDD-LinuxDeviceDrivers/study/driver/cma/cma.ko