字符设备驱动开发流程(总结)
来源:互联网 发布:大数据产业市场规模 编辑:程序博客网 时间:2024/05/20 05:08
准备工作:
#include<linux/errno.h>
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/slab.h>
#include<linux/input.h>
#include<linux/init.h>
#include<linux/serio.h>
#include<linux/delay.h>
#include<linux/clk.h>
#include<linux/miscdevice.h>
#include<linux/gpio.h>
#include<asm/io.h>
#include<asm/irq.h>
#include<asm/uaccess.h>
#include<mach/regs-clock.h>
#include<plat/regs-timer.h>
#include<plat/gpio-cfg.h>
#include<mach/regs-gpio.h>
//#include<mach/gpio.h>
#include<linux/cdev.h>
static int beep_major=0;
MODULE_AUTHOR("LIU");
MODULE_LICENSE("Dual BSD/GPL");
#define BEEP_MAGIC 'K'
#define BEEP_START_CMD _IO(BEEP_MAGIC,1)
#define BEEP_STOP_CMD _IO(BEEP_MAGIC,2)
int beep_open(struct inode *inode,struct file *filp)
{
return 0;
}
int beep_release(struct inode *inode,struct file *filp)
{
return 0;
}
ssize_t beep_read(struct file *file,char __user *buff,size_t count,loff_t *offp)
{
return 0;
}
ssize_t beep_write(struct file *file,const char __user *buff,size_t count,loff_t *offp)
{
return 0;
}
void beep_stop(void)
{
//s3c_gpiolib_output(struct gpio_chip *chip,unsigned offset, int value)
//s3c_gpio_cfgpin(S3C64XX_GPIOREG(0x008e),S3C_GPIO_OUTPUT);
//gpio_direction_output(S3C64XX_GPF(14),0);
s3c_gpio_cfgpin(S3C64XX_GPK(4),0);
//s3c2410_gpio_setpin(S3C2410_GPF(14),0);
}
void beep_start(void){
// s3c_gpio_cfgpin(S3C64XX_GPIOREG(0x008e),S3C_GPIO_OUTPUT);
//gpio_direction_output(S3C64XX_GPK(4),1);
s3c_gpio_cfgpin(S3C64XX_GPK(4),1);
// s3c_gpio_setpin(S3C64XX_GPIOREG(0x008e),1);
}
static int beep_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
{
switch(cmd){
case BEEP_START_CMD:{
beep_start();
break;
}
case BEEP_STOP_CMD:{
beep_stop();
break;
}
default:
break;
}
return 0;
}
static struct file_operations beep_remap_ops={
.owner = THIS_MODULE,
.open = beep_open, //此接口得实现
.release = beep_release, //此接口得实现
.read = beep_read, //此接口得实现
.write = beep_write, //此接口得实现
.unlocked_ioctl = beep_ioctl, //此接口得实现
};
static struct cdev BeepDevs; //声明字符设备
xxx_init(void){
int result;
1.字符设备号的获取
dev_t dev=MKDEV(主设备号,从设备号);
char dev_name[]="beep";//设备名
2.申请设备号:
if(beep_major)
{
result=register_chrdev_region(dev,1,dev_name); //手动申请设备号
}
else
{
result=alloc_chrdev_region(&dev,0,1,dev_name);//如果设备号为0则自动申请,参数dev为设备号,参数0为次设备号,1为一个设备,dev_name为设备名
beep_major = MAJOR(dev);// 获取主设备号,为设备节点的创建做准备
}
if(result<0)
{
printk("beep:unable to get major %d\n",beep_major);
}
if(beep_major==0)
beep_major=result;
3.调整到下面函数
beep_setup_cdev(&BeepDevs,0,&beep_remap_ops);
printk("beep device install,with major %d\n",beep_major);
printk("The device name is:%s\n",dev_name);
return 0;
}
static void beep_setup_cdev(struct cdev *dev,int minor,struct file_operation *fops){
int err,devno = MKDEV(beep_major,minor);
3.初始化字符设备
cdev_init(dev,fops);
dev->owner = THIS_MODULE;
dev->ops =fops;
4.把字符设备注册进内核
err=cdev_add(dev,devno,1);
if(err){
printk("error %d adding beep %d",err,minor);
}
}
static void beep_cleanup(void)
{
cdev_del(&BeepDevs);
unregister_chrdev_region(MKDEV(beep_major,0),1);
printk("beep driver uninstll sucess!");
}
module_init(beep_init);
module_exit(beep_cleanup);
- 字符设备驱动开发流程(总结)
- 字符设备驱动开发流程详解
- 字符设备驱动开发流程详解
- 简单字符设备驱动流程
- 字符设备驱动开发
- linux字符设备驱动总结
- Linux字符设备驱动总结
- LINUX字符设备驱动总结
- Linux字符设备驱动总结
- 字符设备驱动学习总结
- Linux设备驱动开发流程
- Linux设备驱动开发详解总结(一)之字符设备驱动结构
- linux驱动开发-字符设备
- linux字符设备驱动开发
- 驱动开发-字符设备1
- 驱动开发-字符设备2
- 开发一个字符设备驱动
- 【Linux开发】linux设备驱动归纳总结(三):1.字符型设备之设备申请
- oracle中exp,imp的使用详解
- main函数中的argc与argv
- 程序崩溃时自动记录minidump的c++类
- “Runtime Error”不产生dump文件的解决办法
- Hadoop源码之Map/Reduce应用过程
- 字符设备驱动开发流程(总结)
- PHP从页面上传图片到数据库(实例)
- ADB WINDOWS下显示乱码
- 【shell】shell脚本的调试方式(二)
- Android 调试方法
- 使用Windbg生成dump文件
- STM32第二功能引脚映射
- OGNL表达式struts2标签“%,#,$”
- Andriod Sensor