20150226 IMX257 Bus device driver Model programming bus Chapter (II)
2015-02-26 Li Hai along
Before we explained a simple bus driver, the purpose is to create a file under/sys/bus/, but this is not enough, because the bus is also a device, if you want to let the system know, you must register with Device_register.
Here, we start to register a bus so that the bus can contain the properties file, as well as the device files, and the driver.
Let the driver and device files interconnect, this is the real purpose of the bus.
First, the program analysis
The front has been very detailed, we are here on the basis of the above to add:
1. Define the overall device structure
and implement the release function of the device;
2. Export the bus device
3. Registering device drivers
4. Uninstalling the device
Second, the compilation test:
After loading successfully, because the bus is also a device, so there is my_bus this directory under/sys/bus/
And under the/sys/device/also generated a directory, the name of our My_bus Init_name
Attach the MY_BUS.C driver:
1#include <linux/device.h>2#include <linux/module.h>3#include <linux/kernel.h>4#include <linux/init.h>5#include <linux/string.h>6 7 8 Static Char*version ="$LoverXueEr: 1.0 $";9 Ten //detects if the driver matches the device, dev->bus_id and driver->name are equal One Static intMy_match (structDevice *dev,structDevice_driver *driver) { A return!STRNCMP (Dev_name (Dev), Driver->name,strlen (driver->name)); - } - the Static voidMy_bus_release (structDevice *Dev) { -Printk"<0>my bus release\n"); - } - + //set the name of the device Dev_set_name (&dev, "name"); - structDevice My_bus = { +. Init_name ="My_bus0", A. Release =My_bus_release, at }; - - structBus_type My_bus_type = { -. Name ="My_bus", -. match =My_match, - }; inExport_symbol (My_bus);//Export Symbols - Export_symbol (my_bus_type); to + //Show bus version number - Staticssize_t Show_bus_version (structBus_type *bus,Char*buf) { the returnsnprintf (Buf,page_size,"%s\n", Version); * } $ Panax Notoginseng //generate the bus_attr_version structure behind - Staticbus_attr (Version,s_irugo, show_bus_version, NULL); the + Static int__init My_bus_init (void){ A intret; the /*Register Bus*/ +ret = Bus_register (&my_bus_type); - if(ret) $ returnret; $ /*Create a property file*/ - if(Bus_create_file (&my_bus_type, &bus_attr_version)) -Printk"<0>fail to create version attribute! \ n"); the - /*registering a bus device*/Wuyiret = Device_register (&My_bus); the if(ret) -Printk"<0>fail to register Device:my_bus"); Wu returnret; - } About $ Static voidMy_bus_exit (void){ -Bus_unregister (&my_bus_type); -Device_unregister (&My_bus); - } A + Module_init (my_bus_init); the Module_exit (my_bus_exit); - $ theModule_author ("Lover Snow Child"); theModule_license ("GPL");
View Code
Third, error resolution:
1. Error: ' struct device ' has no member named ' BUS_ID '
In the new version of the kernel, the device has not bus_id this idiom, instead of the. Init_name.
Workaround:
Modify the. bus_id below the device to. Init_name,:
Then, dev->bus_id in the match function in the program is modified to Dev_name (dev), you can
20150226 IMX257 Bus device driver Model programming bus Chapter (II)