按照字符设备驱动分步注册的方式实现控制3个灯
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include<linux/uaccess.h>
#include<linux/io.h>
#include<linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include"myled.h"
#define CNAME "led"
char kbuf[128]={0};
unsigned int*vir_LED1_LED3_moder;
unsigned int*vir_LED1_LED3_odr;
unsigned int*vir_LED2_moder;
unsigned int*vir_LED2_odr;
unsigned int*vir_rcc;
struct class *cls;
struct device *dev;
struct cdev *cdev; //定义字符设备驱动结构体对象指针
#if 1
unsigned int major=0;
#else
unsigned int major=500;
#endif
unsigned int minor=0;
int count=3;
int myled_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t myled_read(struct file *file,
char __user * ubuf, size_t size, loff_t *offs)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t myled_write(struct file *file,
const char __user *ubuf, size_t size, loff_t *offs)
{
int ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int choose,ret;
switch(cmd)
{
case LED_ON:
//判断哪个灯开
//通过copy_from_user来接收第三个参数
ret=copy_from_user(&choose,(void *)arg,sizeof(int));
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
switch(choose)
{
case 0:
(*vir_LED1_LED3_odr) |= (1<<10);//输出低电平
break;
case 1:
(*vir_LED2_odr) |= (1<<10);//输出低电平
break;
case 2:
(*vir_LED1_LED3_odr) |= (1<<8);//输出低电平
break;
}
break;
case LED_OFF:
ret=copy_from_user(&choose,(void *)arg,sizeof(int));
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
switch(choose)
{
case 0:
(*vir_LED1_LED3_odr) &= (~(1<<10));//输出低电平
break;
case 1:
(*vir_LED2_odr) &= (~(1<<10));//输出低电平
break;
case 2:
(*vir_LED1_LED3_odr) &= (~(1<<8));//输出低电平
break;
}
break;
}
return 0;
}
int myled_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
const struct file_operations fops = {
.open = myled_open,
.read = myled_read,
.write = myled_write,
.unlocked_ioctl=mycdev_ioctl,
.release = myled_close,
};
static int __init myled_init(void)
{
// //1.创建字符设备驱动
// major = register_chrdev(0,CNAME,&fops);
// if(major < 0){
// printk("register char device driver error\n");
// return major;
// }
// printk("success major = %d\n",major);
int ret,i;
dev_t devno;
//给字符设备驱动对象申请空间
cdev=cdev_alloc();
if(cdev == NULL)
{
printk("给字符设备驱动对象申请空间失败\n");
ret = -ENOMEM;
goto ERR1;
}
printk("给字符设备驱动对象申请空间成功\n");
//对象的初始化
cdev_init(cdev,&fops);
//申请设备号
if(major==0)
{
ret=alloc_chrdev_region(&devno,minor,count,"led");
if(ret)
{
printk("动态指定设备号失败\n");
goto ERR2;
}
//获取主设备号和次设备号
major=MAJOR(devno);
minor=MINOR(devno);
}
else if(major > 0)
{
ret = register_chrdev_region(MKDEV(major,minor),count,"led");
if(ret)
{
printk("静态指定设备号失败\n");
goto ERR2;
}
}
printk("设备号申请成功\n");
//将字符设备驱动注册进内核
ret = cdev_add(cdev,MKDEV(major,minor),count);
if(ret)
{
printk("字符设备驱动注册失败\n");
goto ERR3;
}
cls=class_create(THIS_MODULE,"led");
if(IS_ERR(cls))
{
printk("向上提交目录信息失败\n");
ret = PTR_ERR(cls);
goto ERR4;
}
printk("向上提交目录信息成功\n");
for(i=0;i<count;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
if(IS_ERR(dev))
{
printk("向上提交节点信息失败\n");
ret = PTR_ERR(dev);
goto ERR5;
}
}
printk("向上提交节点信息成功\n");
vir_LED1_LED3_moder=ioremap(PHY_LED1_LED3_MODER,4);
if(vir_LED1_LED3_moder==NULL)
{
printk("vir_LED1_LED3_moder ioremap failed\n");
return -ENOMEM;
}
vir_LED1_LED3_odr=ioremap(PHY_LED1_LED3_ODR,4);
if(vir_LED1_LED3_odr == NULL)
{
printk("vir_LED1_LED3_odr ioremap failed\n");
return -ENOMEM;
}
vir_LED2_moder=ioremap(PHY_LED2_MODER,4);
if(vir_LED2_moder==NULL)
{
printk("vir_LED2_moder ioremap failed\n");
return -ENOMEM;
}
vir_LED2_odr=ioremap(PHY_LED2_ODR,4);
if(vir_LED2_odr == NULL)
{
printk("vir_LED2_odr ioremap failed\n");
return -ENOMEM;
}
vir_rcc=ioremap(PHY_RCC,4);
if(vir_rcc==NULL)
{
printk("vir_rcc ioremap failed\n");
return -ENOMEM;
}
printk("相关寄存器物理地址映射成功\n");
(*vir_rcc) |= (1<<4);//rcc使能
(*vir_rcc) |= (1<<5);//rcc使能
(*vir_LED1_LED3_moder) &=(~(3<<20));
(*vir_LED1_LED3_moder) |= (1<<20);//设置为输出模式
(*vir_LED1_LED3_moder) &=(~(3<<16));
(*vir_LED1_LED3_moder) |= (1<<16);//设置为输出模式
(*vir_LED1_LED3_odr) &= (~(1<<10));//输出低电平
(*vir_LED1_LED3_odr) &= (~(1<<8));//输出低电平
(*vir_LED2_moder) &=(~(3<<20));
(*vir_LED2_moder) |= (1<<20);//设置为输出模式
(*vir_LED2_odr) &= (~(1<<10));//输出低电平
printk("相关寄存器初始化成功\n");
return 0;
ERR5:
for(--i;i>=0;i--)
{
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major,minor),count);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
static void __exit myled_exit(void)
{
//销毁设备节点
int i;
for(i=0;i<count;i++)
{
device_destroy(cls,MKDEV(major,i));
}
class_destroy(cls);
//删除驱动对象
cdev_del(cdev);
//销毁设备号
unregister_chrdev_region(MKDEV(major,minor),count);
//释放动态申请的字符设备驱动的对象空间
kfree(cdev);
iounmap(vir_LED1_LED3_moder);
iounmap(vir_LED1_LED3_odr);
iounmap(vir_LED2_moder);
iounmap(vir_LED2_odr);
iounmap(vir_rcc);
unregister_chrdev(major,CNAME);
}
module_init(myled_init);
module_exit(myled_exit);
MODULE_LICENSE("GPL");