例子1: ------------------------------------------------device function---------------------------------- #define __NO_VERSION__ #include <linux/kernel.h> #include <linux/module.h> #include <linux/version.h> #include <linux/config.h> #include <linux/types.h> #include <linux/fs.h> #include <linux/mm.h> #include <linux/errno.h> #include <asm/segment.h> #include <linux/sched.h> #include <asm/uaccess.h> //#include <linux/uaccess.h> #define KERNEL char kernel_version [] = UTS_RELEASE; unsigned int test_major = 0; static int read_test(struct file *file,char *buf,size_t count,loff_t *f_pos) { int left; if (verify_area(VERIFY_WRITE,buf,count) == -EFAULT ) return -EFAULT; for(left = count ; left > 0 ; left--) { //__put_user(1,buf,1); put_user(left,buf); buf++; } return count; } static int write_tibet(struct inode *inode,struct file *file,const char *buf,int count) { return count; } static int open_tibet(struct inode *inode,struct file *file ) { printk ("device open"); MOD_INC_USE_COUNT; return 0; } static int release_tibet(struct inode *inode,struct file *file ) { printk ("device close"); MOD_DEC_USE_COUNT; } struct file_operations test_fops={ read:read_test, open:open_tibet, release: release_tibet, }; int init_module(void) { int result; result = register_chrdev(0, "test", &test_fops); if (result < 0) { printk("test: can't get major number/n"); return result; } if (test_major == 0) test_major = result; /* dynamic */ printk("hello, i am in kernel mode /n"); return 0; } void cleanup_module(void) { printk("bye, i am going to out /n"); unregister_chrdev(test_major, "test"); } MODULE_LICENSE("GPL"); ------------------------------------------------device Test function---------------------------------- #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> main() { int testdev; int i; char buf [10]; testdev = open ("/dev/test",O_RDWR); if(testdev==-1) { printf("cann't open file /n"); exit (0); } read (testdev,buf,10); for (i=0;i<10;i++) { printf("%d/n",buf[i]); } close(testdev); } ---------------------------------------------debug command------------------------------------------------- [root@localhost proc]# cat version Linux version 2.4.20-8 (bhcompile@porky.devel.redhat.com) (gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)) #1 Thu Mar 13 17:54:28 EST 2003 [root@localhost proc]# :=-Wall -DMODULE -D__KERNEL__ -DLINUX -I /usr/src/linux-2.4.20-8/include -I/usr/src/linux-*.*.*/include gcc -O2 -DMODULE -D__KERNEL__ -DLINUX -I /usr/src/linux-2.4.20-8/include -c test.c gcc -O2 -DMODULE -D__KERNEL__ -DLINUX -I/lib/modules/2.4.20-8/build/include -c test.c gcc -O2 -DMODULE -D_KERNEL_ -DLINUX -I/usr/src/linux-2.4.20-8/include -c test.c insmod CharDriver.o rmmod CharDriver vi /proc/devices cat /proc/devices 查看char_dev 主设备号 mknod /dev/char_dev c 254 0 其中"/dev/char_dev"是要生成的设备名及目录,c 指字符设备,254主设备号 0 从设备号,设置为0 rm -f /dev/char_dev 删除mknod 命令创建的设备文件 ------------------------------------------------------------------------------------------------------ MODULE_LICENSE("GPL"); MODULE_AUTHOR("BECKHAM"); gdb: gcc -g test.c -o test 例子2: ------------------------------------------------CharDriver.c---------------------------------- #define __NO_VERSION__ #include <linux/module.h> #include <linux/version.h> #define KERNEL #include <linux/types.h> #include <linux/fs.h> #include <linux/mm.h> #include <linux/errno.h> #include <asm/segment.h> //#include <linux/sched.h> #include <asm/uaccess.h> //#include <linux/uaccess.h> char kernel_version[]=UTS_RELEASE; static int device_read(struct file *file,char *buf,size_t count,loff_t *f_pos); static int device_open(struct inode *node,struct file *file); static int device_release(struct inode *node,struct file *file); struct file_operations tdd_fops={ read:device_read, open:device_open, release: device_release, }; #define SUCCESS 0 #define DEVICE_NAME "char_dev" static int Device_Open =0; unsigned int test_major=0; static int device_open(struct inode *node,struct file *file) { #ifdef DEBUG printk ("device_open(%p)/n",file); #endif if (Device_Open) return -EBUSY; Device_Open++; MOD_INC_USE_COUNT; return SUCCESS; } static int device_release(struct inode *node,struct file *file) { #ifdef DEBUG printk ("device_release(%p,%p)/n",inode,file); #endif Device_Open--; MOD_DEC_USE_COUNT; } static int device_read(struct file *file,char *buf,size_t count,loff_t *f_pos) { int left; if (verify_area(VERIFY_WRITE,buf,count) == -EFAULT ) return -EFAULT; for(left = count ; left > 0 ; left--) { put_user(left,buf); buf++; } return count; } int init_module(void) { int result; result = register_chrdev(0, "char_dev", &tdd_fops); if (result < 0) { printk("test: can't get major number/n"); return result; } if (test_major == 0) test_major = result; /* dynamic */ printk("hello, i am in kernel mode /n"); return 0; } void cleanup_module(void) { printk("bye, i am going to out /n"); unregister_chrdev(test_major, "char_dev"); } MODULE_LICENSE("GPL"); ------------------------------------------------DriverTest.c--------------------------------- #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(int argc,char *argv[]) { int testdev; int i; char buf [10]; testdev = open ("/dev/char_dev",O_RDWR); if(testdev==-1) { printf("cann't open file /n"); exit (0); } read (testdev,buf,10); for (i=0;i<10;i++) { printf("%d/n",buf[i]); } close(testdev); } ---------------------------------------------debug command------------------------------------------------- [root@localhost proc]# cat version Linux version 2.4.20-8 (bhcompile@porky.devel.redhat.com) (gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)) #1 Thu Mar 13 17:54:28 EST 2003 [root@localhost proc]# :=-Wall -DMODULE -D__KERNEL__ -DLINUX -I /usr/src/linux-2.4.20-8/include -I/usr/src/linux-*.*.*/include gcc -O2 -DMODULE -D__KERNEL__ -DLINUX -I /usr/src/linux-2.4.20-8/include -c CharDriver.c gcc -O2 -DMODULE -D__KERNEL__ -DLINUX -I/lib/modules/2.4.20-8/build/include -c test.c gcc -O2 -DMODULE -D_KERNEL_ -DLINUX -I/usr/src/linux-2.4.20-8/include -c test.c insmod CharDriver.o rmmod CharDriver vi /proc/devices cat /proc/devices 查看char_dev 主设备号 mknod /dev/char_dev c 254 0 其中"/dev/char_dev"是要生成的设备名及目录,c 指字符设备,254主设备号 0 从设备号,设置为0 rm -f /dev/char_dev 删除mknod 命令创建的设备文件 ------------------------------------------------------------------------------------------------------ MODULE_LICENSE("GPL"); MODULE_AUTHOR("BECKHAM"); gdb: gcc -g test.c -o test