platform驱动编写

这篇具有很好参考价值的文章主要介绍了platform驱动编写。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

驱动文件

#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>

unsigned int major = 0;
unsigned int minor = 0;
struct gpio_desc *gpioDesc;
struct class *clz;
struct device *dev;
unsigned int condition;
int number;
wait_queue_head_t wq_head;
ssize_t myDev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
    if (file->f_flags & O_NONBLOCK)
    {
    	return -ENXIO;
    }
    else
    {
        wait_event_interruptible(wq_head, condition);
    }
    condition = 0;
    int ret;
    ret = copy_to_user(ubuf, (void *)&number, size);
    if (ret)
    {
        printk("copy_to_user failed!\n");
        return ret;
    }

    return 0;
}
struct file_operations fop = {
    .read = myDev_read};
irqreturn_t my_handler(int irq, void *dev)
{
    gpiod_set_value(gpioDesc, !gpiod_get_value(gpioDesc));
    condition = 1;
    wake_up_interruptible(&wq_head);
    number = number == 0 ? 1 : 0;
    return IRQ_HANDLED;
}

int drv_probe(struct platform_device *d)
{
    printk("%s,%s,%d\n", __FILE__, __func__, __LINE__);
    init_waitqueue_head(&wq_head);
    major = register_chrdev(major, "myDev", &fop);
    if (major < 0)
    {
        printk("注册字符设备失败\n");
        goto OUT1;
    }
    printk("注册字符设备成功\n");
    clz = class_create(THIS_MODULE, "myDev");
    if (IS_ERR(clz))
    {
        printk("向上提交目录失败\n");
        goto OUT2;
    }
    printk("向上提交目录成功\n");
    dev = device_create(clz, NULL, MKDEV(major, minor), NULL, "myDev");
    if (IS_ERR(dev))
    {
        printk("向上提交设备节点信息失败\n");
        goto OUT3;
    }
    printk("向上提交设备节点信息成功\n");
    struct resource *res = platform_get_resource(d, IORESOURCE_MEM, 0);
    if (res == NULL)
    {
        printk("获取设备资源失败\n");
        goto OUT4;
    }
    printk("获取设备资源成功\n");
    int irqNo = platform_get_irq(d, 0);
    if (irqNo < 0)
    {
        printk("获取软中断号失败\n");
        goto OUT4;
    }
    printk("获取软中断号成功\n");
    gpioDesc = gpiod_get_from_of_node(d->dev.of_node, "led1_gpio", 0, GPIOD_OUT_LOW, NULL);
    if (gpioDesc == NULL)
    {
        printk("解析gpio节点失败\n");
        goto OUT4;
    }
    printk("解析gpio节点成功\n");
    int ret = request_irq(irqNo, my_handler, IRQF_TRIGGER_FALLING, "key1", NULL);
    if (ret < 0)
    {
        printk("申请软中断号失败!\n");
        goto OUT5;
    }
    printk("申请软中断号成功!\n");
    return 0;
OUT5:
    gpiod_put(gpioDesc);
OUT4:
    device_destroy(clz, MKDEV(major, minor));
OUT3:
    class_destroy(clz);
OUT2:
    unregister_chrdev(MKDEV(major, minor), "myDev");
OUT1:
    return -1;
}

int drv_remove(struct platform_device *d)
{
    printk("%s,%s,%d\n", __FILE__, __func__, __LINE__);
    gpiod_set_value(gpioDesc, 0);
    gpiod_put(gpioDesc);
    device_destroy(clz, MKDEV(major, minor));
    class_destroy(clz);
    unregister_chrdev(major, "myDev");
    return 0;
}

struct of_device_id idtable[] = {
    {
        .compatible = "hqyj,myplatform",
    },
    {
        .compatible = "hqyj,myplatform1",
    },
    {
        .compatible = "hqyj,myplatform2",
    },
    {}};

struct platform_driver pdrv = {
    .probe = drv_probe,
    .remove = drv_remove,
    .driver = {
        .name = "aaaa",
        .of_match_table = idtable},
};

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

测试文件文章来源地址https://www.toymoban.com/news/detail-680735.html

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "head.h"

int main()
{
    int number;
    int fd = open("/dev/myDev", O_RDWR);
    if (fd < 0)
    {
        perror("open failed!\n");
        exit(-1);
    }
    while(1) {
        read(fd,&number,sizeof(number));
        printf("%d\n",number);
    }
    close(fd);
    return 0;
}

到了这里,关于platform驱动编写的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包