华清远见上海中心22071

 

#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");
#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 ly";
    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");

#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("high = %d, width =%d", image.width, image.high);
    close(fd);

    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值