mycdev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/uaccess.h>
// 定义功能码
#define LED_ON _IOW('l', 1, int) // 开灯
#define LED_OFF _IOW('l', 0, int) // 关灯
struct device_node *dnode;
unsigned int gpiono1;
unsigned int gpiono2;
unsigned int gpiono3;
struct cdev *cdev;
char kbuf[128] = {0};
unsigned int minor = 0;
#if 0
unsigned int major=0;
#else
unsigned int major = 500;
#endif
dev_t devno;
struct class *cls;
struct device *dev;
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%d\n", __LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
printk("%d\n", __LINE__);
unsigned long ret;
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_to_user(ubuf, kbuf, size);
if (ret)
{
printk("向用户拷贝失败\n");
return ret;
}
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 failed\n");
return ret;
}
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;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
// 定义操作方法结构体变量并赋值
struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
int i, ret;
// 通过名字解析设备树节点
dnode = of_find_node_by_name(NULL, "myleds");
if (dnode == NULL)
{
printk("设备树节点解析失败\n");
return -ENOMEM;
}
// 根据解析到的设备树节点信息结构体解析出GPIO编号
gpiono1 = of_get_named_gpio(dnode, "led1", 0);
if (gpiono1 < 0)
{
printk("解析LED1的GPIO编号失败\n");
return -EIO;
}
gpiono2 = of_get_named_gpio(dnode, "led2", 0);
if (gpiono2 < 0)
{
printk("解析LED2的GPIO编号失败\n");
return -EIO;
}
gpiono3 = of_get_named_gpio(dnode, "led3", 0);
if (gpiono3 < 0)
{
printk("解析LED3的GPIO编号失败\n");
return -EIO;
}
// 申请gpio编号使用权
gpio_request(gpiono1, NULL);
gpio_request(gpiono2, NULL);
gpio_request(gpiono3, NULL);
// 设置GPIO方向为输出并默认输出低电平
gpio_direction_output(gpiono1, 0);
gpio_direction_output(gpiono2, 0);
gpio_direction_output(gpiono3, 0);
cdev = cdev_alloc();
if (cdev == NULL)
{
ret = -1;
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);
printk("major=%d,minor=%d\n", major, minor);
}
else
{
devno = MKDEV(major, 0);
ret = register_chrdev_region(devno, 3, "mycdev");
if (ret)
{
printk("静态申请失败\n");
goto OUT2;
}
printk("major=%d\n", major);
}
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");
for (i = 0; i < 3; i++)
{
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "mycdev%d", i);
if (IS_ERR(dev))
{
printk("向上提交设备信息失败\n");
ret = -PTR_ERR(dev);
goto OUT5;
}
}
printk("自动创建成功\n");
return 0;
// 考虑上述不同情况失败的释放资源问题,例如跳转goto
OUT5:
for (--i; i >= 0; 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)
{
//灭灯
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);
// 1.销毁设备节点信息
for (int i = 0; i < 3; i++)
{
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
// 2.注销字符设备驱动对象
cdev_del(cdev);
// 3.释放设备号
unregister_chrdev_region(devno, 3);
// 4.释放对象空间
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
testcdev.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.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, const char *argv[])
{
char buf[128] = {0};
int fd = open("/dev/mycdev0", O_RDWR);
if (fd < 0)
{
printf("打开文件失败\n");
exit(-1);
}
int a, b;
while (1)
{
printf("请输入要实现的功能:1(开灯) 0(关灯)>");
scanf("%d", &a);
if (a == 1) // 开灯
{
printf("请输入要操作的灯:1(LED1) 2(LED2) 3(LED3)>");
scanf("%d", &b);
ioctl(fd, LED_ON, &b);
}
else // 关灯
{
printf("请输入要操作的灯:1(LED1) 2(LED2) 3(LED3)>");
scanf("%d", &b);
ioctl(fd, LED_OFF, &b);
}
}
close(fd);
return 0;
}