1,以下为示例代码:
- #include <linux/device.h>
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/string.h>
- MODULE_LICENSE("Dual BSD/GPL");
- static char *Version = "2.0.1";
- static int my_match(struct device *dev, struct device_driver *driver)
- {
- return !strncmp(dev->kobj.name, driver->name, strlen(driver->name));
- }
- static void my_bus_release(struct device *dev)
- {
- printk(KERN_DEBUG "my bus release ");
- }
- struct device my_bus = {
- .kobj.name = "my_bus0",
- .release = my_bus_release
- };
- struct bus_type my_bus_type = {
- .name = "my_bus",
- .match = my_match,
- };
- EXPORT_SYMBOL(my_bus);
- EXPORT_SYMBOL(my_bus_type);
- static ssize_t show_bus_version(struct bus_type *bus, char *buf)
- {
- return snprintf(buf, PAGE_SIZE, "%s ", Version);
- }
- static BUS_ATTR(version, S_IRUGO, show_bus_version, NULL);
- static int __init my_bus_init(void)
- {
- int ret;
- ret = bus_register(&my_bus_type);
- if (ret)
- return ret;
- if (bus_create_file(&my_bus_type, &bus_attr_version))
- printk(KERN_NOTICE "Fail to create version attribute! ");
- ret = device_register(&my_bus);
- if (ret)
- printk(KERN_NOTICE "Fail to register device:my_bus! ");
- return ret;
- }
- static void my_bus_exit(void)
- {
- device_unregister(&my_bus);
- bus_unregister(&my_bus_type);
- }
- module_init(my_bus_init);
- module_exit(my_bus_exit);
- #include <linux/device.h>
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/string.h>
- MODULE_LICENSE("Dual BSD/GPL");
- extern struct device my_bus;
- extern struct bus_type my_bus_type;
- static void my_dev_release(struct device *dev)
- {
- }
- struct device my_dev = {
- .init_name = "my_dev",
- .bus = &my_bus_type,
- .parent = &my_bus,
- .release = my_dev_release,
- };
- static ssize_t mydev_show(struct device *dev,struct device_attribute *attr, char *buf)
- {
- return sprintf(buf, "%s ", "This is my device!");
- }
- static DEVICE_ATTR(dev, S_IRUGO, mydev_show, NULL);
- static int __init my_device_init(void)
- {
- int ret = 0;
- ///strncpy(my_dev->kobj.name, "my_dev", strlen(my_dev->name));
- device_register(&my_dev);
- device_create_file(&my_dev, &dev_attr_dev);
- return ret;
- }
- static void my_device_exit(void)
- {
- device_unregister(&my_dev);
- }
- module_init(my_device_init);
- module_exit(my_device_exit);
- #include <linux/device.h>
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/string.h>
- MODULE_LICENSE("Dual BSD/GPL");
- extern struct bus_type my_bus_type;
- static int my_probe(struct device *dev)
- {
- printk("Driver found device which my driver can handle! ");
- return 0;
- }
- static int my_remove(struct device *dev)
- {
- printk("Driver found device unpluged! ");
- return 0;
- }
- struct device_driver my_driver = {
- .name = "my_dev",
- .bus = &my_bus_type,
- .probe = my_probe,
- .remove = my_remove,
- };
- static ssize_t mydriver_show(struct device_driver *driver, char *buf)
- {
- return sprintf(buf, "%s ", "This is my driver!");
- }
- static DRIVER_ATTR(drv, S_IRUGO, mydriver_show, NULL);
- static int __init my_driver_init(void)
- {
- int ret = 0;
- driver_register(&my_driver);
- driver_create_file(&my_driver, &driver_attr_drv);
- return ret;
- }
- static void my_driver_exit(void)
- {
- driver_unregister(&my_driver);
- }
- module_init(my_driver_init);
- module_exit(my_driver_exit);