• 一个linux最简otg驱动源代码


    /*

    加载内核模块:sudo insmod otg.ko

    卸载内核模块:sudo rmmod otg

    列举内核模块: lsmod | grep otg 查看信息;如下图所示。

    编译:

    make -C /lib/modules/4.19.161-kernel/build M=/home/appninja/driver modules

    Makefile中:

    make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules

    */

    #include <linux/module.h>

    #include <linux/fs.h>

    #include <linux/proc_fs.h>

    #include <linux/version.h>

    #include <linux/kernel.h>

    #include <linux/init.h>

    static struct proc_dir_entry *proc_otg_entry = NULL;

    static ssize_t proc_otg_read(struct file *filp, char __user *buf, size_t count, loff_t *offp)

    {

        int n = 0, ret;

        char secrets[100];

        sprintf(secrets, "kernel secrets %s\n", filp->f_path.dentry->d_iname);

        n = strlen(secrets);

        if(*offp < n)

        {

            *offp = n+1;

            ret = n+1;

        }

        else

            ret = 0;

        return ret;

    }

    static ssize_t proc_otg_write(struct file *file, const char __user *buffer,

        size_t count, loff_t *data)

    {

        char cmd[100] = { 0x00 };

        int i = 0;

        if (count < 1)

        {

            printk("count <=1\n");

            return -1;

        }

        if (count > sizeof(cmd))

        {

            printk("count > sizeof(cmd)\n");

            return -2;

        }

        return i;

    }

    #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)

    static const struct proc_ops proc_otg_fops = {

     .proc_read = proc_otg_read,

     .proc_write = proc_otg_write,

    };

    #else

    static const struct file_operations proc_otg_fops = {

     .owner = THIS_MODULE,

     .read  = proc_otg_read,

     .write = proc_otg_write,

    };

    #endif

    static int __init otg_init(void)

    {

        proc_otg_entry = proc_create("otg_usb", 0, NULL, &proc_otg_fops);

        return 0;

    }

    static void __exit otg_exit(void)

    {

        if(proc_otg_entry)

            proc_remove(proc_otg_entry);

    }

    module_init(otg_init);

    module_exit(otg_exit);

    MODULE_LICENSE("GPL");

  • 相关阅读:
    ubantu搭建cdh6(自己留的草稿版本)
    SD_DATA_RECEIVE_SHIFT_REGISTER
    fmllr--学习笔记
    单日直播GMV破亿,爆品热销628w+,8月榜单有哪些看点?
    Java设计模式之外观模式
    kubernetes-Pod
    力扣C++学习笔记——C++ assign全面解析
    Java线程安全
    荔枝海盐的香味
    Oracle上process爆满,session数远远低于process数
  • 原文地址:https://blog.csdn.net/a2831942318/article/details/125483590