Table of contents
dt.c
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/gpio.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include <linux/uaccess.h>
char kbuf[128] = { 0 }; // 存储用户空间数据
int ret = 0; // 返回值
int i = 0; // 循环变量
struct cdev* cdev; // 字符设备驱动结构体
unsigned int major = 500; // 主设备号
unsigned int minor = 0; // 次设备号
unsigned int count = 1; // 申请设备资源个数
dev_t devno; // 设备号
struct class* cls; // 目录信息结构体
struct device* dev; // 设备信息结构体
struct device_node* dnode = NULL; // 设备树节点
int gpiono1, gpiono2, gpiono3; // gpio编号
// struct timer_list timer; // 分配定时器对象
int mycdev_open(struct inode* inode, struct file* file);
ssize_t mycdev_read(struct file* file, char __user* ubuf, size_t size, loff_t* iof);
ssize_t mycdev_write(struct file* file, const char __user* ubuf, size_t size, loff_t* iof);
long mycdev_ioctl(struct file* file, unsigned int cmd, unsigned long arg);
int mycdev_close(struct inode* inode, struct file* file);
// 定义一个操作方法结构体变量并初始化
struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
// 解析gpio设备树节点,初始化,申请gpio编号
int my_gpio_init(struct device_node** dnode, const char* name, const char* propname, int index)
{
int gpiono; // 临时gpio编号
// 解析设备树节点
if (*dnode == NULL) {
*dnode = of_find_node_by_name(NULL, name);
if (*dnode == NULL) {
printk("解析设备树节点失败\n");
return -ENOMEM;
}
}
// 根据设备树节点解析gpio编号
gpiono = of_get_named_gpio(*dnode, propname, index);
if (gpiono < 0) {
printk("解析gpio编号失败\n");
return -EIO;
}
// 向内核申请gpio编号
if (gpio_request(gpiono, NULL)) {
printk("申请gpio编号失败\n");
return -EIO;
}
printk("申请gpio编号成功%d\n", gpiono);
// 设置引脚为输出
gpio_direction_output(gpiono, 0);
return gpiono;
}
// 定时器处理函数
// void mytimer_func(struct timer_list* timer)
// {
// // 将led3一秒亮一秒灭
// gpio_set_value(gpiono3, !gpio_get_value(gpiono3));
// // 重启定时器
// mod_timer(timer, (jiffies + HZ));
// }
static int __init mycdev_init(void)
{
// // 设置定时阈值
// timer.expires = jiffies + HZ;
// // 初始化定时器
// timer_setup(&timer, mytimer_func, 0);
// // 将定时器注册进内核并启用
// add_timer(&timer);
// 1.分配对象空间
cdev = cdev_alloc();
if (cdev == NULL) {
printk("分配字符设备驱动对象失败\n");
ret = -EFAULT;
goto OUT1;
}
printk("分配对象空间成功\n");
// 2.初始化对象
cdev_init(cdev, &fops);
// 3.申请设备号
if (major > 0) // 静态指定设备号
{
ret = register_chrdev_region(MKDEV(major, minor), count, "myled");
if (ret) {
printk("静态指定设备号失败\n");
goto OUT2;
}
} else if (major == 0) // 动态申请设备号
{
ret = alloc_chrdev_region(&devno, minor, count, "myled");
if (ret) {
printk("动态申请设备号失败\n");
goto OUT2;
}
// 获取主设备号和次设备号
major = MAJOR(devno);
minor = MINOR(devno);
}
printk("申请设备号成功\n");
// 4.注册字符设备驱动对象
ret = cdev_add(cdev, MKDEV(major, minor), count);
if (ret) {
printk("注册字符设备驱动对象失败\n");
goto OUT3;
}
printk("注册字符设备驱动对象成功\n");
// 向上提交目录
cls = class_create(THIS_MODULE, "myled");
if (IS_ERR(cls)) {
printk("向上提交目录失败\n");
ret = -PTR_ERR(cls);
goto OUT4;
}
printk("向上提交目录成功\n");
// 向上提交设备节点信息
for (i = 0; i < count; i++) {
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
if (IS_ERR(dev)) {
ret = -PTR_ERR(dev);
goto OUT5;
}
}
printk("向上提交设备节点成功\n");
gpiono1 = my_gpio_init(&dnode, "myleds", "led1", 0);
gpiono2 = my_gpio_init(&dnode, "myleds", "led2", 0);
gpiono3 = my_gpio_init(&dnode, "myleds", "led3", 0);
return 0;
OUT5:
// 释放已经申请的设备节点信息
for (--i; i >= 0; i--) {
device_destroy(cls, MKDEV(major, i));
}
// 释放目录空间
class_destroy(cls);
OUT4:
// 注销字符设备驱动对象
cdev_del(cdev);
OUT3:
// 释放设备号
unregister_chrdev_region(MKDEV(major, minor), count);
OUT2:
// 释放对象空间
kfree(cdev);
OUT1:
return ret;
}
static void __exit mycdev_exit(void)
{
// 关灯
gpio_set_value(gpiono1, 0);
gpio_set_value(gpiono2, 0);
gpio_set_value(gpiono3, 0);
// 注销gpio编号
gpio_free(gpiono1);
gpio_free(gpiono2);
gpio_free(gpiono3);
// 销毁设备节点
for (i = 0; i < count; i++) {
device_destroy(cls, MKDEV(major, i));
}
// 释放目录空间
class_destroy(cls);
// 1.注销字符设备驱动对象
cdev_del(cdev);
// 2.释放设备号
unregister_chrdev_region(MKDEV(major, minor), count);
// 3.释放对象空间
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
int mycdev_open(struct inode* inode, struct file* file)
{
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* iof)
{
// 向用户拷贝
if (size > sizeof(kbuf)) {
size = sizeof(kbuf);
}
ret = copy_to_user(ubuf, kbuf, size);
if (ret != 0) {
printk("copy to user filed\n");
return -EIO;
}
return 0;
}
ssize_t mycdev_write(struct file* file, const char __user* ubuf, size_t size, loff_t* iof)
{
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 which; // 用户层传递的第三个参数
ret = copy_from_user(&which, (void*)arg, 4);
if (ret) {
printk("copy_from_user filed\n");
return ret;
}
switch (cmd) {
case 1: // 开灯
switch (which) {
case 1:
gpio_set_value(gpiono1, 1);
break;
case 2:
gpio_set_value(gpiono2, 1);
break;
case 3:
gpio_set_value(gpiono3, 1);
break;
}
break;
case 0: // 关灯
switch (which) {
case 1:
gpio_set_value(gpiono1, 0);
break;
case 2:
gpio_set_value(gpiono2, 0);
break;
case 3:
gpio_set_value(gpiono3, 0);
break;
}
break;
}
return 0;
}
int mycdev_close(struct inode* inode, struct file* file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
main.c
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char const* argv[])
{
int fd = open("/dev/myled0", O_RDWR);
if (fd < 0) {
printf("打开设备文件失败\n");
exit(-1);
}
int on_off; // 开关选项 0(关) 1(开)
int device; // 设备选项 1~3
while (1) {
printf("请输入:1(开) 0(关)>");
scanf("%d", &on_off);
getchar();
if (on_off != 0 && on_off != 1) {
printf("输入错误,请重新输入\n");
continue;
}
printf("请输入控制的设备:1(LED1) 2(LED2) 3(LED3) >");
scanf("%d", &device);
getchar();
if (device != 1 && device != 2 && device != 3) {
printf("设备输入错误,请重新输入\n");
continue;
}
// 根据on_off和device,开关对应的灯
ioctl(fd, on_off, &device);
}
close(fd);
return 0;
}