驱动开发day4

1.在内核中将字符数组的内容进行打印,通过dmesg命令,查看信息

命令码封装:

#ifndef __ZY_H_
#define __ZY_H_

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

#endif

自动创建设备结点:

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include "zy.h"
#define CNAME "zy"
int major;
struct class* cls;
struct device* dev;
char buf[128]="";
int mycdev_open(struct inode *inode,struct file *file)
{
	return 0;
};
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int ret;
	if(UACCESS_BUF==cmd)
    {
		ret=copy_from_user(buf,(void*)arg,sizeof(buf));
		if(ret)
		{
			printk("copy from user led on!!!!\n");
            return -EIO;
		}
		printk("buf=%s\n",buf);
    }
    return 0;
}
int mycdev_close(struct inode *inode,struct file *file)
{
	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);
	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 mycdev_exit(void)
{
    class_destroy(cls);
    device_destroy(cls,MKDEV(major,0));
	unregister_chrdev(major,CNAME);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

发送数组数据:

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

int main(int argc, char const *argv[])
{
    char buf[128]="hello world";
    int fd=-1;
    fd=open("/dev/zy",O_RDWR);
    if(-1==fd)
    {
        perror("open is error\n");
        exit(1);
    }
    ioctl(fd,UACCESS_BUF,buf);A
    close(fd);
    return 0;
}

 

 

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

结构体封装:

#ifndef __IOCTL_H_
#define __IOCTL_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

自动创建设备结点:

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include "zy2.h"
#define CNAME "zy2"
struct class* cls;
struct device* dev;
int major;
image_t image;
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;
    if(cmd==struct_send)
    {
        ret=copy_from_user(&image,(void*)arg,sizeof(image_t));
        if(ret)
        {
            printk("copy from user error\n");
            return EIO;
        }
        printk("width=%d\nhigh=%d\n",image.width,image.high);
        image.width+=10;
        image.high+=10;
    }
    else if(cmd==struct_recv)
    {
        ret=copy_to_user((void*)arg,&image,sizeof(image_t));
        if(ret)
        {
            printk("copy to user 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);
    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 mycdev_exit(void)
{
	unregister_chrdev(major,CNAME);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

完成数据传递:

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

int main(int argc, char const *argv[])
{
    image_t image={20,1024};
    int fd=-1;
    fd=open("/dev/zy2",O_RDWR);
    if(-1==fd)
    {
        perror("open is error\n");
        exit(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;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值