linux device driver —— ioctl
实现了应用程序和设备驱动通过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的更多相关文章
- linux device driver —— 环形缓冲区的实现
还是没有接触到怎么控制硬件,但是在书里看到了一个挺巧妙的环形缓冲区实现. 此环形缓冲区实际为一个大小为bufsize的一维数组,有一个rp的读指针,一个wp的写指针. 在数据满时写进程会等待读进程读取 ...
- Linux Device Driver 学习(1)
Linux Device Driver 学习(1) 一.搭建虚拟机开发环境 1.选择虚拟机VirtualBox,官网下载.deb包安装: VirtualBox Linux 5.1.6 下载fedora ...
- 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 ...
- Linux Device Driver && Device File
catalog . 设备驱动程序简介 . I/O体系结构 . 访问设备 . 与文件系统关联 . 字符设备操作 . 块设备操作 . 资源分配 . 总线系统 1. 设备驱动程序简介 设备驱动程序是内核的关 ...
- How to learn linux device driver
To learn device driver development, like any other new knowledge, the bestapproach for me is to lear ...
- <<linux device driver,third edition>> Chapter 4:Debugging Techniques
Debugging by Printing printk lets you classify messages accoring to their severity by associating di ...
- <<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 ...
- Linux Device Driver 3th 中的一些坑
linux设备驱动第三版由于年代比较久远,有很多东西已过时.开一贴记录自己发现的一些问题. 4.3.1.4. seq_file接口 此节最后提到用 struct proc_dir_entry* cre ...
- linux device driver —— 字符设备
现在对linux设备驱动还没有什么认识,跟着书上敲了一个字符驱动,这里把代码贴一下. 测试环境是 Ubuntu 16.04 64bit 驱动程序: #include <linux/fs.h> ...
随机推荐
- double类型字符串转换成一个纯数字字符串和一个小数点位数的c++代码
今天工作中遇到一个要不一个double型的字符串转换成一个纯字数字符串和一个标志这个数字字符串的小数点有几位的int类型 例如:“23.123”--->“23123” + 3 比较简单.就是 ...
- 简单学C——第四天
数组 在学数组之前,有必要把前面的知识复习一遍,当然我的复习,仅仅只是提一下,而对于你,则应该认真的看一下前面的知识点,不懂可以百度,哈哈. 前面我们大致学了 1.定义变量,2.数据的输入与输出,3. ...
- vim脚本及配置
============set optional===========set nu //显示行号 numb ...
- LightOj_1104 Birthday Paradox
题目链接 题意: 若一年有n天, 问至少需要多少个人才能满足其中两个人生日相同的概率大于等于0.5? 思路: 经典问题:生日悖论 换成其互斥事件:m个人, 每个人生日都不相同的概率 ≤ 0.5 时最小 ...
- A simple brute force problem.
hdu4971:http://acm.hdu.edu.cn/showproblem.php?pid=4971 题意:给你n个项目,每完成一个项目会有一定的收益,但是为了完成某个项目,要先学会一些技能, ...
- Ubuntu下su:authentication failure的解决办法
$ su - rootPassword: su: Authentication failureSorry. 这时候输入 $ sudo passwd rootEnter new UNIX passwor ...
- UI设计师的 Android 备忘录
Images and themes Nine-patch Colors Holo themes Naming conventions Naming conventions for drawables ...
- Spring messageSource
Spring中可以使用两个类加载资源文件: org.springframework.context.support.ReloadableResourceBundleMessageSource 和 or ...
- Bluetooth LE(低功耗蓝牙) - 第六部分(完)
在本系列前面的文章中我们已经了解了,在我们从一个TI SensorTag中获取温度和湿度数据之前,我们需要经历的各种步骤.在本系列中的最后一篇文章,我们将完成注册并接收SensorTag的通知,并接收 ...
- bzoj1853 bzoj2393
两题是类似的,这里说一下bzoj1853 首先我们求出所有的幸运号码,注意如果存在x是y的倍数则x不算在内,避免之后重复计算 下面我们就要统计幸运号码的倍数了,这显然是要用到容斥原理的 但是幸运号码很 ...