实现了应用程序和设备驱动通过ioctl通信。还是对设备驱动没什么感觉,贴一下代码吧。

在Ubuntu 16.04 64bit中测试通过

ioctldemo.c

#include <linux/module.h>
#include <linux/init.h>
#include <linux/stat.h>
#include <linux/types.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/moduleparam.h>
#include <linux/cdev.h>
#include <asm-generic/uaccess.h>
#include <asm-generic/ioctl.h>
#include <asm-generic/current.h>

#define IOCTLDEMO_MAJOR 0
#define MODULE_NAME "ioctldemo"

#define DEMO_MAGIC 'm'
#define DEMO_SIZE  int
#define DEMO_NR_MAX    1
#define MY_IOCTL_READ _IOR(DEMO_MAGIC,1,DEMO_SIZE);

static int ioctldemo_major = IOCTLDEMO_MAJOR;

void ioctldemo_exit(void);
int  ioctldemo_init(void);
long my_unlocked_ioctl(struct file*, unsigned int, unsigned long);
int  my_cdev_open(struct inode*, struct file*);
int  my_cdev_release(struct inode*,struct file*);

MODULE_LICENSE("Dual BSD/GPL");
module_param(ioctldemo_major,int,S_IRUGO);
module_init(ioctldemo_init);
module_exit(ioctldemo_exit);

struct cdev *my_cdev;

static struct file_operations cdev_ops =
{
    .owner          = THIS_MODULE,
    .open           = my_cdev_open,
    .release        = my_cdev_release,
    .unlocked_ioctl = my_unlocked_ioctl,
};

int __init ioctldemo_init(void)
{
    int ret;
    dev_t devno;
    printk(KERN_NOTICE "=== ioctldemo_init start");
    devno = MKDEV(ioctldemo_major,);
    if(ioctldemo_major)
    {
        printk(KERN_NOTICE "=== ioctldemo_init try register");
        ret = register_chrdev_region(devno,,MODULE_NAME);
    }else
    {
        printk(KERN_NOTICE "=== ioctldemo_init auto register");
        ret = alloc_chrdev_region(&devno,,,MODULE_NAME);
        ioctldemo_major = MAJOR(devno);
    }
    )
    {
        printk(KERN_NOTICE "=== ioctldemo_init register fail");
        return ret;
    }

    my_cdev = cdev_alloc();
    my_cdev->owner = THIS_MODULE;
    my_cdev->ops   = &cdev_ops;
    ret = cdev_add(my_cdev,MKDEV(ioctldemo_major,),);

    )
    {
        printk(KERN_NOTICE "=== ioctldemo_init add cdev fail");
        return ret;
    }

    printk(KERN_NOTICE "=== ioctldemo_init finish");
    ;
}

void __exit ioctldemo_exit(void)
{
    printk (KERN_NOTICE "=== ioctldemo_exit");
    cdev_del(my_cdev);
    unregister_chrdev_region(MKDEV(ioctldemo_major,),);
}

long my_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
    ;
    if(_IOC_TYPE(cmd) != DEMO_MAGIC)
        return -ENOTTY;
    if(_IOC_NR(cmd) > DEMO_NR_MAX)
        return -ENOTTY;
    if(_IOC_DIR(cmd) & _IOC_READ)
        err = !access_ok(VERIFY_WRITE,(void __user*)arg, _IOC_SIZE(cmd));
    if(err)
        return -EFAULT;

    printk(KERN_NOTICE "=== ioctldemo_ioctl current process is: %s, pid is: %d\n",current->comm,current->pid);

    ,(int *)arg);
}

int my_cdev_open(struct inode *node, struct file *filp)
{
    ;
}

int my_cdev_release(struct inode *node, struct file *filp)
{
    ;
}

Makefile

ifneq ($(KERNELRELEASE),)
mymodule-objs := ioctldemo
obj-m := ioctldemo.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  .tmp_versions *.order *.symvers
endif

install.sh

#!/bin/bash

module="ioctldemo"
device="ioctldemo"
name="ioctldemo"

insmod $module.ko

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

 /dev/$device

uninstall.sh

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

file="/dev/$device"

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

echo 'rm module'
/sbin/rmmod $module

测试程序:

#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <asm-generic/ioctl.h>
#include <error.h>

#define CMD _IOR('m',1,int)

int main()
{
    int fd, ret, arg;
    fd = open("/dev/ioctldemo",O_RDWR);
    )
    {
        puts("open fail");
        return fd;
    }
    ret = ioctl(fd,CMD,&arg);
    )
        printf("ioctl result :%d",arg);
    else
        perror("ioctl fail");
    close(fd);
    ;
}

linux device driver —— ioctl的更多相关文章

  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. 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 ...

  4. Linux Device Driver && Device File

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

  5. How to learn linux device driver

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

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

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

  7. <<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 ...

  8. Linux Device Driver 3th 中的一些坑

    linux设备驱动第三版由于年代比较久远,有很多东西已过时.开一贴记录自己发现的一些问题. 4.3.1.4. seq_file接口 此节最后提到用 struct proc_dir_entry* cre ...

  9. linux device driver —— 字符设备

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

随机推荐

  1. 十六进制字符串转化为byte数组

    工作上有这样的需求之前找了好多都不行,好不容易有个可以的赶紧留下来. 原址:http://blog.163.com/roadwalker@126/blog/static/113561841201013 ...

  2. ORACLE 字符串操作

    1 字符串连接   SQL> select 'abc' || 'def' from dual; 'ABC'|------abcdef 2 小写SQL>select lower('ABC01 ...

  3. [BZOJ 1053] [HAOI 2007] 反素数ant

    题目链接:BZOJ 1053 想一想就会发现,题目让求的 1 到 n 中最大的反素数,其实就是 1 到 n 中因数个数最多的数.(当有多于一个的数的因数个数都为最大值时,取最小的一个) 考虑:对于一个 ...

  4. 谈谈在keil下的代码定位

    关于C语言,我们一般都知道对于RAM定位可以用关键字 _at_,但对于程序代码定位往往感到很迷惑, 其实keil中的程序代码定位功能极为强大 Menu: Options for Target 'Tar ...

  5. MFC应用程序创建窗口的过程 good

    MFC应用程序中处理消息的顺序 1.AfxWndProc()      该函数负责接收消息,找到消息所属的CWnd对象,然后调用AfxCallWndProc 2.AfxCallWndProc()  该 ...

  6. Delphi的Owner与Parent可以不一致,而且Owner不是必须存在(一共7个问题) good

    问题1:Owner与Parent不一致:新建一个Form,上面放一个Button1,一个Panel1,然后在Panel1上再放一个Button2,测试结果:procedure TForm1.Butto ...

  7. VS2013中直接浏览网页显示“无法显示此页”的可能原因

    今天在VS2013里面新建了一个空的web应用程序WebApplication1,然后新建了一个 WebForm1.aspx 直接浏览WebForm1.aspx后,网页直接显示: 在网上查询了写资料, ...

  8. 事务Isolation Level 例子详解

    举例分析: 我们有A表, 包含两条数据. Read uncommitted: 假设我们有两个事务 Trans1, Trans2. 它们的操作如下: Trans 1: 更新A1 -> A11, 然 ...

  9. bzoj1816

    这道题不是很难,二分答案+判定即可 注意在一套牌中Joker只能用一次 ..] of longint;     mid,l,r,n,m,i,ans:longint; function check(x: ...

  10. Microsoft Internet Explorer 内存破坏漏洞(CVE-2013-3193)(MS13-059)

    漏洞版本: Microsoft Internet Explorer 6 - 10 漏洞描述: BUGTRAQ ID: 61678 CVE(CAN) ID: CVE-2013-3193 Windows ...