#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/signal.h>
#include <linux/timer.h>
#include <linux/slab.h>

#define printw(fmt, ...)     ({printk( KERN_ERR "[ksync] " fmt, ##__VA_ARGS__); 0; })

;
struct cdev testcdev;
struct class *myclass;
static struct fasync_struct *async;
][] = {};

int hexdump(unsigned char *name, unsigned char *data, unsigned short len)
{
    unsigned char *buf;
    int i, p, ret;
    unsigned ] = {};
    unsigned ;

    tmp_len = sprintf(tmp, "%s hex(len=%d): ", name, len);
    )
        len = ;
    buf = kmalloc( (len + ) *  + tmp_len, GFP_KERNEL);
    memset(buf, ) *  + tmp_len);
    memcpy(buf, tmp, tmp_len);

     == len))
    {
        printw("%s\n", buf);
        kfree(buf);
        ;
    }
    , p = tmp_len; i < len; i++)
    {
        ret = sprintf((buf + p), "%02x ", *(data + i));
        p = p + ret;
    }
    printw("%s\n", buf);
    kfree(buf);
    ;
}

void ksync_send(unsigned short cmd, unsigned char *buffer, short len)
{
    unsigned ] = {};
    sprintf(string, "%s(0x%x, %d)", __func__, cmd, len);

    memcpy(fsync_buffer[] + , ();
    memcpy(fsync_buffer[] + , ();
    memcpy(fsync_buffer[] + ,     buffer, len);
    hexdump(], len+);
    kill_fasync(&async, SIGIO, POLL_IN);
}

void ksync_recv(unsigned short cmd, unsigned char *buffer, short len)
{
    unsigned ] = {};
    sprintf(string, "%s(0x%x, %d)", __func__, cmd, len);
    hexdump(string, buffer, len);
    ksync_send(0x57, buffer, len);
}

int char_open(struct inode *inode, struct file *filp)
{
    printw("char_open ok...\n");
    ;
}

static int char_fasync(int fd, struct file *filp, int mode)
{
    return fasync_helper(fd, filp, mode, &async);
}

int char_release(struct inode *inode,struct file *filp)
{
        printw("char close\n");
        , filp, );;
}
int char_ioctl (struct inode *inode, struct file *filelp, unsigned int cmd, unsigned long args)
{

    ;

}
ssize_t char_write(struct file *filp, const char __user *buffer, size_t count, loff_t *offset)
{
    unsigned ];
    printw("%s\n", __func__);
    ], buffer, count))
    {
        return -EFAULT;
    }
    hexdump("char_write", buffer, count);
    memcpy((], );
    ksync_recv( info[], fsync_buffer[] + , info[] );
    printw("char_write ok...\n");
    return count;
}

ssize_t char_read(struct file *filp, char __user *buffer, size_t count, loff_t *offset)
{
    int ret;
    ret = copy_to_user( (unsigned ], count);
     != ret) {
        printw("[%s][%d][err]\n", __func__, __LINE__);
        return -EFAULT;
    }
    filp->f_pos += count;
    hexdump(], count);
    printw("char_read ok...\n");
    ;
}

struct file_operations fop =
{
        .open = char_open,
        .release = char_release,
        .compat_ioctl = char_ioctl,
        .write = char_write,
        .read = char_read,
        .fasync = char_fasync,
};

int __init a_init(void)
{

    dev_t dev;
    int ret;
    dev = MKDEV(major,);
    ret = register_chrdev_region(dev,,"char");
    if(ret)
    {
        alloc_chrdev_region(&dev,,,"char");
        major = MAJOR(dev);
    }

    testcdev.owner = THIS_MODULE;
    cdev_init(&testcdev, &fop);
    cdev_add(&testcdev, dev, );

    myclass = class_create(THIS_MODULE, "char_class");
    device_create(myclass,NULL,dev,NULL, "ksync");
    printw("module init ok ...\n");
    ;
}

void __exit a_exit(void)
{
    dev_t dev;
    dev = MKDEV(major ,);

    device_destroy(myclass, dev);
    class_destroy(myclass);

    cdev_del(&testcdev);
    unregister_chrdev_region(dev,);
    printw("module exit ok....\n");
}

module_init(a_init);
module_exit(a_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("...");
MODULE_DESCRIPTION("ksync");
MODULE_VERSION("V1.0");
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
int g_fd;
unsigned ][] = {};

int hexdump(unsigned char *name, unsigned char *data, unsigned short len)
{
    unsigned char *buf;
    int i, p, ret;
    unsigned ] = {};
    unsigned ;

    tmp_len = sprintf(tmp, "%s hex(len=%d): ", name, len);
    )
        len = ;
    buf = ) *  + tmp_len);
    memset(buf, ) *  + tmp_len);
    memcpy(buf, tmp, tmp_len);

     == len))
    {
        printf("%s\n", buf);
        free(buf);
        ;
    }
    , p = tmp_len; i < len; i++)
    {
        ret = sprintf((buf + p), "%02x ", *(data + i));
        p = p + ret;
    }
    printf("%s\n", buf);
    free(buf);
    ;
}

void ksync_recv(unsigned short cmd, unsigned char *buffer, short len)
{

}

void ksync_send(unsigned short cmd, unsigned char *buffer, short len)
{
    unsigned ];
    int ret;

    memcpy(s_buf + , ();
    memcpy(s_buf + , ();
    memcpy(s_buf + ,     buffer, len);
    hexdump();
    ret = write(g_fd, s_buf, len + );
    printf("write ret %d\n", ret);
}

void sig_handler(int sig)
{
    int ret;
    unsigned ];
    unsigned ];
    if(sig == SIGIO)
    {
        ret = read(g_fd, r_buf, );
        memcpy((], r_buf + , );
        memcpy((], r_buf + , );
        hexdump(]+);
        ksync_recv(info[], r_buf + , info[]);
    }
    return;
}

int main(void)
{
    unsigned ];
    signal(SIGIO, sig_handler);
    g_fd = open("/dev/ksync", O_RDWR);
     == g_fd)
    {
        printf("open err\n");
        ;
    }
    printf("open %d\n", g_fd);
    fcntl(g_fd, F_SETOWN, getpid());
    fcntl(g_fd, F_SETFL, fcntl(g_fd, F_GETFL) | FASYNC);
    )
    {
        memset(input, );
        gets(input);
        fflush(stdin);
        )
            continue;
        ksync_send(0x56, input, strlen(input) );
    }
    ;
}
# Makefile for PEDD

EXTRA_CFLAGS += -Wframe-larger-than=

ifneq ($(CROSS_COMPILE),)
    EXTRA_CFLAGS += -DARC_SDP
endif

ifeq ($(KERNELRELEASE),)

ifeq ($(CROSS_COMPILE),)
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
else
    KERNELDIR ?= /workspace/bpcie/output/build/linux-arc-axs101--3.13
endif

    PWD       := $(shell pwd)

modules:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions *.symvers *.order *.out

.PHONY: modules modules_install clean

else

    obj-m    := ksync_drv.o

endif

最新文章

  1. Android中使用ImageViewSwitcher实现图片切换轮播导航效果
  2. Ubuntu16.04安装docker
  3. ubuntu 下安装mysql,以及配置远程登录
  4. static和extern对函数的作用
  5. vuejs过滤器
  6. S1:动态方法调用:call &amp; apply
  7. CentOS6 Squid代理服务器的安装与配置
  8. Tomcat工作原理
  9. 多线程实际运用&lt;第七篇&gt;
  10. Linux 下github的使用
  11. 基于SOAP的xml网络交互心得
  12. MongoDb 集群不可用后SECONDARY节点强制启动
  13. HttpClient之可恨的Expect(C# http 请求卡住的解决办法)
  14. Could not find or load main class org.apache.spark.deploy.yarn.ApplicationMaster
  15. [转载]mapreduce合并小文件成sequencefile
  16. Orchard是如何运行的
  17. 理解指令的restrict属性(转)
  18. Redis安全性配置
  19. Linux各发行版本及其软件包管理方法
  20. Aop之使用Castle动态代理实现对方法的拦截

热门文章

  1. golang数据结构之插入排序
  2. python做中学(三)条件编译的用法
  3. Linux 部署vue项目(使用nginx)
  4. 15-Django开发REST接口
  5. 拎壶学python3-----(3)python之while循环用法
  6. pandas 学习 第2篇:Series -(创建,属性,转换和索引)
  7. zookeeper — 实现分布式锁
  8. Winform中在使用Dock属性设计页面布局控件的顺序导致页面效果不同的问题
  9. JS的with关键字到底是什么?
  10. Linux命令大全|linux常用命令