一个最简单的测试在dev/下生成字符设备的程序
2011-12-13 15:43
453 查看
先贴代码:
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h>
#include <linux/module.h> /* Needed for all modules */
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("ttxgz");
int hello_major = 555;
int hello_minor = 0;
int number_of_devices = 1;
int memalloc_major = 111;
int memalloc_minor = 1;
struct cdev cdev;
dev_t dev = 0;
struct file_operations hello_fops = {
.owner = THIS_MODULE
};
struct class *my_class;
struct class *mem_class;
static int __init hello_init(void)
{
int result;
dev = MKDEV (hello_major, hello_minor);
/* create your own class under /sysfs */
my_class = class_create(THIS_MODULE, "my_class");
if(IS_ERR(my_class))
{
printk("Err: failed in creating class.\n");
return -1;
}
device_create( my_class, NULL, MKDEV(hello_major, hello_minor), NULL, "hello%d",0);
mem_class = class_create(THIS_MODULE, "mem_class");
device_create( mem_class, NULL, MKDEV(memalloc_major, memalloc_minor), NULL, "memalloc");
printk(KERN_ALERT "Hello, world!/n");
return 0;
}
static void __exit hello_exit(void)
{
dev_t devno = MKDEV (hello_major, hello_minor);
device_destroy(my_class, MKDEV(hello_major, 0)); //delete device node under /dev
class_destroy(my_class); //delete class created by us
unregister_chrdev_region (devno, number_of_devices);
devno = MKDEV(memalloc_major,memalloc_minor);
device_destroy(mem_class, MKDEV(memalloc_major, memalloc_minor)); //delete device node under /dev
class_destroy(mem_class); //delete class created by us
printk (KERN_INFO "char driver cleaned up\n");
printk(KERN_ALERT "Goodbye, cruel world/n");
}
module_init(hello_init);
module_exit(hello_exit);
Makefile:
ifeq ($(KERNELRELEASE),)
# Assume the source tree is where the running kernel was built
# You should set KERNELDIR in the environment if it's elsewhere
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
# The current directory is passed to sub-makes as argument
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
else
# called from kernel build system: just declare what our modules are
obj-m := hello.o
endif
直接在虚拟机下挂载,可以看见在dev下出现了hello0和memalloc两个设备,printk的信息可以通过dmesg | tail查看
上面的代码部分源于http://www.kerneltravel.net/?p=70和http://www.embedu.org/Column/Column120.htm以及参考了http://www.360doc.com/content/11/0518/16/3038654_117697142.shtml
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h>
#include <linux/module.h> /* Needed for all modules */
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("ttxgz");
int hello_major = 555;
int hello_minor = 0;
int number_of_devices = 1;
int memalloc_major = 111;
int memalloc_minor = 1;
struct cdev cdev;
dev_t dev = 0;
struct file_operations hello_fops = {
.owner = THIS_MODULE
};
struct class *my_class;
struct class *mem_class;
static int __init hello_init(void)
{
int result;
dev = MKDEV (hello_major, hello_minor);
/* create your own class under /sysfs */
my_class = class_create(THIS_MODULE, "my_class");
if(IS_ERR(my_class))
{
printk("Err: failed in creating class.\n");
return -1;
}
device_create( my_class, NULL, MKDEV(hello_major, hello_minor), NULL, "hello%d",0);
mem_class = class_create(THIS_MODULE, "mem_class");
device_create( mem_class, NULL, MKDEV(memalloc_major, memalloc_minor), NULL, "memalloc");
printk(KERN_ALERT "Hello, world!/n");
return 0;
}
static void __exit hello_exit(void)
{
dev_t devno = MKDEV (hello_major, hello_minor);
device_destroy(my_class, MKDEV(hello_major, 0)); //delete device node under /dev
class_destroy(my_class); //delete class created by us
unregister_chrdev_region (devno, number_of_devices);
devno = MKDEV(memalloc_major,memalloc_minor);
device_destroy(mem_class, MKDEV(memalloc_major, memalloc_minor)); //delete device node under /dev
class_destroy(mem_class); //delete class created by us
printk (KERN_INFO "char driver cleaned up\n");
printk(KERN_ALERT "Goodbye, cruel world/n");
}
module_init(hello_init);
module_exit(hello_exit);
Makefile:
ifeq ($(KERNELRELEASE),)
# Assume the source tree is where the running kernel was built
# You should set KERNELDIR in the environment if it's elsewhere
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
# The current directory is passed to sub-makes as argument
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
else
# called from kernel build system: just declare what our modules are
obj-m := hello.o
endif
直接在虚拟机下挂载,可以看见在dev下出现了hello0和memalloc两个设备,printk的信息可以通过dmesg | tail查看
上面的代码部分源于http://www.kerneltravel.net/?p=70和http://www.embedu.org/Column/Column120.htm以及参考了http://www.360doc.com/content/11/0518/16/3038654_117697142.shtml
相关文章推荐
- 一个最简单的测试在/dev下生成字符设备的程序
- 一个简单的字符设备驱动程序和测试程序
- 简单字符设备驱动程序 以及 相应测试程序
- 编写一个程序,一行行地读取输入行,直至到达文件尾。算出每行输入行的长度,然后把最长的那行打印出来。为了简单起见,你可以假定所有的输入行均不超过1000个字符
- 一个将图片生成gif动画的最简单程序
- 一个简单的加减乘除自动生成小程序(JAVA)
- 一个简单的AVR测试程序
- 一个简单的测试计算机的位数的程序: 不用sizeof
- 一个简单的执行程序的GNU automake自动生成Makefile的方法及案例
- linux设备驱动第三篇:写一个简单的字符设备驱动
- 基于mina实现一个简单数据采集中间件的多客户端在线测试程序
- linux设备驱动第三篇:写一个简单的字符设备驱动
- 3ff0 一个简单的执行程序的GNU automake自动生成Makefile的方法及案例
- 【NanoPi Duo试用体验】+一个简单的字符设备驱动
- 一个Java实现的简单的多个客户端聊天程序(未测试)
- 字符设备驱动0:一个简单但完整的字符设备驱动程序
- 用java写一个简单的随机生成井字棋棋子的程序
- 一个简单的执行程序的GNU automake自动生成Makefile的方法及案例
- (5)一个简单字符设备驱动程序
- 编写一个程序,一行行地读取输入行,直至到达文件尾。算出每行输入行的长度,然后把最长的那行打印出来。为了简单起见,你可以假定所有的输入行均不超过1000个字符。