代码:
pdrv.h
#ifndef __PDRV_H__
#define __PDRV_H__
#define LED1_ON _IO('L',(0x1<<1))
#define LED1_OFF _IO('L',(0x1<<2))
#define LED2_ON _IO('L',(0x1<<3))
#define LED2_OFF _IO('L',(0x1<<4))
#define LED3_ON _IO('L',(0x1<<5))
#define LED3_OFF _IO('L',(0x1<<6))
#endif
pdrv.c
#include <linux/module.h>
#include <linux/platform_device.h> // platform_driver | module_platform_driver
#include <linux/mod_devicetable.h> // of_device_id
#include <linux/of_gpio.h> // gpiod_get_from_of_node
#include <linux/err.h> // IS_ERR | PTR_ERR
#include "pdrv.h"
// platform入口出口函数
int platform_led_probe(struct platform_device *);
int platform_led_remove(struct platform_device *);
// 设备树匹配表[match的OF匹配]
struct of_device_id oftable[] = {
{.compatible = "hqyj,myplatform",},
{/*结束*/},
};
// platform驱动对象
struct platform_driver platform_leds = {
.probe = platform_led_probe,
.remove = platform_led_remove,
.driver = {
.name = "reserved",
.of_match_table = oftable,
},
};
// 模块入口出口函数{一键注册宏[模块入口出口函数]}
module_platform_driver(platform_leds);
MODULE_LICENSE("GPL");
struct gpio_desc *gpiod[3];
char *ledname[3] = {"led1", "led2", "led3"};
#define CNAME "myleds"
#define DNAME "myleds"
int major = 0;
int myopen(struct inode *inode, struct file *file){printk("%s.\n", __func__);return 0;}
int myclose(struct inode *inode, struct file *file){printk("%s.\n", __func__);return 0;}
long myioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
printk("%s.\n", __func__);
switch(MINOR(file->f_inode->i_rdev))
{
case 0:
switch(cmd)
{
case LED1_ON:
printk("LED1_ON.\n");
gpiod_set_value(gpiod[0], 1);
break;
case LED1_OFF:
printk("LED1_OFF.\n");
gpiod_set_value(gpiod[0], 0);
break;
default:
printk("传入cmd错误.\n");
break;
}
break;
case 1:
switch(cmd)
{
case LED2_ON:
printk("LED2_ON.\n");
gpiod_set_value(gpiod[1], 1);
break;
case LED2_OFF:
printk("LED2_OFF.\n");
gpiod_set_value(gpiod[1], 0);
break;
default:
printk("传入cmd错误.\n");
break;
}
break;
case 2:
switch(cmd)
{
case LED3_ON:
printk("LED3_ON.\n");
gpiod_set_value(gpiod[2], 1);
break;
case LED3_OFF:
printk("LED3_OFF.\n");
gpiod_set_value(gpiod[2], 0);
break;
default:
printk("传入cmd错误.\n");
break;
}
break;
default:
printk("传入minor错误.\n");
break;
}
return 0;
}
const struct file_operations fops = {
.open = myopen,
.release = myclose,
.unlocked_ioctl = myioctl,
};
struct class *cls;
struct device *dev[3];
int platform_led_probe(struct platform_device *pdev)
{
int i, ret;
// 1. 解析设备树结点[新版{获取GPIO编号并申请}][gpiod_get_from_of_node]
for(i = 0; i < ARRAY_SIZE(gpiod); i++)
{
if(IS_ERR(gpiod[i] = gpiod_get_from_of_node(pdev->dev.of_node, ledname[i], 0, GPIOD_OUT_LOW, NULL)))
{
printk("解析%s失败.\n", ledname[i]);
ret = -PTR_ERR(gpiod[i]);
goto ERR1;
}
gpiod_set_value(gpiod[i], 1);// 点灯
}
// 2. 注册字符设备驱动[register_chrdev]
if((major = register_chrdev(0, CNAME, &fops)) < 0)
{
printk("注册字符设备驱动失败.\n");
ret = -EAGAIN;
goto ERR2;
}
// 3. 向上提交目录信息[class_create]
if(IS_ERR(cls = class_create(THIS_MODULE, DNAME)))
{
printk("向上提交目录信息失败.\n");
ret = -PTR_ERR(cls);
goto ERR3;
}
// 4. 向上提交设备文件信息[device_create]
for(i = 0; i < ARRAY_SIZE(dev); i++)
{
if(IS_ERR(dev[i] = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i)))
{
printk("向上提交设备文件信息%d失败.\n", i);
ret = -PTR_ERR(dev[i]);
goto ERR4;
}
}
return 0;
ERR4:
for(--i; i >= 0; i--)
{
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
ERR3:
unregister_chrdev(major, CNAME);
ERR2:
i = ARRAY_SIZE(gpiod);
ERR1:
for(--i; i >= 0; i--)
{
gpiod_put(gpiod[i]);
}
return ret;
}
int platform_led_remove(struct platform_device *pdev)
{
int i;
// 4. 灭灯+释放编号
for(i = 0; i < ARRAY_SIZE(gpiod); i++)
{
gpiod_set_value(gpiod[i], 0);
gpiod_put(gpiod[i]);
}
// 3. 移除设备文件信息
for(i = 0; i < ARRAY_SIZE(dev); i++)
{
device_destroy(cls, MKDEV(major, i));
}
// 2. 移除目录信息
class_destroy(cls);
// 1. 注销字符设备驱动
unregister_chrdev(major, CNAME);
return 0;
}
test.c
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <unistd.h>
#include "homework.h"
int main(int argc, const char *argv[])
{
int fds[3] = {0};
char *devname[3] = {"/dev/myled1", "/dev/myled2", "/dev/myled3"};
for(int i = 0; i < 3; i++)
{
if(-1 == (fds[i] = open(devname[i], O_RDWR)))
{
perror("open");
return -1;
}
}
printf("文件打开完毕.\n");
char cmd = 0;
while(1)
{
puts("MENU");
puts("0. QUIT");
puts("1. LED1_ON");
puts("2. LED2_ON");
puts("3. LED3_ON");
puts("4. LED1_OFF");
puts("5. LED2_OFF");
puts("6. LED3_OFF");
scanf("%c", &cmd);
printf("您输入的是:%c\n", cmd);
switch(cmd)
{
case '0':
for(int i = 0; i < 3; i++)
{
close(fds[i]);
}
return 0;
case '1':
ioctl(fds[0], LED1_ON);
break;
case '2':
ioctl(fds[1], LED2_ON);
break;
case '3':
ioctl(fds[2], LED3_ON);
break;
case '4':
ioctl(fds[0], LED1_OFF);
break;
case '5':
ioctl(fds[1], LED2_OFF);
break;
case '6':
ioctl(fds[2], LED3_OFF);
break;
default:
printf("请输入正确的cmd.\n");
break;
}
getchar();
puts("键入回车继续-");
while(getchar() != 10);
system("clear");
}
return 0;
}