#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
//相当于之前的platform_device, i2c_client, spi_device
struct test_dev{
struct device dev;
int number;
};
//相当于之前的platform_driver, i2c_driver, spi_driver
struct test_drv{
struct device_driver driver;
int version;
int (*probe)(struct test_dev * pdev);
int (*remove)(struct test_dev * pdev);
};
int mybus_probe(struct test_dev * pdev)
{
printk("--------%s---------\n",__FUNCTION__);
return 0;
}
int mybus_remove(struct test_dev * pdev)
{
printk("--------%s---------\n",__FUNCTION__);
return 0;
}
int mybus_parent_probe(struct device *pdev)
{
struct test_dev * tdev;
struct test_drv * tdrv;
struct device_driver * pdrv;
pdrv = pdev->driver;
tdev = container_of(pdev, struct test_dev, dev);
tdrv = container_of(pdrv, struct test_drv, driver);
tdrv->probe(tdev);
return 0;
}
int mybus_parent_remove(struct device *pdev)
{
struct test_dev * tdev;
struct test_drv * tdrv;
struct device_driver * pdrv;
pdrv = pdev->driver;
tdev = container_of(pdev, struct test_dev, dev);
tdrv = container_of(pdrv, struct test_drv, driver);
tdrv->remove(tdev);
return 0;
}
int mybus_match(struct device *pdev, struct device_driver *pdrv)
{
struct test_dev * tdev;
struct test_drv * tdrv;
tdev = container_of(pdev, struct test_dev, dev);
tdrv = container_of(pdrv, struct test_drv, driver);
if(tdev->number == tdrv->version)
return 1;
return 0;
}
void mybus_release(struct device *pdev)
{
}
struct bus_type bus = {
.name = "liyou_bus",
.match = mybus_match,
};
struct test_dev my_dev = {
.dev = {
.init_name = "mydev",
.bus = &bus,
.release = mybus_release,
},
.number = 0x1234,
};
struct test_drv my_drv = {
.driver = {
.name = "mydrv",
.bus = &bus,
.probe = mybus_parent_probe,
.remove = mybus_parent_remove,
},
.version = 0x1234,
.probe = mybus_probe,
.remove = mybus_remove,
};
static int __init mybus_init(void)
{
int ret;
ret = bus_register(&bus);
ret = device_register(&my_dev.dev);
ret = driver_register(&my_drv.driver);
return 0;
}
static void __exit mybus_exit(void)
{
driver_unregister(&my_drv.driver);
device_unregister(&my_dev.dev);
bus_unregister(&bus);
}
module_init(mybus_init);
module_exit(mybus_exit);
MODULE_LICENSE("GPL");