驱动部分代码
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/of_gpio.h>
#define LEN 3
int i;
dev_t dev;
int major, minor = 0;
struct inode *inode;
static struct cdev *mycdev;
static struct class *myclass;
static struct gpio_desc *des;
static struct device *my_device;
static struct device_node *gpio_node;
struct chrdev_operations *board_status;
static ssize_t read_opr(struct file *file, char __user *buf, size_t size, loff_t *offect)
{
/* 获取引脚的状态 */
int status;
switch (minor)
{
case 0:
status = gpiod_get_value(des);
break;
case 1:
status = gpiod_get_value(des);
break;
case 2:
status = gpiod_get_value(des);
break;
default:
break;
}
copy_to_user(buf,(char *)&status,sizeof(status));
return 0;
}
static ssize_t write_opr(struct file *file, const char __user *buf, size_t size, loff_t *offect)
{
char status;
/* 根据子设备号和写入的值来控制设备 */
inode = file_inode(file);
minor = iminor(inode);
copy_from_user(&status, buf, 1);
switch (minor)
{
case 0:
if (!status)
{
gpiod_set_value(des, 0);
}
else
{
gpiod_set_value(des, 1);
}
break;
case 1:
if (!status)
{
gpiod_set_value(des, 0);
}
else
{
gpiod_set_value(des, 1);
}
break;
case 2:
if (!status)
{
gpiod_set_value(des, 0);
}
else
{
gpiod_set_value(des, 1);
}
break;
default:
break;
}
return 0;
}
static int open_opr(struct inode *inode, struct file *file)
{
/* 初始化设备,根据子设备号来初始化 */
minor = iminor(inode);
switch (minor)
{
case 0:
{
/* 根据子设备号来确定初始化哪个灯,一开始初始化位低电平 */
des = gpiod_get_from_of_node(gpio_node, "led1", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(des))
{
return PTR_ERR(des);
}
}
break;
case 1:
{
des = gpiod_get_from_of_node(gpio_node, "led2", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(des))
{
return PTR_ERR(des);
}
}
break;
case 2:
{
des = gpiod_get_from_of_node(gpio_node, "led3", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(des))
{
return PTR_ERR(des);
}
}
break;
default:
break;
}
return 0;
}
static int release_opr(struct inode *inode, struct file *file)
{
gpiod_put(des);
return 0;
}
static struct file_operations chrdev = {
.owner = THIS_MODULE,
.open = open_opr,
.read = read_opr,
.write = write_opr,
.release = release_opr};
static int __init chrdev_init(void)
{
mycdev = cdev_alloc();
if (!mycdev)
{
printk("cdev alloc failed!\n");
goto LABEL_A;
}
cdev_init(mycdev, &chrdev);
if (alloc_chrdev_region(&dev, 0, 3, "mydrv"))
{
printk("alloc chrdev failed!\n");
goto LABEL_B;
}
major = MAJOR(dev);
minor = MINOR(dev);
if (cdev_add(mycdev, dev, 3))
{
printk("cdev add failed!\n");
goto LABEL_C;
}
myclass = class_create(THIS_MODULE, "myclass");
if (IS_ERR(myclass))
{
goto LABEL_D;
}
for (i = 0; i < LEN; i++)
{
my_device = device_create(myclass, NULL, MKDEV(major, i), NULL, "led%d", i);
if (IS_ERR(my_device))
{
goto LABEL_E;
}
}
gpio_node = of_find_node_by_path("/fsmp1a_leds");
if (!gpio_node)
{
printk("ERRLINE:%d;find node is failed!\n", __LINE__);
goto LABEL_E;
}
return 0;
LABEL_E:
for (--i; i >= 0; i--)
{
device_destroy(myclass, dev);
}
class_destroy(myclass);
LABEL_D:
cdev_del(mycdev);
LABEL_C:
unregister_chrdev_region(dev, 3);
LABEL_B:
kfree(mycdev);
LABEL_A:
return -1;
}
static void __exit chrdev_exit(void)
{
kfree(mycdev);
for (--i; i >= 0; i++)
{
device_destroy(myclass, dev);
}
class_destroy(myclass);
cdev_del(mycdev);
unregister_chrdev_region(dev, 3);
}
module_init(chrdev_init);
module_exit(chrdev_exit);
MODULE_LICENSE("GPL");
测试部分代码
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#define ERR_MSG(err) \
do \
{ \
fprintf(stderr, "ERR_LINE:%d\n", __LINE__); \
perror(err); \
} while (0)
int main(int argc, const char **argv)
{
if (argc != 3)
{
printf("try: <%s /dev/ledx on|off>\n", argv[0]);
return -1;
}
int fd = open(argv[1], O_RDWR);
if (fd < 0)
{
ERR_MSG("open");
return -1;
}
if (!strcmp("on", argv[2]))
{
char val = 1;
int num;
write(fd, &val, 1);
read(fd,&num,sizeof(num));
printf("读取到的引脚状态为:%d\n",num);
}
else
{
char val = 0;
write(fd, &val, 1);
}
close(fd);
return 0;
}
实验现象