01_basicLinux内核模块-CSDN博客文章浏览阅读45次。环境ID=ubuntuMakefilemodules:clean:basic.creturn 0;运行效果。https://blog.csdn.net/m0_37132481/article/details/136157384?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22136157384%22%2C%22source%22%3A%22m0_37132481%22%7D
device_bus_driver.c
root@T:/media/sf_D_DRIVE/kmodule/04_device_bus_driver# cat device_bus_driver.c
#include <linux/module.h>
#include <linux/device.h>
#include <linux/slab.h>
#define TAG "HELLO# "
struct device *dev = NULL;
struct device_driver *drv = NULL;
static int my_bus_match(struct device *_dev, struct device_driver *_drv)
{
printk(TAG "%s called\n", __func__);
return !strcmp(dev_name(dev), drv->name);
}
static int my_bus_probe(struct device *dev)
{
printk(TAG "%s called\n", __func__);
printk(TAG "hello drv_%s, dev_%s, Happy Marriage!\n", dev->driver->name, dev_name(dev));
return 0;
}
static void my_bus_remove(struct device *dev)
{
printk(TAG "%s called\n", __func__);
}
struct bus_type my_bus_type = {
.name = "my-bus",
.match = my_bus_match,
.probe = my_bus_probe,
.remove = my_bus_remove,
};
static int device_bus_driver_init(void)
{
printk(TAG "%s called\n", __func__);
/* register bus */
bus_register(&my_bus_type);
/* register device */
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
dev->init_name = "hello";
dev->bus = &my_bus_type;
device_register(dev);
dev = get_device(dev);
/* register driver */
drv = kzalloc(sizeof(*drv), GFP_KERNEL);
drv->name = "hello";
drv->bus = &my_bus_type;
driver_register(drv);
return 0;
}
static void device_bus_driver_exit(void)
{
printk(TAG "%s called\n", __func__);
/* unregister driver */
driver_unregister(drv);
/* unregister device */
device_unregister(dev);
/* unregister bus */
bus_unregister(&my_bus_type);
/* free resource */
kfree(dev);
kfree(drv);
}
module_init(device_bus_driver_init);
module_exit(device_bus_driver_exit);
MODULE_LICENSE("GPL");
效果