#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "myled.h"
#define CNAME "myled"
char kbuf;
struct cdev *cdev;
struct class * cls;
struct device * dev;
#if 1
unsigned int major = 0;//动态申请设备号
#else
unsigned int major = 500; //静态指定设备号
#endif
volatile unsigned int* virt_rcc;
volatile gpio_t* virt_gpioe;
volatile gpio_t* virt_gpiof;
int minor = 0;
const int count = 3;
int mycdev_open(struct inode *inode, struct file *file)
{
dev_t devno;
dev_t minor;
devno=inode->i_rdev;//获得设备号
minor=MINOR(devno);//获得次设备号
file->private_data=(void *)minor;
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_write (struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
int ret;
if(size>sizeof(kbuf))
{
size=sizeof(kbuf);
}
ret=copy_from_user(&kbuf,ubuf,size);
minor=(int)file->private_data;
if(minor == 0) //操作灯1 PE10
{
if(kbuf == '1')
{
virt_gpioe->ODR |= (0x1 << 10); //led1输出高电平
}
else if(kbuf == '0')
{
virt_gpioe->ODR &= (~(0x1 << 10));//led1输出低电平
}
}
else if(minor == 1) //操作灯2 PF10
{
if(kbuf == '1')
{
virt_gpiof->ODR |= (0x1 << 10); //led2输出高电平
}
else if(kbuf == '0')
{
virt_gpiof->ODR &= (~(0x1 << 10));//led2输出低电平
}
}
else if(minor == 2) //操作灯3 PE8
{
if(kbuf == '1')
{
virt_gpioe->ODR |= (0x1 << 8); //led3输出高电平
}
else if(kbuf == '0')
{
virt_gpioe->ODR &= (~(0x1 << 8)); //led3输出低电平
}
}
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
int mycdev_close (struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
const struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
};
//入口
static int __init mycdev_init(void)
{
int ret;
dev_t devno;
int i;
//1.分配cdev结构体
cdev = cdev_alloc();
if(NULL == cdev)
{
printk("cdev alloc is error\n");
ret = -EIO;
goto ERR1;
}
//2.初始化结构体
cdev_init(cdev,&fops);
//3.申请设备号
if(major > 0)
{
//静态指定设备号
ret = register_chrdev_region(MKDEV(major,minor),count,CNAME);
if(ret)
{
printk("register chrdev region is error\n");
ret = -ENOMEM;
goto ERR2;
}
}
else{
//动态申请设备号
ret = alloc_chrdev_region(&devno,0,count,CNAME);
if(ret)
{
printk("alloc chrdev region is error\n");
ret = -ENOMEM;
goto ERR2;
}
//获取主设备号
major = MAJOR(devno);
//获取次设备号
minor = MINOR(devno);
}
//4.驱动的注册
ret = cdev_add(cdev,MKDEV(major,minor),count);
if(ret)
{
printk("cdev add is error\n");
ret = -EIO;
goto ERR3;
}
//5.自动创建设备节点
//1)向上层提交目录信息
cls = class_create(THIS_MODULE,CNAME);
if(IS_ERR(cls))
{
printk("class create is error\n");
ret = PTR_ERR(cls);
goto ERR4;
}
//2)向上层提交设备节点信息
for(i=0;i<count;i++)
{
dev = device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
if(IS_ERR(dev))
{
printk("device create is error\n");
ret = PTR_ERR(dev);
goto ERR5;
}
}
virt_rcc=ioremap(PHY_RCC,4);
if(NULL==virt_rcc)
{
printk("rcc ioremap is error\n");
return -ENOMEM;
}
virt_gpioe=ioremap(PHY_GPIOE,sizeof(gpio_t));
if(NULL==virt_gpioe)
{
printk("gpioe ioremap is error\n");
return -ENOMEM;
}
virt_gpiof=ioremap(PHY_GPIOF,sizeof(gpio_t));
if(NULL==virt_gpiof)
{
printk("gpiof ioremap is error\n");
return -ENOMEM;
}
//PE10--->LED1初始化
*virt_rcc |=(0x1 << 4);
virt_gpioe->MODER &=(~(0x3 << 20));
virt_gpioe->MODER |=(0x1 <<20);
virt_gpioe->ODR &=(~(0x1 << 10));
//PF10--->LED2初始化
*virt_rcc |=(0x1 << 5);
virt_gpioe->MODER &=(~(0x3 << 20));
virt_gpiof->MODER |=(0x1 << 20);
virt_gpiof->ODR &=(~(0x1 << 10));
//PE8--->LED3初始化
virt_gpioe->MODER &=(~(0x3 << 16));
virt_gpioe->MODER |=(0x1 << 16);
virt_gpioe->ODR &=(~(0x1 << 8));
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 -EIO;
}
//出口
static void __exit mycdev_exit(void)
{
int i;
//1。销毁设备节点信息
for(i=0;i<count;i++)
{
device_destroy(cls,MKDEV(major,i));
}
//2.销毁目录信息
class_destroy(cls);
//3.驱动的注销
cdev_del(cdev);
//4.销毁设备号
unregister_chrdev_region(MKDEV(major,minor),count);
//5.释放cdev结构体
kfree(cdev);
iounmap(virt_rcc);
iounmap(virt_gpioe);
iounmap(virt_gpiof);
}
//指定入口地址
module_init(mycdev_init);
module_exit(mycdev_exit);
//指定出口地址
//许可证
MODULE_LICENSE("GPL");