1, the following is the sample code:
[CPP]View Plaincopy
- #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\n");
- }
- 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\n", 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!\n");
- ret = Device_register (&my_bus);
- if (ret)
- PRINTK (Kern_notice "Fail to register device:my_bus!\n");
- 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);
[CPP]View Plaincopy
- #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\n", "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);
[CPP]View Plaincopy
- #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!\n");
- return 0;
- }
- static int my_remove (struct device *dev)
- {
- PRINTK ("Driver found device unpluged!\n");
- 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\n", "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);
[Linux drivers] [Linux Driver] device driver Model related (i)--Sample code