实验71模块驱动架构实验.docVIP

  • 2
  • 0
  • 约 9页
  • 2016-05-24 发布于重庆
  • 举报
实验71模块驱动架构实验

实验 7-1 模块驱动架构实验 【实验目的】 熟悉模块驱动架构. 掌握模块驱动的makefile文件的编写。 掌握模块驱动的加载和卸载。 【实验步骤】 利用vi编辑器,编写一个xsbase.c驱动架构代码; #include linux/module.h #include linux/fs.h #include linux/init.h #include asm/uaccess.h MODULE_LICENSE(GPL); static char *data; #define xsbase_major 65 #define DEVICE_NAME xsbase static int xsbase_open(struct inode *inode, struct file *file) { printk(KERN_EMERG This xsbase dev is opened!\n); return 0; } static int xsbase_release(struct inode *inode, struct file *file) { printk(KERN_EMERG The xsbase dev is released!\n); return 0; } static ssize_t xsbase_write( struct file *file, const char *buffer, size_t count, loff_t *f_pos) { if(count 0) return -EINVAL; kfree(data); data = (char *)kmalloc(sizeof(char)*(count+1), GFP_KERNEL); if(!data) return -ENOMEM; copy_from_user(data, buffer, count+1); return count; } static ssize_t xsbase_read(struct file *file, char *buf, size_t count, loff_t *f_pos) { int len; if(count 0) return -EINVAL; len = strlen(data); if(len count) count = len; copy_to_user(buf, data, count+1); return count; } static struct file_operations chr_fops = { read: xsbase_read, write: xsbase_write, open: xsbase_open, release: xsbase_release, }; int __init xsbase_init(void) { int res; res = register_chrdev(65, DEVICE_NAME, chr_fops); if(res 0) { printk(================cant get major name!\n); return res; } else printk(KERN_EMERG The xsbase dev is init!\n); return 0; } static void __exit xsbase_exit(void) { printk(KERN_EMERG The xsbase dev is exited ! \n); unregister_chrdev(65,DEVICE_NAME); } module_init(xsbase_init); module_exit(xsbase_exit); MODULE_AUTHOR(Ben.li, ben.li@); MODULE_DESCRIPTION(This is a driver struct demo); 利用vi编辑器,编写一个用于编译xsbase.c驱动架构Makefile文件;其中KERNELDIR ? = /root/work/linux-2.6.9为目标平台Linux内核的所在的路径 # Makefile for the XSBASE. # CFLAGS +=$(DEBFLAGS) -Wall ifneq ($(KERNELRELEASE),) obj-m :=xsbase.o else KERNELDIR ?=

文档评论(0)

1亿VIP精品文档

相关文档