创作本文目的:记录自己的学习历程
一、创建三个设备节点分别控制三个LED灯
1. 驱动文件
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include "dev.h"
#define CNAME "mycdev"
struct class *cls; //句柄
struct cdev *cdev;
struct device *dev;
char kbuf[128] = {}; //定义数组用于存放和用户之间拷贝的数据
int minor = 1; //次设备号从0开始
#if 0
unsigned int major = 0; //动态申请
#else
unsigned int major = 500; //静态指定设备号
#endif
const int count = 5; //指定设备节点的个数为5
//寄存器
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
gpio_t *vir_bee;
gpio_t *vir_fan;
unsigned int *vir_rcc;
// open
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
// read
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
// size参数是用户期待读到的字节长度
int ret;
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_to_user(ubuf, kbuf, size);
if (ret)
{
printk("数据从内核向用户拷贝失败\n");
return -EIO;
}
return size;
}
// write
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);
if (ret)
{
printk("内核向用户拷贝数据失败\n");
return -EIO;
}
return size;
}
long ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int ret, which;
switch (cmd)
{
case DEV_ON:
ret = copy_from_user(&which, (void *)arg, sizeof(int));
if (ret)
{
printk("用户向内核拷贝数据失败\n");
return -EIO;
}
switch (which)
{
case LED1:
vir_led1->ODR |= (1 << 10);
break;
case LED2:
vir_led2->ODR |= (1 << 10);
break;
case LED3:
vir_led3->ODR |= (1 << 8);
break;
case FAN:
vir_fan->ODR |= (1 << 9);
break;
case BEE:
vir_bee->ODR |= (1 << 6);
break;
}
break;
case DEV_OFF:
ret = copy_from_user(&which, (void *)arg, sizeof(int));
if (ret)
{
printk("用户向内核拷贝数据失败\n");
return -EIO;
}
switch (which)
{
case LED1:
vir_led1->ODR &= ~(1 << 10);
break;
case LED2:
vir_led2->ODR &= ~(1 << 10);
break;
case LED3:
vir_led3->ODR &= ~(1 << 8);
break;
case FAN:
vir_fan->ODR &= ~(1 << 9);
break;
case BEE:
vir_bee->ODR &= ~(1 << 6);
break;
}
break;
default:
printk("功能码错误\n");
break;
}
return 0;
}
// close
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
int all_led_init(void)
{
//进行物理地址的映射
vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
if (vir_led1 == NULL)
{
printk("vir_led1 映射失败\n");
return -ENOMEM;
}
printk("vir_led1 映射成功\n");
vir_led2 = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));
if (vir_led2 == NULL)
{
printk("vir_led2 映射失败\n");
return -ENOMEM;
}
printk("vir_led2 映射成功\n");
vir_led3 = ioremap(PHY_LED3_ADDR, sizeof(gpio_t));
if (vir_led3 == NULL)
{
printk("vir_led3 映射失败\n");
return -ENOMEM;
}
printk("vir_led3 映射成功\n");
vir_fan = ioremap(PHY_FAN_ADDR, sizeof(gpio_t));
if (vir_fan == NULL)
{
printk("vir_fan 映射失败\n");
return -ENOMEM;
}
printk("vir_fan 映射成功\n");
vir_bee = ioremap(PHY_BEE_ADDR, sizeof(gpio_t));
if (vir_bee == NULL)
{
printk("vir_bee 映射失败\n");
return -ENOMEM;
}
printk("vir_bee 映射成功\n");
vir_rcc = ioremap(PHY_RCC_ADDR, 4);
if (vir_rcc == NULL)
{
printk("vir_rcc 映射失败\n");
return -ENOMEM;
}
printk("vir_rcc 映射成功\n");
//寄存器的初始化
// led1
vir_led1->MODER &= ~(3 << 20);
vir_led1->MODER |= (1 << 20);
vir_led1->ODR &= ~(1 << 10);
// led2
vir_led2->MODER &= ~(3 << 20);
vir_led2->MODER |= (1 << 20);
vir_led2->ODR &= ~(1 << 10);
// led3
vir_led3->MODER &= ~(3 << 16);
vir_led3->MODER |= (1 << 16);
vir_led3->ODR |= (1 << 8);
// fan
vir_fan->MODER &= ~(3 << 18);
vir_fan->MODER |= (1 << 18);
vir_fan->ODR &= ~(1 << 9);
// bee
vir_bee->MODER &= ~(3 << 12);
vir_bee->MODER |= (1 << 12);
vir_bee->ODR &= ~(1 << 6);
(*vir_rcc) |= (49 << 1);
return 0;
}
//操作方法结构体的初始化
struct file_operations fops =
{
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.unlocked_ioctl = ioctl,
.release = mycdev_close,
};
//入口函数,当驱动安装的时候执行
static int __init mycdev_init(void)
{
int ret, i;
dev_t devno;
// 1.分配设备驱动对象
cdev = cdev_alloc();
if (cdev == NULL)
{
printk("字符设备驱动申请空间失败\n");
ret = -ENOMEM;
goto ERR1;
}
printk("对象分配成功\n");
// 2.初始化设备驱动对象
cdev_init(cdev, &fops);
// 3.设备号的申请
if (major == 0) //动态申请
{
ret = alloc_chrdev_region(&devno, minor, count, "mydev");
if (ret)
{
printk("动态申请设备号失败\n");
goto ERR2;
}
major = MAJOR(devno);
minor = MINOR(devno);
printk("动态申请设备号成功\n");
}
else
{
//静态指定设备号
ret = register_chrdev_region(MKDEV(major, minor), count, "mydev");
if (ret)
{
printk("静态申请设备号失败\n");
goto ERR2;
}
printk("静态申请设备号成功\n");
}
// 4.注册字符设备驱动
ret = cdev_add(cdev, MKDEV(major, minor), count);
if (ret)
{
printk("字符设备驱动注册失败\n");
goto ERR3;
}
printk("注册字符设备驱动成功\n");
// 5.自动创建设备节点
cls = class_create(THIS_MODULE, "dev");
if (IS_ERR(cls))
{
printk("创建逻辑节点目录失败\n");
ret = PTR_ERR(cls);
goto ERR4;
}
printk("创建逻辑节点目录成功\n");
//向上提交节点信息
for (i = 1; i <= count; i++)
{
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "dev_%d", i);
if (IS_ERR(dev))
{
printk("创建逻辑节点失败\n");
ret = PTR_ERR(dev);
goto ERR5;
}
}
printk("创建逻辑节点成功\n");
//寄存器初始化
all_led_init();
return 0;
ERR5:
//销毁设备节点
for (--i; i >= 1; 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 mycdev_exit(void)
{
// 1.销毁设备节点
int i;
for (i = 1; i <= count; i++)
{
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
// 2.注册字符设备驱动
cdev_del(cdev);
// 3.释放设备号
unregister_chrdev_region(MKDEV(major, minor), count);
// 4.释放动态申请的空间
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
2. 测试文件
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "dev.h"
int main(int argc, char const *argv[])
{
char buf[128] = "";
int which;
char path[128] = "";
while (1)
{
memset(buf, 0, sizeof(buf));
memset(path,0,sizeof(path));
printf("请输入操作命令(1:LED1,2:LED2,3:LED3,4:风扇,5:蜂鸣器,0:退出)\n");
scanf("%d",&which);
getchar();
if(which==0)
{
break;
}
sprintf(path,"/dev/dev_%d",which);
int fd = open(path, O_RDWR);
if (fd < 0)
{
printf("打开设备文件%d失败\n",which);
exit(-1);
}
printf("设备文件%d打开成功\n",which);
printf("请输入控制命令(0:关闭,1:开启,q:退出)\n");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
if(buf[0]=='q')
{
break;
}
if (buf[0] == '1')
ioctl(fd, DEV_ON,&which);
else if (buf[0] == '0')
ioctl(fd, DEV_OFF,&which);
else
printf("控制命令不存在\n");
close(fd);
}
return 0;
}