#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "myled.h"
#define CNAME "myled"
int major;
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;
int wet;
image_t image;
ret = copy_from_user(&image,(void*)arg,sizeof(image));
printk("high=%d,width=%d\n",image.high,image.width);
image.high+=10;
image.width+=10;
wet = copy_to_user((void*)arg,&image,sizeof(image));
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)
{
//1.注册字符设备驱动
major = register_chrdev(0,CNAME,&fops);
if(major < 0)
{
printk("register chrdev is error\n");
return major;
}
printk("major=%d\n",major);
return 0;
}
//出口
static void __exit mycdev_exit(void)
{
//2.注销字符设备驱动
unregister_chrdev(major,CNAME);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
//许可证
MODULE_LICENSE("GPL");
#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 "myled.h"
int main(int argc, char const *argv[])
{
image_t image={20,1024};
int fd = -1;
fd = open("/dev/myled",O_RDWR);
if(-1 == fd)
{
perror("open is error");
exit(1);
}
ioctl(fd,UACCESS_BUF,&image);
//ioctl(fd,UACCESS_BUD,image);
printf("high=%d,width=%d\n",image.high,image.width);
close(fd);
return 0;
}
#ifndef __LED_H__
#define __LED_H__
//命令码封装
typedef struct{
int width;
int high;
}image_t;
#define UACCESS_BUF _IOW('a',0,image_t)
//#define UACCESS_BUD _IOR('b',0,image_t1)
#endif
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "myled.h"
#define CNAME "myled"
int major;
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;
char whitch[128];
ret = copy_from_user(whitch,(void*)arg,sizeof(whitch));
if(cmd)
{
printk("%s\n",whitch);
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)
{
//1.注册字符设备驱动
major = register_chrdev(0,CNAME,&fops);
if(major < 0)
{
printk("register chrdev is error\n");
return major;
}
printk("major=%d\n",major);
return 0;
}
//出口
static void __exit mycdev_exit(void)
{
//2.注销字符设备驱动
unregister_chrdev(major,CNAME);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
//许可证
MODULE_LICENSE("GPL");
#ifndef __LED_H__
#define __LED_H__
//命令码封装
#define UACCESS_BUF _IOW('a',0,char [128])
#endif
#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 "myled.h"
int main(int argc, char const *argv[])
{
int fd = -1;
char whitch[128]="i am huyue";
fd = open("/dev/myled",O_RDWR);
if(-1 == fd)
{
perror("open is error");
exit(1);
}
ioctl(fd,UACCESS_BUF,whitch);
printf("%s\n",whitch);
close(fd);
return 0;
}