一、ioctl传递字符数组
在内核中将字符数组的内容进行打印 ---->通过dmesg命令,可以查看到信息
.h头文件>>>
#ifndef __LED_H__
#define __LED_H__
#define UACCESS_BUF _IOW('a',1,char [128])
#endif
驱动程序>>>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/ioctl.h>
#include <linux/io.h>
#include "led.h"
#define CNAME "myled"
int major;
int mycdev_open(struct inode *inode,struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
long mycdev_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
{
int ret;
char kbuf[128]={0};
if(cmd==UACCESS_BUF)
{
ret = copy_from_user(kbuf,(char*)arg,sizeof(char [128]));
if(ret)
{
printk("copy from user is error...\n");
return -EIO;
}
printk("kbuf=%s",kbuf);
}
return 0;
}
int mycdev_close(struct inode *inode,struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
const struct file_operations fops = {
.open = mycdev_open,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
major=register_chrdev(0,CNAME,&fops);
if(major<0)
{
printk("register chrdev is error\n");
return major;
}
printk("major=%d\n",major);
return 0;
}
static void __exit mycdev_exit(void)
{
unregister_chrdev(major,CNAME);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用层程序>>>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "led.h"
char buf[128]="hello world";
int main(int argc, char const *argv[])
{
int fd = -1;
fd = open("/dev/myled",O_RDWR);
if(-1==fd)
{
perror("open is error");
exit(1);
}
ioctl(fd,UACCESS_BUF,buf);
close(fd);
return 0;
}
现象实现>>>
二、ioctl传递结构体
1.将结构体的数据传递到内核空间,并在内核空间进行打印
2.在内核空间,将width和 high分别+10之后的值,传递给用户空间
3.在用户空间进行打印结构体的值为{30,1034}
.h头文件>>>
#ifndef __LED_H__
#define __LED_H__
typedef struct
{
int width;
int high;
}image_t;
#define UACCESS _IOWR('a',0,image_t)
#endif
驱动程序>>>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/ioctl.h>
#include <linux/io.h>
#include "led.h"
#define CNAME "myled"
int major;
int mycdev_open(struct inode *inode,struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
long mycdev_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
{
int ret_from;
int ret_to;
image_t kimage;
if(cmd==UACCESS)
{
ret_from = copy_from_user(&kimage,(image_t*)arg,sizeof(image_t));
if(ret_from)
{
printk("copy from user is error...\n");
return -EIO;
}
printk("width=%d,high=%d\n",kimage.width,kimage.high);
kimage.width+=10;
kimage.high+=10;
ret_to = copy_to_user((image_t*)arg,&kimage,sizeof(kimage));
if(ret_to)
{
printk("copy to user is error...\n");
return -EIO;
}
}
return 0;
}
int mycdev_close(struct inode *inode,struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
const struct file_operations fops = {
.open = mycdev_open,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
major=register_chrdev(0,CNAME,&fops);
if(major<0)
{
printk("register chrdev is error\n");
return major;
}
printk("major=%d\n",major);
return 0;
}
static void __exit mycdev_exit(void)
{
unregister_chrdev(major,CNAME);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
应用层程序>>>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "led.h"
int main(int argc, char const *argv[])
{
int fd = -1;
image_t image={20,1024};
fd = open("/dev/myled",O_RDWR);
if(-1==fd)
{
perror("open is error");
exit(1);
}
ioctl(fd,UACCESS,&image);
printf("width=%d,high=%d\n",image.width,image.high);
close(fd);
return 0;
}
实现现象>>>