mycdev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
/*myleds{
led1=<&gpioe 10 0>; //&gpioe表示引用的是gpioe控制器 //10表示gpioe10
//0表示默认状态
led2=<&gpiof 10 0>;
led3=<&gpioe 8 0>;
};*/
char kbuf[128] = "";
struct device_node *dnode;
// led1,2,3编号
unsigned int gpiono1, gpiono2, gpiono3;
int minor = 0; // 次设备号
unsigned int major = 500; // 静态指定设备号
dev_t devno;
struct cdev *cdev;
struct class *cls;
struct device *dev;
const int count = 3;
// 定义功能码
#define LED_ON _IOW('l', 1, int) // 开灯
#define LED_OFF _IOW('l', 0, int) // 关灯
int mycdev_open(struct inode *inode, struct file *file)
{
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;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
unsigned long ret;
// 向用户空间读取拷贝
if (size > sizeof(kbuf)) // 用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小
size = sizeof(kbuf);
ret = copy_to_user(ubuf, kbuf, size);
if (ret)
{
printk("copy_to_user filed\n");
return ret;
}
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
unsigned long ret;
// 从用户空间读取数据
if (size > sizeof(kbuf)) // 用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小
size = sizeof(kbuf);
ret = copy_from_user(kbuf, ubuf, size);
if (ret)
{
printk("copy_to_user filed\n");
return ret;
}
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
// ioctl
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int which, ret;
// 将ioctl第三个参数的数值拷贝给which;
ret = copy_from_user(&which, (void *)arg, sizeof(int));
if (ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
// 根据功能码进行硬件功能控制
switch (cmd)
{
case LED_ON:
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 LED_OFF:
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;
}
// 定义操作方法结构体变量并赋值
struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
.unlocked_ioctl = mycdev_ioctl,
};
static int __init mycdev_init(void)
{
int ret = 0, i;
// 1实例化字符设备驱动对象
cdev = cdev_alloc();
if (cdev == NULL)
{
ret = ENOMEM;
goto OUT1;
}
printk("字符设备对象实例化成功\n");
// 2初始化字符设备驱动对象
cdev_init(cdev, &fops);
// 3申请设备号
if (major == 0) // 动态申请设备号
{
ret = alloc_chrdev_region(&devno, minor, 3, "mycdev");
if (ret)
{
printk("动态申请设备号失败\n");
goto OUT2;
}
major = MAJOR(devno);
minor = MINOR(devno);
}
else // 静态指定设备号
{
devno = MKDEV(major, 0);
ret = register_chrdev_region(devno, 3, "mycdev");
if (ret)
{
printk("静态设备号失败\n");
goto OUT2;
}
}
printk("设备号申请成功\n");
// 4.将字符设备驱动对象注册进内核
ret = cdev_add(cdev, devno, 3);
if (ret)
{
printk("注册字符设备驱动失败\n");
goto OUT3;
}
printk("注册字符设备驱动成功\n");
// 5.自动创建设备节点
cls = class_create(THIS_MODULE, "mycdev");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret = -PTR_ERR(cls);
goto OUT4;
}
printk("向上提交目录成功\n");
// 向上提交设备节点s
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mycdev");
if (IS_ERR(dev))
{
printk("向上提交设备信息失败\n");
ret = PTR_ERR(dev);
goto OUT5;
}
printk("自动创建设备节点成功\n");
// 查找myleds节点
dnode = of_find_node_by_name(NULL, "myleds");
if (dnode == NULL)
{
printk("设备树节点解析失败\n");
ret = ENOMEM;
goto OUT5;
}
// of_get_named_gpio 从设备树中解析并获取指定名称的 GPIO 引脚
gpiono1 = of_get_named_gpio(dnode, "led1", 0);
if (gpiono1 < 0)
{
printk("of_get_named_gpio led1 failed\n");
ret = ENOENT;
goto OUT5;
}
gpiono2 = of_get_named_gpio(dnode, "led2", 0);
if (gpiono2 < 0)
{
printk("of_get_named_gpio led2 failed\n");
ret = ENOENT;
goto OUT5;
}
gpiono3 = of_get_named_gpio(dnode, "led3", 0);
if (gpiono3 < 0)
{
printk("of_get_named_gpio led3 failed\n");
ret = ENOENT;
goto OUT5;
}
// gpio_request用于请求并配置特定的 GPIO 引脚,以便在设备驱动程序中对其进行控制和操作
ret = gpio_request(gpiono1, NULL);
if (ret != 0)
{
printk("gpio_request gpiono1 failed\n");
ret = EPERM;
goto OUT5;
}
ret = gpio_request(gpiono2, NULL);
if (ret != 0)
{
printk("gpio_request gpiono2 failed\n");
ret = EPERM;
goto OUT5;
}
ret = gpio_request(gpiono3, NULL);
if (ret != 0)
{
printk("gpio_request gpiono3 failed\n");
ret = EPERM;
goto OUT5;
}
printk("已申请到GPIO使用权\n");
// 设置GPIO管脚为输出
gpio_direction_output(gpiono1, 0);
gpio_direction_output(gpiono2, 0);
gpio_direction_output(gpiono3, 0);
return 0;
OUT5:
// 释放提交成功的设备节点信息
for (i = 0; i < count; i++)
{
device_destroy(cls, MKDEV(major, i));
}
// 销毁目录
class_destroy(cls);
OUT4:
cdev_del(cdev);
OUT3:
unregister_chrdev_region(devno, 3);
OUT2:
kfree(cdev);
OUT1:
return -ret;
}
static void __exit mycdev_exit(void)
{
// 1.销毁设备节点
device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
// 2.注销字符设备驱动
cdev_del(cdev);
// 3.释放设备号
unregister_chrdev_region(MKDEV(major, minor), count);
// 4.释放动态申请的空间
kfree(cdev);
// 用于释放之前通过 gpio_request 函数请求的 GPIO 引脚
gpio_free(gpiono1);
gpio_free(gpiono2);
gpio_free(gpiono3);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
main.c
#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>
#define LED_ON _IOW('l', 1, int) // 开灯
#define LED_OFF _IOW('l', 0, int) // 关灯
int main(int argc, char const *argv[])
{
char buf[128] = {0};
int which, n;
int fd = open("/dev/mycdev", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(EXIT_FAILURE); // 程序退出,返回值非0表示失败
}
while (1)
{
printf("请选择:1(开灯) 0(关灯)--->");
scanf("%d", &n);
if (1 == n)
{
printf("请选择:1(led1) 2(led2) 3(led3)--->");
scanf("%d", &which);
ioctl(fd, LED_ON, &which);
}
else if (0 == n)
{
printf("请选择:1(led1) 2(led2) 3(led3)--->");
scanf("%d", &which);
ioctl(fd, LED_OFF, &which);
}
}
close(fd);
return 0;
}