现在对linux设备驱动还没有什么认识,跟着书上敲了一个字符驱动,这里把代码贴一下.

测试环境是 Ubuntu 16.04 64bit

驱动程序:

#include <linux/fs.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>

#define CDEVDEMO_MAJOR 0
#define BUFFER_SIZE 512

static int cdevdemo_major = CDEVDEMO_MAJOR;
static char msgbuff[BUFFER_SIZE];

void cdevdemo_exit(void);
int  cdevdemo_init(void);

MODULE_LICENSE("Dual BSD/GPL");
module_param(cdevdemo_major,int,S_IRUGO);
//注册初始化方法 module_init(cdevdemo_init);
//注册退出方法 module_exit(cdevdemo_exit); struct cdev *my_cdev; ssize_t cdev_write(struct file *, const char __user *, size_t, loff_t *); ssize_t cdev_read (struct file *, char __user *, size_t, loff_t *); int cdev_open (struct inode *,struct file *); int cdev_release (struct inode *,struct file *); struct file_operations my_fops = { .owner = THIS_MODULE, .open = cdev_open, .release = cdev_release, .read = cdev_read, .write = cdev_write, }; void init_cdev(void) { int ret; my_cdev = cdev_alloc(); my_cdev->owner = THIS_MODULE; my_cdev->ops = &my_fops; //添加字符设备 ret = cdev_add(my_cdev,MKDEV(cdevdemo_major,),); ) { printk(KERN_NOTICE "=== cdev_add fail"); } printk(KERN_NOTICE "=== init_cdev finish"); } int __init cdevdemo_init(void) { int ret; dev_t devno; printk(KERN_NOTICE "=== cdevdemo_init 0"); devno = MKDEV(cdevdemo_major,); if(cdevdemo_major) { printk(KERN_NOTICE "=== cdevdemo_init try register"); ret = register_chrdev_region(devno,,"cdevdemo"); }else { printk(KERN_NOTICE "=== cdevdemo_init auto register"); ret = alloc_chrdev_region(&devno,,,"cdevdemo"); cdevdemo_major = MAJOR(devno); } ) { printk(KERN_NOTICE "=== cdevdemo_init register fail"); return ret; } init_cdev(); printk(KERN_NOTICE "=== cdevdemo_init finish"); ; } void __exit cdevdemo_exit(void) { printk (KERN_NOTICE "=== cdevdemo_exit"); //去除字符设备 cdev_del(my_cdev); unregister_chrdev_region(MKDEV(cdevdemo_major,),); } ssize_t cdev_write(struct file *filp, const char __user *buf, size_t count, loff_t *offp) { int ret; printk(KERN_NOTICE "=== cdev_write"); ret = copy_from_user(msgbuff,buf,count%BUFFER_SIZE); ) { printk(KERN_NOTICE "=== cdev_write copy_from_user fail %d",ret); return -EFAULT; } msgbuff[count] = '-'; msgbuff[count+] = '-'; msgbuff[count+] = 'k'; msgbuff[count+] = 'e'; msgbuff[count+] = 'r'; msgbuff[count+] = 'n'; msgbuff[count+] = 'e'; msgbuff[count+] = 'l'; msgbuff[count+] = '\0'; printk(KERN_NOTICE "--- cdev_write : %s",msgbuff); return count%BUFFER_SIZE; } ssize_t cdev_read (struct file *filp, char __user *buf, size_t count, loff_t *offp) { int ret; printk(KERN_NOTICE "=== cdev_read"); ret = copy_to_user(buf,msgbuff,count%BUFFER_SIZE); ) { printk(KERN_NOTICE "=== cdev_read copy_to_user fail %d",ret); return -EFAULT; } printk(KERN_NOTICE "--- cdev_read :%s",msgbuff); return count%BUFFER_SIZE; } int cdev_open (struct inode *inode,struct file *filp) { printk(KERN_NOTICE "=== cdev_open"); ; } int cdev_release (struct inode *inode,struct file *filp) { printk(KERN_NOTICE "=== cdev_release"); ; }

Makefile

ifneq ($(KERNELRELEASE),)
mymodule-objs := cdev
obj-m := cdev.o
else
PWD  := $(shell pwd)
KVER ?= $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
all:
    $(MAKE) -C $(KDIR) M=$(PWD)
clean:
    rm -rf *.cmd *.o *.mod.c *.ko .tmp_versions
endif

install.sh

#!/bin/bash

module="cdev"
device="cdev"
name="cdevdemo"

insmod $module.ko

 ]
then
    exit
fi
major=$(awk "{if(\$2==\"$name\"){print \$1}}"  /proc/devices)

 /dev/$device

uninstall.sh

#!/bin/bash
module="cdev"
device="cdev"

file="/dev/$device"

if [ -e $file ]
then
    rm -rf /dev/$device
    echo 'rm device'
fi

echo 'rm module'
/sbin/rmmod $module

测试程序

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

int main(void)
{
    int fd,ret;
    ] = "hello I'm from user";
    ] = {};
    fd = open("/dev/cdev",O_RDWR);
    )
    {
        puts("open fail");
        ;
    }
    ret = write(fd,buff,strlen(buff));
    printf("write ret :%d\n",ret);
    ret = read(fd,rbuff,);
    printf("read  ret :%d\n%s",ret,rbuff);
    ;
}

运行结果:

write ret :
read  ret :
hello I'm from user--kernel

linux device driver —— 字符设备的更多相关文章

  1. Linux驱动设计——字符设备驱动(一)

    Linux字符设别驱动结构 cdev结构体 struct cdev { struct kobject kobj; struct module *owner; const struct file_ope ...

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

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

  3. Linux Device Driver 学习(1)

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

  4. how to write your first linux device driver

    how to write your first linux device driver 0. environment-ubuntu 1804 64bit 1. apt-get install linu ...

  5. linux driver ------ 字符设备驱动 之 “ 创建设备节点流程 ”

    在字符设备驱动开发的入门教程中,最常见的就是用device_create()函数来创建设备节点了,但是在之后阅读内核源码的过程中却很少见device_create()的踪影了,取而代之的是device ...

  6. Linux Device Driver && Device File

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

  7. 【Linux驱动】字符设备驱动

    一.linux系统将设备分为3类:字符设备.块设备.网络设备.使用驱动程序: 1.字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据,读取数据需要按照先后数据.字符设备是面 ...

  8. 手把手教Linux驱动3-之字符设备架构详解,有这篇就够了

    一.Linux设备分类 Linux系统为了管理方便,将设备分成三种基本类型: 字符设备 块设备 网络设备 字符设备: 字符(char)设备是个能够像字节流(类似文件)一样被访问的设备,由字符设备驱动程 ...

  9. Linux学习 :字符设备框架

    一.系统功能框架: U-boot : 启动内核 linux kernel: 启动应用 应用: open,read,write 都是通过C库实现,汇编就相当于swi val,引发中断,通过系统调用接口在 ...

随机推荐

  1. Linux下的echo服务器

    epoll模式下的echo服务器,忘记从哪个网页上粘贴过来的了,学习一下 /* * main.cc * * Created on: 2009-11-30 * Author: liheyuan * De ...

  2. golang_protobuf环境搭建

    搭建golang使用rotobuf使用环境 一 安装protobuf: 1 下载protobuf源码:https://github.com/google/protobuf 2 进入源码目录: ./au ...

  3. C#Lambda表达式学习日记

    Lambda表达式只是用更简单的方式来写匿名方法,彻底简化了对.NET委托类型的使用. 现在,如果我们要使用泛型 List<> 的 FindAll() 方法,当你从一个集合去提取子集时,可 ...

  4. bzoj 3624: [Apio2008]免费道路 生成树的构造

    3624: [Apio2008]免费道路 Time Limit: 2 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 111  Solved: 4 ...

  5. 初识EL表达式

    1.EL最初出现在JSTL,后来引入JSP 2.核心作用:减少JSp中Java代码数量,同时方便修改 3.算术.逻辑.关系符号都是两种,防止出现歧义,比如:/和div,%和mod,>=和ge,相 ...

  6. C/C++中的内存对齐 C/C++中的内存对齐

    一.什么是内存对齐.为什么需要内存对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特 定的内存地址 ...

  7. 【HDOJ】1325 Is It A Tree?

    并查集.需要考虑入度. #include <stdio.h> #include <string.h> #define MAXNUM 10005 int bin[MAXNUM]; ...

  8. [LeetCode#212]Word Search II

    Problem: Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...

  9. Linux Shell编程(14)——内部变量

    内建变量影响Bash脚本行为的变量.$BASHBash二进制程序文件的路径 bash$ echo $BASH /bin/bash$BASH_ENV该环境变量保存一个Bash启动文件路径,当启动一个脚本 ...

  10. 关于java、Android中Math的一些用法

    java.math.Math类常用的常量和方法: Math.PI 记录的圆周率Math.E记录e的常量Math.abs 求绝对值Math.sin 正弦函数 Math.asin 反正弦函数Math.co ...