实验步骤:
1.配置环境
2.阅读和理解代码
#define DEVICE_NAME "demo" static ssize_t demo_write(struct file *filp,const char * buffer, size_t count) { char drv_buf[]; copy_from_user(drv_buf , buffer, count); … } static ssize_t demo_read(struct file *filp,char *buffer,size_t count,loff_t *ppos) { char drv_buf[]; copy_to_user(buffer, drv_buf,count); …. } static int demo_ioctl(struct inode *inode, struct file *file,unsigned int cmd, unsigned long arg) { } static int demo_open(struct inode *inode, struct file *file) { } static int demo_release(struct inode *inode, struct file *filp) { MOD_DEC_USE_COUNT; DPRINTK("device release "); return 0; } static struct file_operations demo_fops = { owner: THIS_MODULE, write:demo_write, read: demo_read, ioctl: demo_ioctl, open: demo_open, release:demo_release, }; #ifdef CONFIG_DEVFS_FS static devfs_handle_t devfs_demo_dir, devfs_demoraw; #endif static int __init demo_init(void) { int result; #ifdef CONFIG_DEVFS_FS devfs_demo_dir = devfs_mk_dir(NULL, "demo", NULL); devfs_demoraw = devfs_register(devfs_demo_dir, "0", DEVFS_FL_DEFAULT, demo_Major, demo_MINOR, S_IFCHR | S_IRUSR | S_IWUSR,&demo_fops, NULL); #else SET_MODULE_OWNER(&demo_fops); result = register_chrdev(demo_Major, "scullc", &demo_fops); if (result < 0) return result; if (demo_Major == 0) demo_Major = result; /* dynamic */ #endif printk(DEVICE_NAME " initialized "); return 0; } static void __exit demo_exit(void) { unregister_chrdev(demo_major, "demo"); kfree(demo_devices); printk(DEVICE_NAME " unloaded "); } module_init(demo_init); module_exit(demo_exit);
3.make成功
4.测试驱动程序
输入命令./test_demo,插入驱动模块 demo.o
实验结果: