9.20 作业

myplatform.c

cpp 复制代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/of_gpio.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/cdev.h>

char number = 1;
struct gpio_desc *led1;
unsigned int irqno;
struct timer_list timer;
unsigned int major = 0;
unsigned int minor = 0;
char *devname = "myplatform";
struct cdev *cdev;
struct class *cls;
struct device *dev;

// 封装操作方法
int led_open(struct inode *inode, struct file *file)
{
    return 0;
}
ssize_t led_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
    unsigned long res = copy_to_user(ubuf, &number, size < sizeof(number) ? size : sizeof(number));
    return res;
}
int led_close(struct inode *inode, struct file *file)
{
    return 0;
}
// 定义一个操作方法结构体对象并且初始化
struct file_operations fops = {
    .open = led_open,
    .read = led_read,
    .release = led_close,
};

irqreturn_t key_handler(int irq, void *dev)
{
    number = !number;
    gpiod_set_value(led1, !gpiod_get_value(led1));
    return IRQ_HANDLED;
}

int pdrv_probe(struct platform_device *pdev)
{
    unsigned int dnub;
    printk("%s%s%d\n", __FILE__, __func__, __LINE__);

    cdev = cdev_alloc();
    if (!cdev)
    {
        printk("字符设备驱动对象申请空间失败\n");
        return -1;
    }
    printk("字符设备驱动对象申请空间成功\n");
    cdev_init(cdev, &fops);

    if (alloc_chrdev_region(&dnub, minor, 1, devname))
    {
        printk("动态申请设备号失败\n");
        return -1;
    }
    printk("动态申请设备号成功\n");
    major = MAJOR(dnub);

    if (cdev_add(cdev, MKDEV(major, minor), 1))
    {
        printk("注册驱动失败\n");
        return -1;
    }
    printk("注册驱动成功\n");

    cls = class_create(THIS_MODULE, devname);
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -1;
    }
    printk("向上提交目录成功\n");

    dev = device_create(cls, NULL, MKDEV(major, minor), NULL, devname);
    if (IS_ERR(cls))
    {
        printk("向上提交设备节点信息失败\n");
        return -1;
    }
    printk("向上提交设备节点信息成功\n");

    led1 = gpiod_get_from_of_node(pdev->dev.of_node, "led1-gpio", 0, GPIOD_OUT_HIGH, NULL);
    if (IS_ERR(led1))
    {
        printk("解析GPIO失败\n");
        return -1;
    }
    printk("解析GPIO成功\n");

    irqno = irq_of_parse_and_map(pdev->dev.of_node, 0);
    if (!irqno)
    {
        printk("中断号解析失败\n");
        return -1;
    }
    printk("中断号解析成功\n");

    if (request_irq(irqno, key_handler, IRQF_TRIGGER_FALLING, "key", NULL))
    {
        printk("中断注册失败\n");
        return -1;
    }
    printk("中断注册成功\n");

    return 0;
};

int pdrv_remove(struct platform_device *pdev)
{
    printk("%s%s%d\n", __FILE__, __func__, __LINE__);
    gpiod_set_value(led1, 0);
    gpiod_put(led1);
    device_destroy(cls, MKDEV(major, minor));
    class_destroy(cls);
    cdev_del(cdev);
    unregister_chrdev_region(MKDEV(major, minor), 1);
    kfree(cdev);
    free_irq(irqno, NULL);
    return 0;
}

struct of_device_id oftable[] = {
    {
        .name = "myplatform",
        //.compatible = "hqyj,myplatform",
    },
    {},
};

struct platform_driver pdrv = {
    .probe = pdrv_probe,
    .remove = pdrv_remove,
    .driver = {
        .name = "abcd",
        .of_match_table = oftable,
    },
};

module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

test.c

cpp 复制代码
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "head.h"

int main()
{
    int fd;
    char number;

    fd = open("/dev/myplatform", O_RDWR);
    if (fd < 0)
    {
        printf("文件打开失败\n");
        return -1;
    }

    read(fd,&number,sizeof(number));

    printf("number=%d\n",number);

    close(fd);
    return 0;
}

设备树

结果

相关推荐
云栖梦泽2 分钟前
Linux内核与驱动:5.并发与竞争
linux·c++
牢七6 分钟前
CVE-2022-37202 nday 研究 sql
linux·windows·microsoft
打工人1379号7 分钟前
2K3000常见问题合集
linux·运维·服务器
冰冷的希望10 分钟前
【系统】非虚拟机,物理机安装Ubuntu教程,Windows与Linux(Ubuntu)双系统共存!
linux·windows·ubuntu·系统架构·vmware·双系统·pe系统
minji...29 分钟前
Linux 进程信号(四)内核态&&用户态,sigaction,可重入函数,volatile,SIGCHLD信号
linux·运维·服务器
新兴AI民工29 分钟前
【Linux内核二十九】进程管理模块:CFS调度器check_preempt_wakeup
linux·linux内核·wakeup
lwx91485238 分钟前
Linux-parted命令
linux·运维·服务器
xin_yao_xin44 分钟前
Linux 下 Docker 安装教程(2026)
linux·运维·docker
不愿透露姓名的大鹏1 小时前
Linux环境下Node.js后台运行方式(实用版)
linux·运维·node.js
biubiubiu07061 小时前
Linux 与 Shell 自动化运维基础知识记录
linux·运维·自动化