分类: LINUX
2011-04-07 12:18:14
static int __init gobalvar_init(void) { if (register_chrdev(MAJOR_NUM, " gobalvar ", &gobalvar_fops)) { //…注册失败 } else { //…注册成功 } } |
static void __exit gobalvar_exit(void) { if (unregister_chrdev(MAJOR_NUM, " gobalvar ")) { //…卸载失败 } else { //…卸载成功 } } |
int (*open)(struct inode * ,struct file *); |
void (*release) (struct inode * ,struct file *) ; |
ssize_t (*read) (struct file *, char *, size_t, loff_t *); |
static ssize_t globalvar_read(struct file *filp, char *buf, size_t len, loff_t *off) { … copy_to_user(buf, &global_var, sizeof(int)); … } |
ssize_t (*write) (struct file *, const char *, size_t, loff_t *); |
static ssize_t globalvar_write(struct file *filp, const char *buf, size_t len, loff_t *off) { … copy_from_user(&global_var, buf, sizeof(int)); … } |
int (*ioctl) (struct inode * ,struct file * ,unsigned int ,unsigned long); |
loff_t (*llseek) (struct file *, loff_t, int); |
unsigned int (*poll) (struct file *, struct poll_table_struct *); |
struct file_operations gobalvar_fops = { read: gobalvar_read, write: gobalvar_write, }; |
#include #include #include #include MODULE_LICENSE("GPL"); #define MAJOR_NUM 254 //主设备号 static ssize_t globalvar_read(struct file *, char *, size_t, loff_t*); static ssize_t globalvar_write(struct file *, const char *, size_t, loff_t*); //初始化字符设备驱动的file_operations结构体 struct file_operations globalvar_fops = { read: globalvar_read, write: globalvar_write, }; static int global_var = 0; //"globalvar"设备的全局变量 static int __init globalvar_init(void) { int ret; //注册设备驱动 ret = register_chrdev(MAJOR_NUM, "globalvar", &globalvar_fops); if (ret) { printk("globalvar register failure"); } else { printk("globalvar register success"); } return ret; } static void __exit globalvar_exit(void) { int ret; //注销设备驱动 ret = unregister_chrdev(MAJOR_NUM, "globalvar"); if (ret) { printk("globalvar unregister failure"); } else { printk("globalvar unregister success"); } } static ssize_t globalvar_read(struct file *filp, char *buf, size_t len, loff_t *off) { //将global_var从内核空间复制到用户空间 if (copy_to_user(buf, &global_var, sizeof(int))) { return - EFAULT; } return sizeof(int); } static ssize_t globalvar_write(struct file *filp, const char *buf, size_t len, loff_t *off) { //将用户空间的数据复制到内核空间的global_var if (copy_from_user(&global_var, buf, sizeof(int))) { return - EFAULT; } return sizeof(int); } module_init(globalvar_init); module_exit(globalvar_exit); |
gcc -D__KERNEL__ -DMODULE -DLINUX -I /usr/local/src/linux2.4/include -c -o globalvar.o globalvar.c |
inmod globalvar.o |
cat /proc/devices |
mknod /dev/globalvar c 254 0 |
#include #include #include #include main() { int fd, num; //打开"/dev/globalvar" fd = open("/dev/globalvar", O_RDWR, S_IRUSR | S_IWUSR); if (fd != -1 ) { //初次读globalvar read(fd, &num, sizeof(int)); printf("The globalvar is %d\n", num); //写globalvar printf("Please input the num written to globalvar\n"); scanf("%d", &num); write(fd, &num, sizeof(int)); //再次读globalvar read(fd, &num, sizeof(int)); printf("The globalvar is %d\n", num); //关闭"/dev/globalvar" close(fd); } else { printf("Device open failure\n"); } } |
gcc -o globalvartest.o globalvartest.c |
./globalvartest.o |