现在对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. js实现中文转拼音

    首先需要注意ES6在严格模式下中常量太长会出问题,CHAR_DICT.FULL_DICT.POLYPHONE都是很大的常量,所以我都外部加载了,否则编译运行会有问题,先贴代码,常量在最后,如下: js ...

  2. 应用git(SSH设置)

    git配置完成email和user后,才可以生成SSH Key. $ git config --global user.name "自定义名称" $ git config --gl ...

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

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

  4. java 伪共享

    MESI协议及RFO请求典型的CPU微架构有3级缓存, 每个核都有自己私有的L1, L2缓存. 那么多线程编程时, 另外一个核的线程想要访问当前核内L1, L2 缓存行的数据, 该怎么办呢?有人说可以 ...

  5. Ecshop开发

    http://www.cnblogs.com/xcxc/category/579565.html

  6. Principles of Motion Sensing

    Principlesof Motion Sensing Various sensors capable of detecting motionin free space have been comme ...

  7. Delphi常用排序

    1.冒泡排序 Delphi/Pascal code   ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 procedure BubbleSort(var x:a ...

  8. Android+struts2+JSON方式的手机开发(Login)

    在手机的后台服务无论是调用WebService还是Http请求,多数都是采用Android的HttpClient实现相关的调用实现.本文实现Android+Struts2+JSON方式实现为手机前台提 ...

  9. servlet单例多线程

    Servlet如何处理多个请求访问? Servlet容器默认是采用单实例多线程的方式处理多个请求的: 1.当web服务器启动的时候(或客户端发送请求到服务器时),Servlet就被加载并实例化(只存在 ...

  10. 使用m2eclipse搭建Web项目

    这篇文章已经指导好了:http://www.cnblogs.com/quanyongan/archive/2013/04/21/3033838.html