In fact, these three parts involve a wide range, but here we only briefly describe the model.
First, let's take a look at the bus program section:
/* An out-of-registration bus device is added */# include <Linux/device. h> # include <Linux/module. h> # include <Linux/kernel. h> # include <Linux/init. h> # include <Linux/string. h> module_author ("David xie"); module_license ("dual BSD/GPL"); static char * version = "$ revision: 1.9 $ "; /* If bus_id and name are the same, the driver can process the device. */Static int my_match (struct device * Dev, struct device_driver * driver) {return! Strncmp (Dev-> bus_id, driver-> name, strlen (Driver-> name);} static void my_bus_release (struct device * Dev) {printk (kern_debug "My bus release \ n");} struct device my_bus = {. bus_id = "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);/** export a simple attribute. */static ssize_t show_bus_version (Str UCT 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;/* Register bus */ret = bus_register (& my_bus_type); If (RET) return ret; /* Create an attribute file */If (bus_create_file (& my_bus_type, & bus_attr_version) printk (kern_notice "fail to create version attribute! \ N ");/* register a bus device. */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 (& register);} module_init (my_bus_init); module_exit (my_bus_exit );
In this bus Registration Program, the first thing to note is/* register the bus */ret = bus_register (& my_bus_type). Also, pay attention to the structure my_bus_type, which is used to describe the bus. There is also another function: ret = device_register (& my_bus); this struct my_bus is used to describe this bus device. The third thing to note is static int my_match (struct device * Dev, struct device_driver
* Driver) This is a matching function between the I device and the driver, which is a core function of the bus. There are also export_symbol (my_bus); export_symbol (my_bus_type); note that the content of these two struct should be used externally.
Next, let's talk about the device program:
# Include <Linux/device. h> # include <Linux/module. h> # include <Linux/kernel. h> # include <Linux/init. h> # include <Linux/string. h> module_author ("David xie"); module_license ("dual BSD/GPL"); extern struct device my_bus; extern bus_type my_bus_type;/* Why need this? */Static void my_dev_release (struct device * Dev) {} struct device my_dev = {. bus = & my_bus_type ,. parent = & my_bus ,. release = my_dev_release,};/** export a simple attribute. */static ssize_t mydev_show (struct device * Dev, 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; /* initialize the device */strncpy (my_dev.bus_id, "my_dev", bus_id_size); // here my_dev is the name of the device file. /* Register the device */device_register (& my_dev);/* Create an attribute file */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 );
In the device program, strncpy (my_dev.bus_id, "my_dev", bus_id_size); // here my_dev is the name of the device file. The initialization device calls the structure my_dev. The structure of my_dev is as follows:
Struct device my_dev = {
. Bus = & my_bus_type,
. Parent = & my_bus,
. Release = my_dev_release,
};
In the structure my_dev, bus = & my_bus_type calls the attribute of the bus, so the device is mounted on the bus.
Finally, the driver:
# Include <Linux/device. h> # include <Linux/module. h> # include <Linux/kernel. h> # include <Linux/init. h> # include <Linux/string. h> module_author ("David xie"); 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;}/* ① when the driver processes the devices that can be processed by itself on the bus. When registering the driver (driver_register (& my_driver), you can find the device you can process. ② Why can the driver process devices that can be processed by itself. This is done through the bus, using static int my_match (struct device * Dev, struct device_driver * driver ). */Struct device_driver my_driver = {. Name = "my_dev",. Bus = & my_bus_type,. Probe = my_probe, // devices that can be processed on the processing bus.. Remove = my_remove,};/** export a simple attribute. */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; /* register the driver */driver_register (& my_driver);/* Create an attribute file */driver_create_file (& my_driver, & driver_attr_drv); return;} static void my_driver_exit (void) {driver_unregister (& my_driver);} module_init (my_driver_init); module_exit (my_driver_exit );
In the driver,/* register the driver */driver_register (& my_driver); this function calls the structure my_driver. The structure of my_driver is as follows:
Struct device_driver my_driver = {
. Name = "my_dev ",
. Bus = & my_bus_type,
. Probe = my_probe, // devices that can be processed on the processing bus.
. Remove = my_remove,
};
There are two points to note in the structure my_driver: One. name = "my_dev". This is the name of the driver. The name of the driver must match the name of the online device .. Bus = & my_bus_type .. Probe = my_probe, which is a real driver in this function.
Here is just a simple introduction to the model to prevent mistakes.