/*driver test code */
#include
#include
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");
static int char_read(struct file *filp, char __user *buffer, size_t, loff_t*);
static int char_open(struct inode*, struct file*);
static int char_write(struct file *filp, const char __user *buffer, size_t, loff_t*);
static int char_release(struct inode*, struct file*);
static int chropen;
struct cdev *chardev;
struct class *my_class;
static int len;
static dev_t dev;
static char *to;
static const struct file_operations char_ops={
.read = char_read,
.write = char_write,
.open = char_open,
.release = char_release,
};
static int __init char_init(void)
{
printk(KERN_ALERT"Initing......\n");
chardev = cdev_alloc();
if(chardev == NULL){
return -1;
}
if(alloc_chrdev_region(&dev, 0, 10, "xmimx")) /*动态分配设备号*/
{
printk(KERN_ALERT"Register char dev error\n");
return -1;
}
chropen = 0;
len = 0;
cdev_init(chardev, &char_ops);
if(cdev_add(chardev, dev, 1)){
printk(KERN_ALERT"Add char dev error!\n");
}
my_class = class_create(THIS_MODULE, "xmimx_class"); /*创建一个类*/
if(IS_ERR(my_class)) {
printk("Err: failed in creating class.\n");
return -1;
}
device_create(my_class, NULL, dev, NULL, "xmimx"); /*创建设备节点,并向系统注册该设备*/
return 0;
}
#define DP_MAJOR MAJOR(dev)
#define DP_MINOR MINOR(dev)
static int char_open(struct inode *inode, struct file *file)
{
if(chropen == 0)
chropen++;
else{
printk(KERN_ALERT"Another process open the char device\n");
return -1;
}
try_module_get(THIS_MODULE);
return 0;
}
static int char_release(struct inode *inode,struct file *file)
{
chropen--;
module_put(THIS_MODULE);
return 0;
}
static int char_read(struct file *filp,char __user *buffer,size_t length,loff_t *offset)
{
unsigned long nn;
nn = copy_to_user(buffer, to, length);
printk("buffer = %s\n", buffer);
return length;
}
static int char_write(struct file *filp, const char __user *buffer, size_t length, loff_t *offset)
{
unsigned long n;
to = (char *)kmalloc((sizeof(char)) * (length+1), GFP_KERNEL);
memset(to, '\0', length+1);
n = copy_from_user(to, buffer, length);
printk("n = %ld\n", n);
printk("to = %s\n", to);
return length;
}
static void __exit module_close(void)
{
len=0;
printk(KERN_ALERT"Unloading..........\n");
unregister_chrdev_region(MKDEV(DP_MAJOR,DP_MINOR),10);
cdev_del(chardev);
device_destroy(my_class, dev);
class_destroy(my_class);
}
module_init(char_init);
module_exit(module_close);
/********************************************************************************/
/*app code*/
#include
#include
#include
#include
#include
#include
#include
main()
{
int fd;
int i;
char data[256];
int retval;
fd=open("/dev/xmimx",O_RDWR);
if(fd==-1)
{
perror("error open\n");
exit(-1);
}
printf("open /dev/xmimx ok\n");
retval=write(fd,"David Zeng",10);
if(retval==-1)
{
perror("write error\n");
exit(-1);
}
memset(data, '\0', 256);
retval=read(fd,data,10);
if(retval==-1)
{
perror("read error\n");
exit(-1);
}
data[retval] = '\0';
printf("read ok:%s\n",data);
close(fd);
}
/***************************************************/
/*****makefile*****/
TARGET=demo2
KDIR=/usr/src/linux-headers-2.6.32-25-generic
PWD=$(shell pwd)
obj-m:=$(TARGET).o
default:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -rf *.o *.ko *.mod.c .*.cmd *~ .tmp_versions modules.order Module.symvers $(TARGET)_test
install:
sudo insmod $(TARGET).ko
uninstall:
sudo rmmod $(TARGET)
test:
gcc -o $(TARGET)_test $(TARGET)_test.c
/**********相关注释请参考其他几篇文章******/