华清远见上海中心班--11.17作业

一、用ioctl传递字符数组

#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/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;
}
//关闭
int mycdev_close(struct inode *inode,struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
long my_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,(void*)arg,sizeof(kbuf));
        printk("kbuf = %s\n",kbuf);    
    }
    return 0;
}
const struct file_operations fops ={
    .open = mycdev_open,
    .release = mycdev_close,
    .unlocked_ioctl = my_ioctl,
};
//入口
static int __init demo_init(void)
{
    //注册字符设备驱动
    major = register_chrdev(0,CNAME,&fops);
    if(major <0)
    {
        printk("registe chrdec is error\n");
        return major;
    }
    printk("major=%d\n",major);
    return 0;
}
static void __exit demo_exit(void)
{
    //注销字符设备驱动

    unregister_chrdev(major,CNAME);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");



#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "led.h"
char buf[128]={"I am world"};
int main(int argc,char const *argv[])
{
    int fd = -1;
    fd = open("/dev/myled",O_RDWR);
    if(-1 == fd)
    {
        perror("open is error\n");
        exit(1);
    }
    ioctl(fd,UACCESS_BUF,buf);
    close(fd);
    return 0;
}





测试现象:用dmesg命令查看到,在内核中打印的语句是与用户空间传输的“I am world”一致

二、用ioctl传递结构体

#ifndef __LED_H__
#define __LED_H__

typedef struct{
    int width;
    int higth;
}image_t;
//编写命令码
#define UACCESS_STRUCT _IOW('a',1,image_t)
#endif







#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.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;
}
//关闭
int mycdev_close(struct inode *inode,struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
long my_ioctl(struct file* file,unsigned int cmd,unsigned long arg)
{
    int ret;
    image_t kimage;
    if(cmd == UACCESS_STRUCT)
    {
        ret = copy_from_user(&kimage,(void*)arg,sizeof(kimage));
        printk("width = %d\n",kimage.width);    
        printk("higth = %d\n",kimage.higth);
        kimage.width += 10;
        kimage.higth += 10;
        ret = copy_to_user((void*)arg,&kimage,sizeof(kimage));
        if(ret)
        {
            printk("to user error\n");
            return -EIO;
        }
    }
    return 0;
}
const struct file_operations fops ={
    .open = mycdev_open,
    .release = mycdev_close,
    .unlocked_ioctl = my_ioctl,
};
//入口
static int __init demo_init(void)
{
    //注册字符设备驱动
    major = register_chrdev(0,CNAME,&fops);
    if(major <0)
    {
        printk("registe chrdec is error\n");
        return major;
    }
    printk("major=%d\n",major);
    return 0;
}
static void __exit demo_exit(void)
{
    //注销字符设备驱动
    unregister_chrdev(major,CNAME);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");







#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "led.h"
int main(int argc,char const *argv[])
{
    image_t image ={20,1024};
    int fd = -1;
    fd = open("/dev/myled",O_RDWR);
    if(-1 == fd)
    {
        perror("open is error\n");
        exit(1);
    }
    ioctl(fd,UACCESS_STRUCT,&image);
    printf("width=%d\n",image.width);
    printf("higth=%d\n",image.higth);
    close(fd);

    return 0;
}




测试现象:dmesg查看到width=20,higth=1024;用户空间接收到内核传回的+10的值,width=30,higth=1034

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值