嵌入式学习记录3----自动创建设备节点,实现读写接口
作者:互联网
1、自动创建设备节点
涉及的函数:
struct class *class_create(struct module *owner, const char *name);
----在/sys/class/目录下会创建对应的类
void class_destroy(struct class *cls)
struct device *device_create(struct class *class, struct device *parent,
dev_t devt, const char *fmt, ...);
----在/dev/目录下会创建结点
void device_destroy(struct class *class, dev_t devt);
注意:某一步失败,要说释放掉前边申请的资源。
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>
static unsigned int major = 250;
static unsigned int minor = 0;
struct class *cls;
dev_t devno = 0;
int hello_open(struct inode *inode, struct file *file)
{
printk("hello_open\n");
return 0;
}
int hello_release(struct inode *inode, struct file *file)
{
printk("hello_release\n");
return 0;
}
struct file_operations fops =
{
.open = hello_open,
.release = hello_release,
};
static int hello_init(void)
{
int ret = 0;;
struct device *class_dev;
printk("hello_init\n");
ret = register_chrdev(major, "zlyinit", &fops);
if(ret < 0)
{
printk("register_chrdev fail\n");
return ret;
}
cls = class_create(THIS_MODULE, "hellozlycls");// /sys/class/
if (IS_ERR(cls)) {
printk("class_create fail\n");
ret = PTR_ERR(cls);
goto out_err_1;
}
devno = MKDEV(major, minor);
class_dev = device_create(cls, NULL, devno, "hellozlydev");// /dev/hellozlydev
if (IS_ERR(class_dev)) {
ret = PTR_ERR(class_dev);
printk("device_create fail\n");
goto out_err_2;
}
printk("device_create succ\n");
return ret;
out_err_2:
class_destroy(cls);
out_err_1:
unregister_chrdev(major, "zlyinit");
printk("ret=[%u]\n", ret);
return ret;
}
static void hello_exit(void)
{
printk("hello_exit\n");
device_destroy(cls, devno);
class_destroy(cls);
unregister_chrdev(major, "zlyinit");
return;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
2、实现read和write接口
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
static unsigned int major = 250;
static unsigned int minor = 0;
struct class *cls;
dev_t devno = 0;
#define KMAX_LEN 32
char kbuf[KMAX_LEN] = "kernel";
int hello_open(struct inode *inode, struct file *file)
{
printk("hello_open\n");
return 0;
}
int hello_release(struct inode *inode, struct file *file)
{
printk("hello_release\n");
return 0;
}
//read(fd,buff,len);
ssize_t hello_read(struct file *filep, char __user *buf, size_t size, loff_t *pos)
{
int error;
printk("hello_read\n");
if(size > KMAX_LEN)
{
size = KMAX_LEN;
}
if(copy_to_user(buf, kbuf, size))
{
error=-EFAULT;
return error;
}
return size;
}
//write(fd,buff,len)
ssize_t hello_write(struct file *filep, const char __user *buf, size_t size, loff_t *pos)
{
int error;
printk("hello_write\n");
if(size > KMAX_LEN)
{
size = KMAX_LEN;
}
memset(kbuf, 0, KMAX_LEN);
if(copy_from_user(kbuf, buf, size))
{
error = -EFAULT;
return error;
}
printk("hello_write:%s\n", kbuf);
return size;
}
struct file_operations fops =
{
.open = hello_open,
.release = hello_release,
.read = hello_read,
.write = hello_write,
};
static int hello_init(void)
{
int ret = 0;;
struct device *class_dev;
printk("hello_init\n");
ret = register_chrdev(major, "zlyinit", &fops);
if(ret < 0)
{
printk("register_chrdev fail\n");
return ret;
}
cls = class_create(THIS_MODULE, "hellozlycls");// /sys/class/
if (IS_ERR(cls)) {
printk("class_create fail\n");
ret = PTR_ERR(cls);
goto out_err_1;
}
devno = MKDEV(major, minor);
class_dev = device_create(cls, NULL, devno, "hellozlydev");// /dev/hellozlydev
if (IS_ERR(class_dev)) {
ret = PTR_ERR(class_dev);
printk("device_create fail\n");
goto out_err_2;
}
printk("device_create succ\n");
return ret;
out_err_2:
class_destroy(cls);
out_err_1:
unregister_chrdev(major, "zlyinit");
printk("ret=[%u]\n", ret);
return ret;
}
static void hello_exit(void)
{
printk("hello_exit\n");
device_destroy(cls, devno);
class_destroy(cls);
unregister_chrdev(major, "zlyinit");
return;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
标签:struct,读写,ret,hello,----,printk,嵌入式,class,cls 来源: https://blog.csdn.net/u010797002/article/details/118467076