ubuntu12.04插入字符设备模块(不用重新编译内核)

直接上操作步骤啦!

1.写好的自己添加的模块源码(test_drv.c )


/* test_drv.c */

#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
        
        
#include 
        
        
          #include 
         
           #include 
          
            #include 
           
             #include 
            
              #define TEST_DEVICE_NAME "test_dev" #define BUFF_SZ 1024 /*全局变量*/ static struct cdev test_dev; unsigned int major =0; static char *data = NULL; /*函数声明*/ static ssize_t test_read(struct file *file, char *buf, size_t count, loff_t *f_pos); static ssize_t test_write(struct file *file,const char *buffer, size_t count,loff_t *f_pos); static int test_open(struct inode *inode, struct file *file); static int test_release(struct inode *inode,struct file *file); /*读函数*/ static ssize_t test_read(struct file *file, char *buf, size_t count, loff_t *f_pos) { int len; if (count < 0 ) { return -EINVAL; } len = strlen(data); count = (len > count)?count:len; if (copy_to_user(buf, data, count)) { return -EFAULT; } return count; } /*写函数*/ static ssize_t test_write(struct file *file, const char *buffer, size_t count, loff_t *f_pos) { if(count < 0) { return -EINVAL; } memset(data, 0, BUFF_SZ); count = (BUFF_SZ > count)?count:BUFF_SZ; if (copy_from_user(data, buffer, count)) { return -EFAULT; } return count; } /*打开函数*/ static int test_open(struct inode *inode, struct file *file) { printk("This is open operation\n"); data = (char*)kmalloc(sizeof(char) * BUFF_SZ, GFP_KERNEL); if (!data) { return -ENOMEM; } memset(data, 0, BUFF_SZ); return 0; } /*关闭函数*/ static int test_release(struct inode *inode,struct file *file) { printk("This is release operation\n"); if (data) { kfree(data); data = NULL; } return 0; } static void test_setup_cdev(struct cdev *dev, int minor, struct file_operations *fops) { int err, devno = MKDEV(major, minor); cdev_init(dev, fops); dev->owner = THIS_MODULE; dev->ops = fops; err = cdev_add (dev, devno, 1); if (err) { printk (KERN_NOTICE "Error %d adding test %d", err, minor); } } /* tests设备的file_operations结构 */ static struct file_operations test_fops = { .owner = THIS_MODULE, .read = test_read, .write = test_write, .open = test_open, .release = test_release, }; /*模块注册入口*/ int init_module(void) { int result; dev_t dev = MKDEV(major, 0); if (major) { result = register_chrdev_region(dev, 1, TEST_DEVICE_NAME); } else { result = alloc_chrdev_region(&dev, 0, 1, TEST_DEVICE_NAME); major = MAJOR(dev); } if (result < 0) { printk(KERN_WARNING "Test device: unable to get major %d\n", major); return result; } test_setup_cdev(&test_dev, 0, &test_fops); printk("The major of the test device is %d\n", major); return 0; } /*卸载模块*/ void cleanup_module(void) { cdev_del(&test_dev); unregister_chrdev_region(MKDEV(major, 0), 1); printk("Test device uninstalled\n"); } 
             
            
           
          
        
       
       
      
      
     
     
    
    

2.编写makefile文件
ifeq ($(KERNELRELEASE),)

KERNELDIR ?= /lib/modules/$(shell uname -r)/build

PWD := $(shell pwd)

modules:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

.PHONY: modules modules_install clean

else
    obj-m := test_drv.o
endif

3. 在终端路径下以root身份编译模块

#sudo make    (调用第一个命令default)

这时,在test_drv.c所在文件夹就会有test_drv.ko ,这个就是我们需要的内核模块啦


4. 插入模块,让其工作(注意必须是root权限)

#insmod test_drv.ko 

完成后,再看看系统中的模块:

#lsmod

显示模块列表,发现多了一个模块“test_drv”,表示成功加载了!


5.分配次设备号

每个文件都有两个设备号,第一个是主设备号,标识驱动程序,第二个是从设备号,标识使用同一个设备驱动程序的不同的硬件设备。

分配前,我们必须要知道主设备号:      

#cat  /proc/devices 

(我这里的主设备号为249.)

现在我们分配从设备号:

#mknod  /dev/test_dev c 249 0

实际上就是在虚拟文件夹/dev/中加一个设备(在系统看来是文件)test_dev ,注意,这个是设备名,在测试文件中真是要利用这个设备名打开设备。最后的0表示从设备号。可以随便分配(只要不冲突)。

此时可以在/dev/目录下看到新建的设备test_dev了:

#ls /dev/


6.测试新加的字符驱动程序

附上测试程序:


/* test.c */

#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
        
        
#include 
        
        
         
         
#include 
         
         
           #include 
          
            #include 
           
             #define TEST_DEVICE_FILENAME "/dev/test_dev" #define BUFF_SZ 1024 int main() { int fd, nwrite, nread; char buff[BUFF_SZ]; fd = open(TEST_DEVICE_FILENAME, O_RDWR); if (fd < 0) { perror("open"); exit(1); } do { printf("Input some words to kernel(enter 'quit' to exit):"); memset(buff, 0, BUFF_SZ); if (fgets(buff, BUFF_SZ, stdin) == NULL) { perror("fgets"); break; } buff[strlen(buff) - 1] = '\0'; if (write(fd, buff, strlen(buff)) < 0) { perror("write"); break; } if (read(fd, buff, BUFF_SZ) < 0) { perror("read"); break; } else { printf("The read string is from kernel:%s\n", buff); } } while(strncmp(buff, "quit", 4)); close(fd); exit(0); } 
            
           
         
        
        
       
       
      
      
     
     

编译测试程序:

#gcc -o test test.c

执行测试程序 :

#./test

之后根据提示输入字符,查看输出即可。


7.删除设备

就像删除普通文件一样:

#rm  /dev/test_dev 

删除后,可查看/dev/目录下已经木有test_dev 了:

#ls /dev/


8.接着删除模块

# rmmod test_drv.ko 

可查模块列表中已经没有test_drv模块了:

#lsmod

这样插入字符驱动模块,并调用显示,再删除模块就都完成啦!




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值