#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
MODULE_LICENSE("GPL");
#define NODE_NAME "hello_node"
static int hello_open(struct inode *node, struct file *fp)
{
printk("hello open.\n");
return 0;
}
static int hello_release(struct inode *node, struct file *fp)
{
printk("hello release.\n");
return 0;
}
static int hello_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
{
printk("hello ioctl, cmd: %d, arg: %d\n", cmd, arg);
return 0;
}
struct file_operations hello_ops = {
.owner = THIS_MODULE,
.open = hello_open,
.release = hello_release,
.unlocked_ioctl = hello_ioctl,
};
struct miscdevice hello_misc_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = NODE_NAME,
.fops = &hello_ops,
};
static int hello_probe(struct platform_device *dev)
{
printk("hello probe, device name: %s\n", dev->name);
misc_register(&hello_misc_dev);
return 0;
}
static int hello_remove(struct platform_device *dev)
{
printk("hello remove, device name: %s\n", dev->name);
misc_deregister(&hello_misc_dev);
return 0;
}
struct platform_driver hello_driver = {
.probe = hello_probe,
.remove = hello_remove,
.driver = {
.name = "hello_ctl",
.owner = THIS_MODULE,
}
};
struct platform_device hello_dev = {
.name = "hello_ctl",
.id = -1,
};
static int hello_init()
{
printk(KERN_EMERG "hello init.\n");
platform_device_register(&hello_dev);
platform_driver_register(&hello_driver);
return 0;
}
static void hello_exit()
{
printk(KERN_EMERG "hello exit.\n");
platform_driver_unregister(&hello_driver);
platform_device_unregister(&hello_dev);
}
module_init(hello_init);
module_exit(hello_exit);
转载请注明原文地址:https://ipadbbs.8miu.com/read-50829.html