有关驱动与应用层数据交互的小例子( 以及驱动 epoll 实现的实现方式 )

发布时间 2023-03-22 21:17:02作者: superconvert

介绍

  1. 演示了一个驱动对应多个设备,以及各个设备的存取
  2. 演示了应用与驱动,mmap 的映射实现与访问
  3. 演示了应用层通过 select, poll, epoll 方式读写设备数据
  4. netlink 的方式待续

driver

  1. 删除驱动 rmmod memdev
  2. 一般内核都会对模块进行签名检查,没好的办法,关闭内核源码的对模块签名,重新编译内核,用新内核启动

app

  1. mmap 是演示应用层与驱动层之间的使用

  2. read 和 write 演示了利用 select, poll, epoll 模型从内核读取东西

  3. netlink_unicast 需要驱动层打开 NETLINK_UNICAST 宏定义

    Compile kernel module and user space program:
    make

    Load kernel module:
    insmod ./netlink_test.ko

    Also check kernel log dmesg for module debug output:
    ./nl_recv "Hello you!"
    Hello you!

    Unload kernel module:
    rmmod netlink_test.ko

  4. netlink_multicast 需要驱动层打开 NETLINK_MULTICAST 宏定义

    Compile kernel module and user space program.
    make

    Load kernel module:
    insmod ./netlink_test.ko

    Also check kernel log dmesg for module debug output.
    ./nl_recv "Hello you!"
    Listen for message...
    Received from kernel: Hello you!
    Listen for message...

    Execute ./nl_recv in another console as well and see how the message is send to the kernel and back to all running nl_recv instances.
    Note: Only root or the kernel can send a message to a multicast group!

    Unload kernel module:
    rmmod netlink_test.ko

  5. netlink_namespace 需要驱动层打开 NETLINK_NAMESPACE 宏定义

    Compile kernel module and user space program.
    make

    Load kernel module:
    insmod ./netlink_test.ko

    Check kernel log dmesg for module debug output.
    ./nl_recv "Hello you!"
    Hello you!

    Unload kernel module:
    rmmod netlink_test.ko

源码下载

随便找个 linux 系统,需要找到对应的内核源码包,去掉对模块的签名校验,安装新内核,以新内核启动系统
本文源码