华清远见11.17

1.在用户空间中有个字符数组,要求在内核空间打印,用dmesg查看。(ioctl实现)

        zy.h,封装一个发送用的命令码

#ifndef __LED_H__
#define __LED_H__
#define UACCESS_BUF _IOW('a',1,char [128])
#endif

        zy.c 申请并自动创建设备结点,在mydev_ioctl函数中确认命令码并接受字符数组信息存储在kbuf中,同时打印kbuf信息。

#include <linux/module.h>
#include <linux/init.h>
#include<linux/device.h>
#include<linux/uaccess.h>
#include<linux/io.h>
#include<linux/fs.h>
#include"zy.h"
#define CNAME "zy"
int major;
struct class* cls;
struct device* dev;
char kbuf[128]={};
int mydev_open (struct inode *inode, struct file *file)
{
    return 0;
}
int mydev_close (struct inode *inode, struct file *file)
{
    return 0;
}

long mydev_ioctl (struct file *file, unsigned int cmd,unsigned long arg)
{
    int ret;
    if(cmd==UACCESS_BUF)
    {
        ret=copy_from_user(kbuf,(void*)arg,sizeof(kbuf));
        if(ret)
        {
            printk("copy from user error\n");
            return EIO;
        }
        printk("kbuf = %s\n",kbuf);
    }
    return 0;
}
const struct file_operations fops={
    .open=mydev_open,
    .release=mydev_close,
    .unlocked_ioctl=mydev_ioctl,
    
};

static int __init demo_init(void)
{
    major=register_chrdev(0,CNAME,&fops);
    if(major<0)
    {
        printk("register_chrdev error\n");
        return major;
    }
    printk("major=%d\n",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);
    }
    return 0;    
}

static void __exit demo_exit(void)
{
    device_destroy(cls,MKDEV(major,0));
    class_destroy(cls);
    unregister_chrdev(major,CNAME);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");

        test.c 打开驱动文件并发送字符数组

#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 "zy.h"
int main(int argc, char const *argv[])
{
    char buf[128]={"You have an ambitious nature and may make a name for yourself"};
    int fd=-1;
    fd=open("/dev/zy",O_RDWR);
    if(-1==fd)
    {
        perror("open error");
        return -1;
    }
    ioctl(fd,UACCESS_BUF,buf);
    return 0;
}

makefile:编译工具

ARCH?=x86
NAME?=zy
ifeq ($(ARCH),x86)
	KERNEDIR:=/lib/modules/$(shell uname -r)/build
else
	KERNEDIR:=/home/ubuntu/linux-5.10.61
endif
PWD:=$(shell pwd)
all:
	make -C $(KERNEDIR) M=$(PWD) modules
clean:
	make -C $(KERNEDIR) M=$(PWD) clean
obj-m:=$(NAME).o

实验结果截图

 二:将结构题传递到内核空间,在内核空间打印,把值+10后传递会用户空间打印。

        zy2.h:封装结构体和发送接收两个命令码。

#ifndef __LED_H__
#define __LED_H__
typedef struct {
    int width;
    int high;
}image_t;
#define struct_send _IOW('a',1,image_t)
#define struct_recv _IOR('a',1,image_t)
#endif

        zy2.c:在接收后打印,然后加10返回用户空间

#include <linux/module.h>
#include <linux/init.h>
#include<linux/device.h>
#include<linux/uaccess.h>
#include<linux/io.h>
#include<linux/fs.h>
#include"zy2.h"
#define CNAME "zy2"
int major;
struct class* cls;
struct device* dev;
image_t kimage;
int mydev_open (struct inode *inode, struct file *file)
{
    return 0;
}
int mydev_close (struct inode *inode, struct file *file)
{
    return 0;
}

long mydev_ioctl (struct file *file, unsigned int cmd,unsigned long arg)
{
    int ret;
    if(cmd==struct_send)
    {
        ret=copy_from_user(&kimage,(void*)arg,sizeof(image_t));
        if(ret)
        {
            printk("copy from user error\n");
            return EIO;
        }
        printk("width=%d\nhigh=%d\n",kimage.width,kimage.high);
        kimage.width+=10;
        kimage.high+=10;
    }
    else if(cmd==struct_recv)
    {
        ret=copy_to_user((void*)arg,&kimage,sizeof(image_t));
        if(ret)
        {
            printk("copy to user error\n");
            return EIO;
        }
    }
    return 0;
}
const struct file_operations fops={
    .open=mydev_open,
    .release=mydev_close,
    .unlocked_ioctl=mydev_ioctl,
    
};

static int __init demo_init(void)
{
    major=register_chrdev(0,CNAME,&fops);
    if(major<0)
    {
        printk("register_chrdev error\n");
        return major;
    }
    printk("major=%d\n",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);
    }
    return 0;    
}

static void __exit demo_exit(void)
{
    device_destroy(cls,MKDEV(major,0));
    class_destroy(cls);
    unregister_chrdev(major,CNAME);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");

        test2.c:发送接收并打印

#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 "zy2.h"
int main(int argc, char const *argv[])
{
    image_t image={20,40};
    int fd=-1;
    fd=open("/dev/zy2",O_RDWR);
    if(-1==fd)
    {
        perror("open error");
        return -1;
    }
    ioctl(fd,struct_send,&image);
    ioctl(fd,struct_recv,&image);
    printf("width=%d\nhigh=%d\n",image.width,image.high);
    close(fd);
    return 0;
}

makefile:编译工具

ARCH?=x86
NAME?=zy2
ifeq ($(ARCH),x86)
	KERNEDIR:=/lib/modules/$(shell uname -r)/build
else
	KERNEDIR:=/home/ubuntu/linux-5.10.61
endif
PWD:=$(shell pwd)
all:
	make -C $(KERNEDIR) M=$(PWD) modules
clean:
	make -C $(KERNEDIR) M=$(PWD) clean
obj-m:=$(NAME).o

结果截图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值