how to write your first linux device driver

0. environment
-ubuntu 1804 64bit

1. apt-get install linux-headers-$(uname -r)

2. code hello.c

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void)
{
printk(KERN_ALERT "Hello, solidmango\n");
return ;
} static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, solidmango\n");
} module_init(hello_init);
module_exit(hello_exit);

3. Makefile

# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)
obj-m := hello.o
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif

4. sudo insmod hello.ko

5. sudo rmmod hello

6. view result

sm@ubuntu:~/dev$ cat /var/log/syslog|grep solidmango
Sep :: ubuntu kernel: [ 3041.143749] Hello, solidmango
Sep :: ubuntu kernel: [ 3068.192172] Goodbye, solidmango

7. sign your kernel module

/lib/modules/4.15./build/scripts/sign-file sha512 /lib/modules/4.15./build/certs/signing_key.pem /lib/modules/4.15./build/certs/signing_key.x509 hello.ko

signing_key.pem and signing_key.x509 files are generated when build the linux kernel

reference
https://wiki.gentoo.org/wiki/Signed_kernel_module_support#Enabling_module_signature_verification

how to write your first linux device driver的更多相关文章

  1. linux device driver —— 环形缓冲区的实现

    还是没有接触到怎么控制硬件,但是在书里看到了一个挺巧妙的环形缓冲区实现. 此环形缓冲区实际为一个大小为bufsize的一维数组,有一个rp的读指针,一个wp的写指针. 在数据满时写进程会等待读进程读取 ...

  2. Linux Device Driver 学习(1)

    Linux Device Driver 学习(1) 一.搭建虚拟机开发环境 1.选择虚拟机VirtualBox,官网下载.deb包安装: VirtualBox Linux 5.1.6 下载fedora ...

  3. Linux Device Driver && Device File

    catalog . 设备驱动程序简介 . I/O体系结构 . 访问设备 . 与文件系统关联 . 字符设备操作 . 块设备操作 . 资源分配 . 总线系统 1. 设备驱动程序简介 设备驱动程序是内核的关 ...

  4. How to learn linux device driver

    To learn device driver development, like any other new knowledge, the bestapproach for me is to lear ...

  5. <<linux device driver,third edition>> Chapter 4:Debugging Techniques

    Debugging by Printing printk lets you classify messages accoring to their severity by associating di ...

  6. <<linux device driver,third edition>> Chapter 3:Char Drivers

    The Internal Representation of Device Numbers Within the kernel,the dev_t type(defined in linux/type ...

  7. linux device driver —— ioctl

    实现了应用程序和设备驱动通过ioctl通信.还是对设备驱动没什么感觉,贴一下代码吧. 在Ubuntu 16.04 64bit中测试通过 ioctldemo.c #include <linux/m ...

  8. linux device driver —— 字符设备

    现在对linux设备驱动还没有什么认识,跟着书上敲了一个字符驱动,这里把代码贴一下. 测试环境是 Ubuntu 16.04 64bit 驱动程序: #include <linux/fs.h> ...

  9. <<linux device driver,third edition>> Chapter 2: Building and Running Modules

    Kernel Modules Versus Applications Kernel modules programming is similar to event driven programming ...

随机推荐

  1. ActionMq + mqttws3.1 实现持久化订阅

    activemq版本:5.15.3 Eclipse Paho MQTT JavaScript library mqttws3.1:在amq安装目录下webapp-demo目录下可以找到 实现步骤请阅读 ...

  2. Asp.net MVC企业级开发(02)---Log4net

    Log4Net 是用来记录日志的,可以将程序运行过程中的信息输出到一些地方(文件.数据库.EventLog等).日志就是程序的“黑匣子”,可以通过日志查看系统的运行过程,从而发现系统的问题. 日志的作 ...

  3. Android 常用炫酷控件(开源项目)git地址汇总

    第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.P ...

  4. 开发技术--Numpy模块

    开发|Numpy模块 Numpy模块是数据分析基础包,所以还是很重要的,耐心去体会Numpy这个工具可以做什么,我将从源码与 地产呢个实现方式说起,祝大家阅读愉快! Numpy模块提供了两个重要对象: ...

  5. maven 学习---Maven本地资源库

    Maven的本地资源库是用来存储所有项目的依赖关系(插件jar和其他文件,这些文件被Maven下载)到本地文件夹. 很简单,当你建立一个Maven项目,所有相关文件将被存储在你的Maven本地仓库. ...

  6. JavaScript Timing 事件及两种时钟写法

    JavaScript 可以在时间间隔内执行. 这就是所谓的定时事件( Timing Events). ------------------------------------------------- ...

  7. provisional headers are shown 一例

    系统首页的ajax调用出现 报错: provisional headers are shown 最后查到的原因时,mysql数据库的磁盘满了,而首页的ajax调用要插入一条记录到数据库,卡住了.

  8. 【MySQL】自增步长调整

    mysql> show variables like '%increment%'; +-----------------------------+-------+ | Variable_name ...

  9. VSCode在Ubuntu下快捷键和Windows下不一致的解决办法

    Windows下切换前一次和后一次光标位置,用的快捷键是Alt+<-和Alt+->.很遗憾,Ubuntu下并不是这个快捷键.不清楚为什么VSCode不提供统一的快捷键,但对于我来说,我很想 ...

  10. 【jsp】案例:显示商品列表 & 问题:List内添加元素,为什么值都变成一样的了

    代码: 1.Product: package domain; public class Product { /* `pid` varchar(50) NOT NULL, `pname` varchar ...