华清远见上海中心22071班
作业1:例如:char buf[128] = "i am huyue";---->用户空间
要求:在内核中将字符数组的内容进行打印 ---->通过dmesg命令,可以查看到信息
命令码封装:#define UACCESS_BUF _IOW('a',1,char [128])
作业2:typedef struct{
int width;
int high;
}image_t;
image_t image = {20,1024};
1.将结构体的数据传递到内核空间,并在内核空间进行打印
2.在内核空间,将width和 high分别+10之后的值,传递给用户空间
3.在用户空间进行打印结构体的值为{30,1034}
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "ioctl.h"
#define CNAME "myled"
char kbuf[128] = "";
struct class *cls;
struct device *dvc;
int major, res;
int mychardev_open(struct inode *inode, struct file *file)
{
printk("open success\n");
return 0;
}
int mychardev_release(struct inode *inode, struct file *file)
{
printk("release success\n");
return 0;
}
long mychardev_ioctl(struct file *file, unsigned int request, unsigned long addr)
{
if (request == UACCESS_BUF)
{
res = copy_from_user(kbuf, (void *)addr, sizeof(kbuf));
if (res)
{
printk("copy_from_user failed\n");
return -EIO;
}
printk("copy_from_user success\n");
printk("KBUF = %s", kbuf);
return 0;
}
return 0;
}
struct file_operations fop = {
.open = mychardev_open,
.release = mychardev_release,
.unlocked_ioctl = mychardev_ioctl};
static int __init led_init(void)
{
major = register_chrdev(0, CNAME, &fop);
if (major < 0)
{
printk("register_chrdev failed\n");
return major;
}
printk("register_chrdev success.major = %d\n", major);
// 上传目录
cls = class_create(THIS_MODULE, CNAME);
if (IS_ERR(cls))
{
printk("class_create failed\n");
PTR_ERR(cls);
}
// 上传节点
dvc = device_create(cls, NULL, MKDEV(major, 0), NULL, CNAME);
if (IS_ERR(dvc))
{
printk("device_create failed\n");
PTR_ERR(dvc);
}
return 0;
}
static void __exit led_exit(void)
{
device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
unregister_chrdev(major, CNAME);
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
#ifndef __IOCTL_H__
#define __IOCTL_H__
#define UACCESS_BUF _IOW('a', 1, char[128])
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include "ioctl.h"
int main(int argc, const char *argv[])
{
int fd = open("/dev/myled", O_RDWR);
if (-1 == fd)
{
perror("open");
return -1;
}
char buf[128] = "i am lanxing";
ioctl(fd, UACCESS_BUF, buf);
return 0;
}
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "ioctl.h"
#define CNAME "myled"
char kbuf[128] = "";
struct class *cls;
struct device *dvc;
int major, res;
image_t kimage;
int mychardev_open(struct inode *inode, struct file *file)
{
printk("open success\n");
return 0;
}
int mychardev_release(struct inode *inode, struct file *file)
{
printk("release success\n");
return 0;
}
long mychardev_ioctl(struct file *file, unsigned int request, unsigned long addr)
{
if (request == STRUCT)
{
res = copy_from_user(&kimage, (void *)addr, sizeof(kimage));
if (res)
{
printk("copy_from_user failed\n");
return -EIO;
}
printk("copy_from_user success\n");
kimage.high += 10;
kimage.width += 10;
res = copy_to_user((void *)addr, (void *)&kimage, sizeof(kimage));
if (res)
{
printk("copy_to_user failed\n");
return -EIO;
}
return 0;
}
return 0;
}
struct file_operations fop = {
.open = mychardev_open,
.release = mychardev_release,
.unlocked_ioctl = mychardev_ioctl};
static int __init led_init(void)
{
major = register_chrdev(0, CNAME, &fop);
if (major < 0)
{
printk("register_chrdev failed\n");
return major;
}
printk("register_chrdev success.major = %d\n", major);
// 上传目录
cls = class_create(THIS_MODULE, CNAME);
if (IS_ERR(cls))
{
printk("class_create failed\n");
PTR_ERR(cls);
}
// 上传节点
dvc = device_create(cls, NULL, MKDEV(major, 0), NULL, CNAME);
if (IS_ERR(dvc))
{
printk("device_create failed\n");
PTR_ERR(dvc);
}
return 0;
}
static void __exit led_exit(void)
{
device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
unregister_chrdev(major, CNAME);
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
#ifndef __IOCTL_H__
#define __IOCTL_H__
typedef struct
{
int width;
int high;
} image_t;
#define STRUCT _IOWR('a', 0, image_t)
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include "ioctl.h"
int main(int argc, const char *argv[])
{
int fd = open("/dev/myled", O_RDWR);
if (-1 == fd)
{
perror("open");
return -1;
}
image_t image = {20, 1024};
ioctl(fd, STRUCT, &image);
printf("WIDTH = %d, HIGH =%d", image.width, image.high);
close(fd);
return 0;
}