Linux写一个最简单的字符驱动

 

1、驱动主设备号手动分配

 

驱动程序

#include <linux/module.h>

#include <linux/kernel.h>

#include <linux/fs.h>

#include <linux/init.h>

#include <linux/delay.h>

#include <asm/uaccess.h>

#include <asm/irq.h>

#include <asm/io.h>

#include <asm/arch/regs-gpio.h>

#include <asm/hardware.h>



unsigned int major = 1024;





static int first_drv_open(struct inode *inode, struct file *file)

{

	printk("this is first_drv_open\r\n");

	return 0;

}







static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)

{

	printk("this is first_drv_writer\r\n");

	return 0;

}









static struct file_operations first_drv_fops = {

    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */

    .open   =   first_drv_open,     

	.write	=	first_drv_write,	   

};







int first_init(void)

{

	register_chrdev(major, "first_drv", &first_drv_fops); 

	return 0;

}



void first_exit(void)

{

	unregister_chrdev(major, "first_drv");

}







module_init(first_init);

module_exit(first_exit);

 

Makefile:

KERN_DIR=/home/sudaroot/study/linux/kernel/linux-2.6.22.6

all:
	make -C $(KERN_DIR) M=`pwd` modules 

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order

obj-m	+= led.o

编译:

$ make

 

 

测试程序:

#include<stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>







int main(int argc, char **argv)

{

	int fd;

	int val = 1;



	fd = open("/dev/xxx", O_RDWR);

	if(fd < 0)

	{

		printf("can't open!\r\n");

	}



	write(fd, &val, 4);

	

	return 0;

}

编译:

$arm-linux-gcc ledtest.c  -o ledtest

 

加载驱动和测试程序

# insmod led.ko
# mknod /dev/xxx c 1024 0             //创建节点
# ./ledtest 
this is first_drv_open
this is first_drv_writer

 

2、驱动主设备号自动分配

#include <linux/module.h>

#include <linux/kernel.h>

#include <linux/fs.h>

#include <linux/init.h>

#include <linux/delay.h>

#include <asm/uaccess.h>

#include <asm/irq.h>

#include <asm/io.h>

#include <asm/arch/regs-gpio.h>

#include <asm/hardware.h>



unsigned int major = 0;





static int first_drv_open(struct inode *inode, struct file *file)

{

	printk("this is first_drv_open\r\n");

	return 0;

}







static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)

{

	printk("this is first_drv_writer\r\n");

	return 0;

}









static struct file_operations first_drv_fops = {

    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */

    .open   =   first_drv_open,     

	.write	=	first_drv_write,	   

};



int first_init(void)

{

	major = register_chrdev(0, "first_drv", &first_drv_fops); 

	return 0;

}



void first_exit(void)

{

	unregister_chrdev(major, "first_drv");

}







module_init(first_init);

module_exit(first_exit);

通过# cat /proc/devices 

找到系统分配的

后面手动创建节点,执行测试驱动。

 

 

3、自动创建设备节点

Linux系统有有udev和mdev,开发板上的是mdev,mdev会根据驱动设置的信息创建节点。

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>

unsigned int major = 0;
struct class *first_class;
struct class_device *first_class_device; 



static int first_drv_open(struct inode *inode, struct file *file)
{
	printk("this is first_drv_open\r\n");
	return 0;
}



static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
	printk("this is first_drv_writer\r\n");
	return 0;
}




static struct file_operations first_drv_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   first_drv_open,     
	.write	=	first_drv_write,	   
};


int first_init(void)
{
	major = register_chrdev(0, "first_drv", &first_drv_fops); 
	first_class = class_create(THIS_MODULE, "first_drv");		//在/sys下创建一个类
	first_class_device = class_device_create(first_class, NULL, MKDEV(major, 0), NULL, "first_drv");  //根据类和设备号创建设备节点

	return 0;
}


void first_exit(void)
{	
	class_device_unregister(first_class_device);
	class_destroy(first_class);
	unregister_chrdev(major, "first_drv");
}



module_init(first_init);
module_exit(first_exit);
MODULE_LICENSE("GPL");









 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值