Created
June 3, 2013 13:53
-
-
Save ustbgaofan/5698282 to your computer and use it in GitHub Desktop.
字符设备驱动程序的创建实例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <sys/ioctl.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#define DEVICE_FILENAME "/dev/calldev" | |
int main() | |
{ | |
int dev; | |
char buff[128]; | |
int ret; | |
printf("1) device file open\n"); | |
dev=open(DEVICE_FILENAME,O_RDWR|O_NDELAY); | |
if(dev>=0) | |
{ | |
printf("2)seek function call\n"); | |
ret =lseek(dev,0x20,SEEK_SET); | |
printf("ret=%08X\n",ret); | |
printf("3)read function call \n"); | |
ret=read(dev,0x30,0x31); | |
printf("ret=%08X\n",ret); | |
printf("4) write function call\n"); | |
ret=write(dev,0x40,0x41); | |
printf("ret=%08X\n",ret); | |
printf("5)ioctl function call\n"); | |
ret=ioctl(dev,0x51,0x52); | |
printf("ret=%08X\n",ret); | |
printf("6)device file close\n"); | |
ret=close(dev); | |
printf("ret=%08X\n",ret); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <linux/init.h> | |
#include <linux/module.h> | |
#include <linux/kernel.h> | |
#include <linux/fs.h> | |
#include <linux/errno.h> | |
#include <linux/types.h> | |
#include <linux/fcntl.h> | |
#define CALL_DEV_NAME "calldev" | |
#define CALL_DEV_MAJOR 240 | |
int call_open(struct inode *inode ,struct file *filp) | |
{ | |
int num = MINOR(inode->i_rdev); | |
printk("call open->minor:%d\n",num); | |
return 0; | |
} | |
loff_t call_llseek(struct file *filp,loff_t off,int whence) | |
{ | |
printk("call llseek->off:%08X,whence:%08X\n",off,whence); | |
return 0x23; | |
} | |
ssize_t call_read(struct file *filp,char *buf,size_t count,loff_t *f_pos) | |
{ | |
printk("call read->buf:%08X,count:%08X\n",buf,count); | |
return 0x33; | |
} | |
ssize_t call_write(struct file *filp,const char *buf,size_t count,loff_t *f_pos) | |
{ | |
printk("call write->buf :%08X,count:%08X\n",buf,count); | |
return 0x43; | |
} | |
int call_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg) | |
{ | |
printk("call ioctl -> cmd:%08X,arg:%08X\n",cmd,arg); | |
return 0x53; | |
} | |
int call_release(struct inode *inode ,struct file *filp) | |
{ | |
printk("call release\n"); | |
return 0; | |
} | |
struct file_operations call_fops = | |
{ | |
.owner=THIS_MODULE, | |
.llseek=call_llseek, | |
.read=call_read, | |
.write=call_write, | |
.unlocked_ioctl=call_ioctl, | |
.open=call_open, | |
.release=call_release, | |
}; | |
int call_init (void) | |
{ | |
int result; | |
printk("call call_init \n"); | |
//call_fops.ioctl = call_ioctl; | |
result=register_chrdev(CALL_DEV_MAJOR,CALL_DEV_NAME,&call_fops); | |
if(result<0) return result; | |
return 0; | |
} | |
void call_exit (void) | |
{ | |
printk("call call_exit \n"); | |
unregister_chrdev(CALL_DEV_MAJOR,CALL_DEV_NAME); | |
} | |
module_init (call_init); | |
module_exit (call_exit); | |
MODULE_LICENSE("DUAL BSD /GPL"); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
obj-m:=call_dev.o | |
KDIR :=/lib/modules/$(shell uname -r)/build | |
PWD := $(shell pwd) | |
default: | |
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules | |
clean: | |
rm -rf *.ko | |
rm -rf *.mod.* | |
rm -rf .*.cmd | |
rm -rf *.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mknod /dev/calldev c 240 32 | |
make | |
insmod call_dev.ko | |
lsmod | |
gcc -o call_app call_app.c | |
./call_app.c | |
dmesg | |
rmmod call_dev |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment