2023-3-16作业

ioctl练习题

一、传递字符数组

要求:在内核空间打印字符数组信息内容 

二、传递结构体

用户空间: width = 10 high = 200 将用户空间数据传递给内核空间,在内核空间进行打印,打印之后需要每个变量+10,在传递给用户空间,在用户空间打印增加后的值


mycdev.h:

#ifndef __MYCDEV_H__
#define __MYCDEV_H__

typedef struct
{
    int width;
    int high;  
}image_t;


#define ARR_SEND _IOW('a',1,char[128])
#define STRUCT_SEND _IOW('a',1,image_t)

#endif

mycdev.c:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include "mycdev.h"
#include <linux/io.h>

#define CNAME "myled"
unsigned int major;
struct class* cls;
struct device* dev;

int myled_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

long myled_ioctl(struct file *file, unsigned int cmd, unsigned long args)
{
    int res;
    char buf[128] = {0};
    image_t msg;
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    switch(cmd)
    {
    case ARR_SEND:
        res = copy_from_user(buf,(void*)args,sizeof(buf));
        if(res)
        {
            printk("copy from user is error\n");
            return  -EIO;
        }
        printk("%s\n",buf);
        break;
    case STRUCT_SEND:
        res = copy_from_user(&msg,(void*)args,sizeof(msg));
        if(res)
        {
            printk("copy from user is error\n");
            return  -EIO;
        }
        printk("msg.high = %d,msg.width = %d\n", msg.high, msg.width);
        msg.high += 10;
        msg.width += 10;

        res = copy_to_user((void*)args,&msg,sizeof(msg));
        if(res)
        {
            printk("copy from user is error\n");
            return  -EIO;
        }
        break;
    }
    return 0;
}

int myled_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

const struct file_operations fops = {
    .open = myled_open,
    .unlocked_ioctl = myled_ioctl,
    .release = myled_close,
};

static int __init mycdev_init(void)
{
    //1.注册字符设备驱动
    major = register_chrdev(major,CNAME,&fops);
    if(major < 0) //2.判断返回值
    {
        printk("register chrdev is error\n");
    }
    //3.打印获取到的主设备号
    printk("register chrdev major=%d\n",major);

    //4.向上层提交目录信息
    cls = class_create(THIS_MODULE,CNAME);
    if(IS_ERR(cls))
    {
        printk("class create error\n");
        return PTR_ERR(cls);
    }
    //5.向上层提交设备信息
    dev = device_create(cls,NULL,MKDEV(major,0),NULL,CNAME);
    if(IS_ERR(dev))
    {
        printk("class create error\n");
        return PTR_ERR(dev);
    }

    return 0;
}

static void __exit mycdev_exit(void)
{
    //销毁向上层提交设备信息
    device_destroy(cls,MKDEV(major,0));
    //销毁向上层提交目录信息
    class_destroy(cls);
    //注销字符设备驱动
    unregister_chrdev(major,CNAME);
    printk("unregister chrdev succeed\n");
}

module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

text.c:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "mycdev.h"
#include <sys/ioctl.h>
#include <string.h>

int main(int argc,const char * argv[])
{
    char arr[128] = {0};
    int fd = -1;
    fd = open("/dev/myled", O_RDWR);
    if(-1 == fd)
    {
        perror("open is error\n");
        return -1;
    }

    image_t msg = {.high = 100,.width = 200};
    ioctl(fd,STRUCT_SEND,&msg);
    printf("msg.high = %d,msg.width = %d\n", msg.high, msg.width);

    printf("please input>>>");
    fgets(arr,sizeof(arr),stdin);
    arr[strlen(arr)-1] = '\0';
    ioctl(fd,ARR_SEND,arr);
    
    close(fd);

    return 0;
}

测试结果如下:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值