现在对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. ARM的STRB和LDRB指令分析

    一.SDRAM 1.存储结构 SDRAM的内部是一个存储阵列.阵列就如同表格一样,将数据“填”进去.在数据读写时和表格的检索原理一样,先指定一个行(Row),再指定一个列 (Column),我们就可以 ...

  2. Java 高效检查一个数组中是否包含某个值

    如何检查一个数组(未排序)中是否包含某个特定的值?在Java中,这是一个非常有用并又很常用的操作.同时,在StackOverflow中,有时一个得票非常高的问题.在得票比较高的几个回答中,时间复杂度差 ...

  3. bzoj 1079: [SCOI2008]着色方案 DP

    1079: [SCOI2008]着色方案 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 803  Solved: 512[Submit][Status ...

  4. Java语言基础(一) 标识符

    Java标识符的问题: ①不可以以数字开头 int 123number = 0; //错误 ②可以使用任意的货币符号(¥和$等等)中文也可以  int $i = 0; //正确 int ¥i = 1; ...

  5. 在linux中使用php将word文档转为pdf

    使用本教程需要在linux中安装openoffice,改页面中有详细的安装与使用教程(http://www.cnblogs.com/sustudy/p/3999628.html). 既然,你看了该教程 ...

  6. QVariant类学习(非常强大的类型,甚至能处理QMap<QString ,QVariant>)

    详细描述: QVariant类作为一个最为普遍的Qt数据类型的联合. 因为c++禁止没有构造函数和析构函数的联合体,许多继承的Qt类不能够在联合体当中使用.(联合体当中的变量共用一个存储区),没有了联 ...

  7. LeetCode解题报告:LRU Cache

    LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should suppo ...

  8. 【EJS】

    // 用=号输出,就会被escapge转义编码 <%= VARIABLE_NAME %> // 用“-”输出原始内容, 不会被escape <%- VARIABLE_NAME %&g ...

  9. Connection 和Dispose的学习日志

  10. java生成随机整数

    1. 使用Random类的nextInt方法: Random rand = new Random(); rand.nextInt(max);, 此时输出[0,max),注意右边是开区间,如果需要设定最 ...