Linux系统下写一个驱动程序,Linux下的第一个驱动程序(模板)

makefile文件:

KERN_DIR = /home/youshan/linux-2.6.32.2

all:

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

clean:

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

rm -rf modules.order

obj-m   += first_drv.o驱动程序:

/*************************************************

* File name   : first_drv.c(可当做模板使用)

* Description : 参考韦东山视频,改写了部分代码

* Author      : sg131971@qq.com

* Version     : V1.0

* Date        :

* Compiler    : arm-linux-gcc-4.4.3

* Target      : mini2440(Linux-2.6.32)

* History     :

*           

*************************************************/

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include        /* device_create() */

staticstructclass*firstdrv_class;

staticstructclass_device *firstdrv_class_dev;

staticintmajor;

staticintfirst_drv_open(structinode *inode,structfile *file)

{

printk("First_drv_open\n");

return0;

}

staticssize_t first_drv_read(structfile *file,constchar__user *buf,size_tcount, loff_t * ppos)

{

printk("First_drv_read\n");

return0;

}

staticssize_t first_drv_write(structfile *file,constchar__user *buf,size_tcount, loff_t * ppos)

{

printk("First_drv_write\n");

return0;

}

staticintfirst_drv_release(structinode *inode,structfile *file)

{

printk("First_drv_release\n");

return0;

}

staticstructfile_operations first_drv_fops = {

.owner = THIS_MODULE,

.open = first_drv_open,

.read = first_drv_read,

.write = first_drv_write,

.release = first_drv_release,

};

staticint__init first_drv_init(void)

{

/* cat /proc/devices  */

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

/* ls /sys/class/first_drv */

firstdrv_class = class_create(THIS_MODULE,"first_drv_sys");

if(IS_ERR(firstdrv_class))

returnPTR_ERR(firstdrv_class);

/* ls /dev/first_drv */

firstdrv_class_dev = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL,"first_drv_dev");

if(unlikely(IS_ERR(firstdrv_class_dev)))

returnPTR_ERR(firstdrv_class_dev);

return0;

}

staticvoid__exit first_drv_exit(void)

{

unregister_chrdev(major,"first_drv_proc");

device_unregister(firstdrv_class_dev);

class_destroy(firstdrv_class);

}

module_init(first_drv_init);

module_exit(first_drv_exit);

MODULE_AUTHOR("WHUT-ShiGuang");

MODULE_DESCRIPTION("Mini2440 Test Driver");

MODULE_VERSION("1.0");

MODULE_LICENSE("GPL");

测试程序:

/*************************************************

* File name   :

* Description :

* Author      : sg131971@qq.com

* Version     : V1.0

* Date        :

* Compiler    : arm-linux-gcc-4.4.3

* Target      : mini2440(Linux-2.6.32)

* History     :

*           

*************************************************/

#include

#include

#include

#include

#include

intmain()

{

intfd;

intval = 1;

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

printf("fd = %d\n", fd);

write(fd, &val, 4);

read(fd, &val, 4);

close(fd);

return0;

}测试结果:

[root@ www.linuxidc.com /home]#

[root@ www.linuxidc.com /home]# ls /dev/first_drv_dev -l

crw-rw----    1 root     root     253,   0 Jan  1 08:31 /dev/first_drv_dev

[root@ www.linuxidc.com /home]#

[root@ www.linuxidc.com /home]# cat /proc/devices | grep first_drv

253 first_drv_proc

[root@ www.linuxidc.com /home]#

[root@ www.linuxidc.com /home]# ls /sys/class/first_drv_sys/

first_drv_dev

[root@ www.linuxidc.com /home]#

[root@ www.linuxidc.com /home]#

[root@ www.linuxidc.com /home]# ./app

First_drv_open

fd = 3

First_drv_write

First_drv_read

First_drv_release

[root@ www.linuxidc.com /home]#

[root@ www.linuxidc.com /home]#

bd92d10dc4abf7b308e6aec0fc3053c2.gif OVER! THANK YOU !0b1331709591d260c1c78e86d0c51c18.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值