ubuntu编译字符设备

yizhihongxing

前言

创建一个简单的字符设备驱动程序。

​ 本文命令的运行基本上都需要root权限,使用root账号,或者在命令前面加上sudo。

​ 如果你使用ssh远程连接的服务器进行代码编写。那么不要在root用户下创建文件或者文件夹。这会导致你ssh连接vscode编写代码的权限问题。可以在普通用户创建好所有的文件,然后编写。

代码

驱动程序

hello_driver.c

#include <linux/types.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/cdev.h>
 
 
dev_t hello_devid;
struct cdev hello_cdev;
int hello_major = 0;
int hello_minor;
 
 
uint8_t kernel_buffer[1024] = {0};
static struct class *hello_class;
 
 
 
static int hello_world_open(struct inode * inode, struct file * file)
{
	printk("hello_world_open\r\n");
	return 0;
}
 
static int hello_world_release (struct inode * inode, struct file * file)
{
	printk("hello_world_release\r\n");
	return 0;
}
 
static ssize_t hello_world_read (struct file * file, char __user * buffer, size_t size, loff_t * ppos)
{
	printk("hello_world_read size:%ld\r\n",size);
	copy_to_user(buffer,kernel_buffer,size);
	return size;
}
 
static ssize_t hello_world_write(struct file * file, const char __user * buffer, size_t size, loff_t *ppos)
{
	printk("hello_world_write size:%ld\r\n",size);
	copy_from_user(kernel_buffer,buffer,size);
	return size;
}
 
 
static const struct file_operations hello_world_fops = {
	.owner		= THIS_MODULE,
	.open		= hello_world_open,
	.release = hello_world_release,
	.read		= hello_world_read,
	.write	= hello_world_write,
};
 
 
static int __init hello_driver_init(void)
{
	int ret;
	printk("hello_driver_init\r\n");
 
	alloc_chrdev_region(&hello_devid, 0, 1, "hello");
	hello_major = MAJOR(hello_devid);
	hello_minor = MINOR(hello_devid);
	printk("hello driver major=%d,minor=%d\r\n",hello_major, hello_minor);	
 
	hello_cdev.owner = THIS_MODULE;
	cdev_init(&hello_cdev, &hello_world_fops);
	cdev_add(&hello_cdev, hello_devid, 1);
	
	hello_class = class_create(THIS_MODULE,"hello_class");
 
	device_create(hello_class,NULL,hello_devid,NULL,"hello"); /* /dev/hello */
 
	return 0;
}
 
static void __exit hello_driver_cleanup(void)
{
	printk("hello_driver_cleanup\r\n");
	cdev_del(&hello_cdev);
	unregister_chrdev_region(hello_devid, 1);
	
	device_destroy(hello_class,hello_devid);
	class_destroy(hello_class);
	
}
 
 
module_init(hello_driver_init);
module_exit(hello_driver_cleanup);
MODULE_LICENSE("GPL");

Makefile文件

KERNELDIR := /lib/modules/$(shell uname -r)/build
CURRENT_PATH := $(shell pwd)
obj-m := hello_driver.o

KBUILD_CFLAGS   += -Wno-unused-result -Wno-unused-variable
build: kernel_modules
 
kernel_modules:
	$(MAKE) ${CFLAGS} -C $(KERNELDIR) M=$(CURRENT_PATH) modules
	$(CROSS_COMPILE)gcc -o test_app test_app.c
clean:
	$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean
	rm -rf test_app

测试程序

test_app.c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

uint8_t buffer[512] = {0};
 
int main(int argc, char *argv[])
{
	int fd;
	int ret;
	
	fd  = open(argv[1], O_RDWR);
    if(!fd)
    {
        printf("everthing is error\n");
    }
 
	if(!strcmp("read",argv[2]))
	{
		printf("read data from kernel\r\n");
		ret = read(fd,buffer,sizeof(buffer));
		printf("ret len:%d data:%s\r\n",ret,buffer);
	}
 
	if(!strcmp("write",argv[2]))
	{
		printf("write data to kernel %s len:%ld\r\n",argv[3],strlen(argv[3]));
		ret = write(fd,argv[3],strlen(argv[3]));
		printf("ret len:%d\r\n",ret);
	}
 
	
	close(fd);
 
	
}

编译

执行make命令编译

root@ubuntu:/home/dong/workspace/drivercode# make
make  -C /lib/modules/5.19.0-38-generic/build M=/home/dong/workspace/drivercode modules
make[1]: Entering directory '/usr/src/linux-headers-5.19.0-38-generic'
warning: the compiler differs from the one used to build the kernel
  The kernel was built by: x86_64-linux-gnu-gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
  You are using:           gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
  CC [M]  /home/dong/workspace/drivercode/hello_driver.o
  MODPOST /home/dong/workspace/drivercode/Module.symvers
  CC [M]  /home/dong/workspace/drivercode/hello_driver.mod.o
  LD [M]  /home/dong/workspace/drivercode/hello_driver.ko
  BTF [M] /home/dong/workspace/drivercode/hello_driver.ko
Skipping BTF generation for /home/dong/workspace/drivercode/hello_driver.ko due to unavailability of vmlinux
make[1]: Leaving directory '/usr/src/linux-headers-5.19.0-38-generic'
gcc -o test_app test_app.c

会生成hello_driver.ko文件和test_app

加载模块

insmod hello_driver.ko

卸载模块

rmmod hello_driver

查看模块

lsmod |grep hello_driver

运行测试

创建设备文件

这个文件不是随便创造的,会分配设备号,如果不使用分配的设备号创建会出现读写错误。

​ 每一次加载或者卸载模块都会有dmesg信息。

​ 使用dmesg -c清除信息。加载模块,使用dmesg查看模块信息。

root@ubuntu:/home/dong/workspace/drivercode# dmesg
[164284.337396] hello_driver_init
[164284.337399] hello driver major=238,minor=0

我的设备主设备号就是238,从设备号是0

创建设备文件命令

mknod /dev/hello c 238 0

测试

读 : ./test_app /dev/hello read

写: ./test_app /dev/hello write 需要写入的内容

原文链接:https://www.cnblogs.com/wdgray/p/17337041.html

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:ubuntu编译字符设备 - Python技术站

(0)
上一篇 2023年4月22日
下一篇 2023年4月22日

相关文章

  • Linux:进程模型和进程管理

    在Linux系统中,执行一个程序或命令就可以触发一个进程,系统会给予这个进程一个ID,称为PID,同时根据触发这个进程的用户与相关属性关系,基于这个PID一组有效的权限设置。举个常见的例子,我们要操作系统的时候通常是利用ssh连接程序或直接在主机上登录,然后获取shell。默认的shell是bash,对应的路径为/bin/bash,那么同时间的每个人登录都是…

    Linux 2023年4月13日
    00
  • arm linux下shell gpio点灯

    我使用的lichee Pi 芯片是全志的v3s。摸索一下linux的点灯。 我的代码 #!/bin/sh echo “led run!” echo 36 > /sys/class/gpio/unexport echo 37 > /sys/class/gpio/unexport echo 36 > /sys/class/gpio/export…

    Linux 2023年4月13日
    00
  • Linux系统–初识shell

    访问命令行(command line interface,CLI) Linux控制台 1)Linux系统启动时,会自动创建多个虚拟控制台。虚拟控制台是运行在Linux系统内存中的终端会话。多数Linux发行版会启动5~6个 2)通常必须按下Ctrl+Alt组合键,然后再按一个功能键(F1~F7)来进入你要使用的虚拟控制台。功能键F2键会生成虚拟控制台2,F3…

    Linux 2023年4月18日
    00
  • linux系统Ansible自动化运维部署方法

    Linux系统Ansible自动化运维部署方法 Ansible是一种用于自动化IT工具的开源软件,它可以协调管理节点和远程节点上的程序。借助Ansible,运维人员可以部署、管理和升级IT应用程序和系统。以下是使用Ansible自动化运维部署的基本步骤: 步骤1:安装Ansible 在Linux中,你可以通过以下命令安装Ansible: $ sudo yum…

    Linux 2023年5月14日
    00
  • Linux open命令

    Linux 中的 open 命令是一个用于打开文件或目录的命令。它可以在 Linux 系统中打开文件或目录,并使用默认的应用程序来处理它们。在 Linux 中,通常会使用 open 命令来打开文件或目录,以便用户可以方便地查看和编辑它们。 使用 open 命令的基本语法如下: open [options] file 其中,file 是要打开的文件或目录的名称…

    Linux 2023年5月11日
    00
  • Linux svn的搭建与使用(图文详解)

    Linux SVN的搭建与使用 本篇攻略旨在帮助各位Linux用户了解如何在Linux系统上搭建SVN服务器,并学习如何使用SVN进行版本控制。 1. 安装SVN服务器 1.1 安装SVN服务端 在Linux系统上安装SVN服务端,需要使用以下命令: sudo apt-get update sudo apt-get install subversion 1.…

    Linux 2023年5月14日
    00
  • 详解Linux Centos7下安装Python的方法

    下面是详解LinuxCentos7下安装Python的方法: 1. 先安装必要的编译工具和库文件 在终端中输入以下命令: sudo yum groupinstall -y "Development Tools" sudo yum install -y zlib-devel bzip2-devel openssl-devel ncurses…

    Linux 2023年5月14日
    00
  • Linux中QQ软件的安装和配置

    当然可以,以下是“Linux中QQ软件的安装和配置”的完整攻略,希望能对你有所帮助。 安装Wine 在Linux系统中安装QQ需要安装Windows模拟器Wine,执行以下命令安装: sudo apt install wine 安装完成后可以通过以下命令查看Wine的版本信息: wine –version 下载QQ安装程序 有些不正经的第三方网站提供了QQ…

    Linux 2023年5月14日
    00
合作推广
合作推广
分享本页
返回顶部