#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;
}