linux设备驱动程序的编写
系统大全为您提供
star.h
#ifndef STAR_H_H_H
#define STAR_H_H_H
#define AUTHOR "Tao Yang"
#define DESCRIPTION "A CHAR DEVICE DIRVERS SAMPLE USING UDEV"
#define VERSION "0.1"
#endif
star.c
//module_init module_exit
#include
#include
//module_param
#include
//printk container_of
#include
//dev_t MAJOR MINOR MKDEV
#include
//file_operations file register/unregister_chrdev_region alloc_chrdev_region register/unregister_chrdev(old)
#include
/ev cdev_init/add/del
#include
//copy_from_user copy_to_user
#include
#include
#include "star.h"
//define device number
#define STAR_MAJOR 0
#define STAR_MINOR 0
#define STAR_DEVS 1
#define DEVICE_NAME "star0"
#define CLASS_NAME "star"
static int howmany=5;
module_param(howmany,int,S_IRUGO);
//module info
MODULE_AUTHOR(AUTHOR);
MODULE_DESCRIPTION(DESCRIPTION);
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");
//device variables
static struct class* star_class=NULL;
static struct device* star_sysdevice=NULL;
int star_major=STAR_MAJOR;
int star_minor=STAR_MINOR;
int star_nr_devs=STAR_DEVS;
//device struct
static struct star_dev {
char *data;
struct cdev cdev;
};
static struct star_dev star_device;
static ssize_t star_read(struct file * filp,char * buf,size_t count,loff_t *ppos)
{
int i;
char star_str[10000];
struct star_dev *dev=filp->private_data;
for (i=0;i
star_str[i]='*';
}
star_str[howmany]=' ';
int len=strlen(star_str);
if(count
return -EINVAL;
if(*ppos!=0)
return 0;
if(copy_to_user(buf,star_str,len))
return -EINVAL;
*ppos=len;
return len;
}
int star_open(struct inode *inode,struct file *filp)
{
struct star_dev *dev;
dev=container_of(inode->i_cdev,struct star_dev,cdev);
filp->private_data=dev;
//......
return 0;
}
//file operations
static const struct file_operations star_fops = {
.owner = THIS_MODULE,
.read = star_read,
.open = star_open,
};
static void star_setup_cdev(struct star_dev *dev,int index)
{
int err,devno=MKDEV(star_major,star_minor+index);
printk(KERN_ALERT "setup cdev... ");
cdev_init(&dev->cdev,&star_fops);
dev->cdev.owner=THIS_MODULE;
dev->cdev.ops=&star_fops;
err=cdev_add(&dev->cdev,devno,1);
if(err)
printk(KERN_ALERT "Error %d adding star%d",err,index);
}
static int __init star_init(void)
{
int ret;
dev_t dev;
printk(KERN_ALERT "hello tom! ");
if(star_major){
dev=MKDEV(star_major,star_minor);
ret=register_chrdev_region(dev,star_nr_devs,DEVICE_NAME);
printk(KERN_ALERT "static! ");
}else{
ret=alloc_chrdev_region(&dev,star_minor,star_nr_devs,DEVICE_NAME);
star_major=MAJOR(dev);
printk(KERN_ALERT "dynamic! ");
printk(KERN_ALERT "Device Major is %d! ",star_major);
}
if(ret<0){
printk(KERN_ALERT "star:can't get major %d ",star_major);
return ret;
}
printk(KERN_ALERT "set up cdev!");
star_setup_cdev(&star_device,0);
star_class=class_create(THIS_MODULE,CLASS_NAME);
if(IS_ERR(star_class)){
printk(KERN_ALERT "failed to register device class '%s' ",CLASS_NAME);
}
//with a class ,the easiest way to instantiate a device is to call device_create()
star_sysdevice=device_create(star_class,NULL,MKDEV(star_major,0),NULL,DEVICE_NAME);
return 0;
}
static void __exit star_exit(void)
{
device_destroy(star_class,MKDEV(star_major,star_minor));
class_unregister(star_class);
class_destroy(star_class);
cdev_del(&star_device.cdev);
printk(KERN_ALERT "goodbye... ");
}
module_init(star_init);
module_exit(star_exit);
Makefile
ifneq ($(KERNELRELEASE),)
obj-m:=star.o
else
KERNELDIR ?= b/modules/$(shell uname -r)ild
PWD :=$(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
编译源代码:
root@ubuntu:~/embedded_linux/ldd3/practice# make
make -C b/modules/3.8.0-29-genericild M=/home/tom/embedded_linux/ldd3/practice modules
make[1]: Entering directory `/usrcnux-headers-3.8.0-29-generic'
CC [M] /home/tom/embedded_linux/ldd3/practice/star.o
/home/tom/embedded_linux/ldd3/practice/star.c:58:1: warning: useless storage class specifier in empty declaration [enabled by default]
/home/tom/embedded_linux/ldd3/practice/star.c: In function ‘star_read’:
/home/tom/embedded_linux/ldd3/practice/star.c:72:5: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
/home/tom/embedded_linux/ldd3/practice/star.c:66:22: warning: unused variable ‘dev’ [-Wunused-variable]
/home/tom/embedded_linux/ldd3/practice/star.c:81:1: warning: the frame size of 10032 bytes is larger than 1024 bytes [-Wframe-larger-than=]
Building modules, stage 2.
MODPOST 1 modules
CC /home/tom/embedded_linux/ldd3/practice/star.mod.o
LD [M] /home/tom/embedded_linux/ldd3/practice/star.ko
make[1]: Leaving directory `/usrcnux-headers-3.8.0-29-generic'
root@ubuntu:~/embedded_linux/ldd3/practice#
安装驱动模块:
root@ubuntu:~/embedded_linux/ldd3/practice# insmod star.ko
root@ubuntu:~/embedded_linux/ldd3/practice# dmesg -c
[ 3946.696548] hello tom!
[ 3946.699892] dynamic!
[ 3946.699894] Device Major is 249!
[ 3946.699896] setup cdev...
本文来自系统大全http://www.win7cn.com/如需转载请注明!推荐:win7纯净版
相关文章
- Win11 Dev 26300.7674 发布!正式开启 26300 篇章
- NVIDIA 发布 591.86 显卡驱动!附下载地址
- Win11 更新致调制解调器失效,微软回应非 Bug 是安全调整
- 微软发布 Win11 24H2/25H2 紧急补丁 KB5078127
- 微软发布 Win11/10 紧急带外更新,修复关机异常和远程登录问题
- AMD 最新显卡驱动 26.1.1 版本发布!附下载地址
- 英特尔发布 32.0.101.8425 显卡驱动!附文件下载
- 微软 PowerToys 0.97 发布:命令面板可自定义
- 微信 Windows 4.1.7 内测更新:新增群接龙 + 语音转文字
- 微软 Win11 预览版桌面水印不更新问题将修复
系统下载排行榜71011xp
【纯净之家】Windows7 64位 全新纯净版
2番茄花园 Win7 64位 快速稳定版
3【纯净之家】Windows7 32位 全新纯净版
4【纯净之家】Win7 64位 Office2007 办公旗舰版
5【雨林木风】Windows7 64位 装机旗舰版
6JUJUMAO Win7 64位旗舰纯净版
7【纯净之家】Windows7 SP1 32位 全补丁旗舰版
8【电脑公司】Windows7 64位 免费旗舰版
9【深度技术】Windows7 64位 官方旗舰版
10【游戏专用】Windows7 64位 装机旗舰版
【纯净之家】Windows10 22H2 64位 游戏优化版
2【纯净之家】Windows10 22H2 64位 专业工作站版
3【纯净之家】Windows10企业版LTSC 2021 纯净版
4【深度技术】Windows10 64位 专业精简版
5【纯净之家】Windows10 22H2 64位 企业版
6【雨林木风】Windows10 64位 专业精简版
7JUJUMAO Win10 X64 极速精简版
8【深度技术】 Win10 64位国庆特别版
9【纯净之家】Windows10 32位 官方正式版
10【雨林木风】Windows10 64位 官方专业版
【纯净之家】Windows11 23H2 64位 游戏优化版
2【纯净之家】Windows11 23H2 64位 专业工作站版
3【纯净之家】Windows11 23H2 64位 纯净专业版
4风林火山Windows11下载中文版(24H2)
5【纯净之家】Windows11 23H2 64位 企业版
6【纯净之家】Windows11 23H2 64位 中文家庭版
7JUJUMAO Win11 24H2 64位 专业版镜像
8【纯净之家】Windows11 23H2 64位专业精简版
9【纯净之家】Windows11 23H2 64位 纯净家庭版
10JUJUMAO Win11 24H2 64位 精简版镜像
深度技术 GHOST XP SP3 电脑专用版 V2017.03
2深度技术 GGHOST XP SP3 电脑专用版 V2017.02
3萝卜家园 GHOST XP SP3 万能装机版 V2017.03
4番茄花园 GHOST XP SP3 极速体验版 V2017.03
5Win7系统下载 PCOS技术Ghost WinXP SP3 2017 夏季装机版
6雨林木风 GHOST XP SP3 官方旗舰版 V2017.03
7电脑公司 GHOST XP SP3 经典旗舰版 V2017.03
8萝卜家园 GHOST XP SP3 完美装机版 V2016.10
9雨林木风GHOST XP SP3完美纯净版【V201710】已激活
10雨林木风 GHOST XP SP3 精英装机版 V2017.04
热门教程
装机必备 更多+
重装工具



