驱动IOCTL 11/17

要求

:在内核中将字符数组的内容进行打印    ---->通过dmesg命令,可以查看到信息

#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<linux/device.h>
#include "myled.h"

#define CNAME "mydev"
int major;
//cls句柄
struct class *cls;
//dev句柄
struct device *dev;
char kbuf[128]="";
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loffs)
{
    int ret;
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    if(size>sizeof(kbuf))
    {
        size=sizeof(kbuf);
    }
    //将用户空间数据拷贝到内核空间
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy to fail\n");
        return -EIO;
    }
    return size;
}

long mycdev_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
{
   int ret;;
   //判断命令码
   switch (cmd)
   {
    case UACCESS_BUF:
        ret=copy_from_user(kbuf,(void*)arg,sizeof(kbuf));
        if(ret)
        {
            printk("用户传递数据到内核失败\n");
            return -EIO;
        }
        printk("%s\n",kbuf);
    break;
   
   default:
    break;
   }
    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 mydev_init(void)
{
    //注册字符设备驱动
    major = register_chrdev(0,CNAME,&fops);
    if(major < 0)
    {
        printk("register chrdev is error\n");
        return major;
    }

    //向上层提交目录信息
    cls = class_create(THIS_MODULE,CNAME);
    if(IS_ERR(cls))
    {
        return PTR_ERR(cls);
    }
    //向上层提交设备结点信息
    dev = device_create(cls,NULL,MKDEV(major,0),NULL,CNAME);
     if(IS_ERR(dev))
    {
        return PTR_ERR(dev);
    }
    printk("major=%d\n",major);

    return 0;
}

static void __exit mydev_exit(void)
{
      //注销提交设备结点信息
    device_destroy(cls,MKDEV(major,0));
    //注销提交目录信息
    class_destroy(cls);
    unregister_chrdev(major,CNAME);
}

module_init(mydev_init);
module_exit(mydev_exit);
MODULE_LICENSE("GPL");
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#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 "myled.h"

int main(int argc, char const *argv[])
{
    int fd = -1;
    int whitch;
    char buf[128]="hello shanghai";
    fd = open("/dev/mydev",O_RDWR);
    if(-1 == fd)
    {
        perror("open is error");
        exit(1);
    }

    while (1)
    {
        ioctl(fd,UACCESS_BUF,buf);
        sleep(1);
    }
    
    close(fd);
    return 0;
}

作业2:

1.将结构体的数据传递到内核空间,并在内核空间进行打印
2.在内核空间,将width和 high分别+10之后的值,传递给用户空间
3.在用户空间进行打印结构体的值为{30,1034}

#ifndef __LED_H__
#define __LED_H__

typedef struct{
    int width;
    int high;
}image_t;
#define UACCESS_STRUCT _IOW('a',1,image_t)
#define UACCESS_STRUCT_TOUSER _IOR('a',2,image_t)
#endif
#include<linux/init.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<linux/uaccess.h>
#include<linux/io.h>
#include<linux/device.h>
#include "myled.h"

#define CNAME "mydev"
int major;
//cls句柄
struct class *cls;
//dev句柄
struct device *dev;
image_t kimage;
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 ret1,ret2;
   //判断命令码
   switch (cmd)
   {
    case UACCESS_STRUCT:
        ret1=copy_from_user(&kimage,(void*)arg,sizeof(image_t));
        if(ret1)
        {
            printk("用户传递数据到内核失败\n");
            return -EIO;
        }
        printk("用户传递到内核的width=%d:high=%d\n",kimage.width,kimage.high);
    break;
    case UACCESS_STRUCT_TOUSER:
        kimage.high+=10;
        kimage.width+=10;
        ret2=copy_to_user((void*)arg,&kimage,sizeof(image_t));
        if(ret2)
        {
            printk("copy to fail\n");
            return -EIO;
        }
        break;
   default:
    break;
   }
   
    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 mydev_init(void)
{
    //注册字符设备驱动
    major = register_chrdev(0,CNAME,&fops);
    if(major < 0)
    {
        printk("register chrdev is error\n");
        return major;
    }

    //向上层提交目录信息
    cls = class_create(THIS_MODULE,CNAME);
    if(IS_ERR(cls))
    {
        return PTR_ERR(cls);
    }
    //向上层提交设备结点信息
    dev = device_create(cls,NULL,MKDEV(major,0),NULL,CNAME);
     if(IS_ERR(dev))
    {
        return PTR_ERR(dev);
    }
    printk("major=%d\n",major);

    return 0;
}

static void __exit mydev_exit(void)
{
      //注销提交设备结点信息
    device_destroy(cls,MKDEV(major,0));
    //注销提交目录信息
    class_destroy(cls);
    unregister_chrdev(major,CNAME);
}

module_init(mydev_init);
module_exit(mydev_exit);
MODULE_LICENSE("GPL");
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#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 "myled.h"

int main(int argc, char const *argv[])
{
    int fd = -1;
    image_t image={20,1024};
    image_t get_image;
    fd = open("/dev/mydev",O_RDWR);
    if(-1 == fd)
    {
        perror("open is error");
        exit(1);
    }

    while (1)
    {
        ioctl(fd,UACCESS_STRUCT,&image);
        ioctl(fd,UACCESS_STRUCT_TOUSER,&get_image);
        printf("内核传递过来的为width=%d:high=%d\n",get_image.width,get_image.high);
        sleep(1);
    }
    
    close(fd);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值