hello_bus.c
#include linux/module.h
#include linux/init.h
#include linux/device.h
#include linux/kdev_t.h
#include linux/mod_devicetable.h
#include linux/export.h
#include hello_bus.h
int bus_match(struct device *dev, struct device_driver *drv);
struct bus_type BusSY = {
.name = "hellobus",
.match = bus_match,
};
EXPORT_SYMBOL_GPL(BusSY);
int bus_match(struct device *dev, struct device_driver *drv)
{
const struct of_device_id * pof_match_table = drv->of_match_table;
struct device * pdev;
pdev = get_device(dev);
if (!pdev)
return -EINVAL;
for (pof_match_table; pof_match_table->name[0]; pof_match_table++)
if (strcmp(dev->bus->name,pof_match_table->name) == 0)
{
return 1;
}
return NULL;
}
static int BusReg_init(void)
{
int ret;
ret = bus_register(&BusSY);
if (ret)
printk(KERN_NOTICE "Unable to register BusSY\n");
return ret;
}
static void BusReg_exit(void)
{
bus_unregister(&BusSY);
}
MODULE_AUTHOR("HELLOBUS");
MODULE_LICENSE("GPL");
module_init(BusReg_init);
module_exit(BusReg_exit);
hello_bus.h
#ifndef HELLO_BUS_H_
#define HELLO_BUS_H_
extern struct bus_type BusSY;
#endif
hello_dev.c
#include linux/module.h
#include linux/init.h
#include linux/device.h
#include linux/kdev_t.h
#include hello_bus.h
struct device deviceSY = {
.init_name = "hellodev",
.bus = &BusSY,
};
static int devicesReg_init(void)
{
int ret;
ret = device_register(&deviceSY);
if (ret)
printk(KERN_NOTICE "Unable to register deviceSY\n");
return ret;
}
static void devicesReg_exit(void)
{
device_unregister(&deviceSY);
}
MODULE_AUTHOR("HELLODEV");
MODULE_LICENSE("GPL");
module_init(devicesReg_init);
module_exit(devicesReg_exit);
hello_drv.c
#include linux/module.h
#include linux/init.h
#include linux/device.h
#include linux/mod_devicetable.h
#include hello_bus.h
int helloprobe(struct device *dev);
static const struct of_device_id helloid[] = {
{.name = "hellobus",},
{},
};
struct device_driver driverSY = {
.name = "hellodrv",
.bus = &BusSY,
.of_match_table = &helloid,
.probe = helloprobe,
};
int helloprobe(struct device *dev)
{
printk("hello driver probe,but there is do nothing now!\n");
return 0;
}
static int driverReg_init(void)
{
int ret;
ret = driver_register(&driverSY);
if (ret)
printk(KERN_NOTICE "Unable to register driverSY\n");
return ret;
}
static void driverReg_exit(void)
{
driver_unregister(&driverSY);
}
MODULE_AUTHOR("HELLODRV");
MODULE_LICENSE("GPL");
module_init(driverReg_init);
module_exit(driverReg_exit);
ifneq ($(KERNELRELEASE),)
obj-m +=hello_bus.o
obj-m +=hello_dev.o
obj-m +=hello_drv.o
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions *.order *.symvers
.PHONY: modules modules_install clean
endif
阅读(1333) | 评论(0) | 转发(0) |