Loaded from http://blog.csdn.net/sjwangjinbao/article/details/6077225
When writing a bus device driver, it was found that the struct device in the 2.6.33.2 kernel had no members of BUS_ID, and the const char *init_name member was found. However, this member cannot be used directly to set and read the device name.
The method for kernel operation is dev_name and Dev_set_name. The definition in the kernel is:
1) dev_name
Static inline const char *dev_name (const struct device *dev)
{
Return Kobject_name (&dev->kobj);
}
2) Dev_set_name
/**
* Dev_set_name-set a device name
* @dev: Device
* @fmt: format string for the device ' s name
*/
int dev_set_name (struct device *dev, const char *FMT, ...)
{
Va_list Vargs;
int err;
Va_start (Vargs, FMT);
Err = Kobject_set_name_vargs (&dev->kobj, FMT, Vargs);
Va_end (Vargs);
return err;
}
EXPORT_SYMBOL_GPL (Dev_set_name);
For specific examples, please see my next article on bus device-driven examples.
Loaded from: http://blog.csdn.net/sjwangjinbao/article/details/6077236
Here is a summary of the simple bus device-driven framework program.
0, establish the folder Busdevdrv
1, Bus
Set up the bus file bus.c in the folder, the code is as follows.
#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
Module_author ("Sjwangjinbao");
Module_license ("Dual BSD/GPL");
static char *version = "$Revision: 1.0 $";
static int My_match (struct device *dev, struct device_driver)
{
Return!strncmp (Dev_name (Dev), Driver->name, strlen (driver->name));
}
static void My_bus_release (struct device *dev)
{
PRINTK ("My Bus release/n");
}
struct device My_bus = {
. 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 (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;
/* Registered bus/*
ret = Bus_register (&my_bus_type);
if (ret)
{
return ret;
}
/* Create property File/*
if (Bus_create_file (&my_bus_type, &bus_attr_version))
{
PRINTK ("Fail to create version attribute!/n");
}
/* Initialize BUS device * *
Dev_set_name (&my_bus, "My_bus0");
/* Register BUS device * *
ret = Device_register (&my_bus);
if (ret)
{
PRINTK ("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);
2, Equipment
Set up the device file dev.c under the folder, the code is as follows.
#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
Module_author ("Sjwangjinbao");
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 = {
. Bus = &my_bus_type,
. Parent = &my_bus,
. Release = My_dev_release,
};
/*
* Export a simple attribute.
*/
Static ssize_t mydev_show (struct device *dev,struct device_attribute *attr, char *buf)
{
Return sprintf (buf, "%s/n", "It is Sjwangjinbao device!");
}
Static device_attr (Dev, S_irugo, mydev_show, NULL);
static int __init my_device_init (void)
{
int ret = 0;
/* Initialize device/*
Dev_set_name (&my_dev, "My_dev");
/* Registration Equipment * *
Device_register (&my_dev);
/* Create property 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);
3. Drive
Set up the driver file drv.c under the folder, the code is as follows.
#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
Module_author ("Sjwangjinbao");
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",
. Probe = My_probe,
};
/*
* Export a simple attribute.
*/
Static ssize_t mydriver_show (struct device_driver *driver, char *buf)
{
Return sprintf (buf, "%s/n", "It is Sjwangjinbao driver!");
}
Static driver_attr (DRV, S_irugo, Mydriver_show, NULL);
static int __init my_driver_init (void)
{
int ret = 0;
/* Registration Driver * *
Driver_register (&my_driver);
/* Create property File/*
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);
4, Makefile
Set up the makefile under the folder, as follows.
Ifneq ($ (kernelrelease),)
Obj-m: = BUS.O dev.o drv.o
Else
Kdir: =/usr/src/linux-2.6.33.2
All
Make-c $ (Kdir) m=$ (PWD) modules
Clean
Rm-f *.ko *.o *.mod.o *.mod.c *.symvers
endif
5, make
Executing the Make command under the folder will get Bus.ko, Dev.ko, Drv.ko three files.
6. Add Module
Use the root user to add 3 modules, in turn, by calling the following command:
Insmod Bus.ko
Insmod Dev.ko
Insmod.drv.ko
At this time, will be found under the/sys/bus My_bus already exist. Look at the/sys/bus/my_bus/devices/and find that our My_dev devices are already there, and/sys/bus/my_bus/drivers/to see if we can find our My_dev drivers. In the/sys/bus/my_bus/devices/my_dev/driver to see, found that the My_dev driver already exists, which means that our My_dev devices and My_dev drivers have been linked together. Also, a My_dev device is found under the/sys/bus/my_bus/drivers/my_dev, which means that our My_dev devices and My_dev drivers are already connected.
In addition, we can also view their properties.
1 under/sys/bus/my_bus, we found a version file that looked at it with cat and the output was:
$Revision: 1.9 $
This is exactly what we specified in the program.
2 in/sys/bus/my_bus/devices/my_dev, we will find a dev file, with cat view it, the result is:
This is my device!
This is exactly what we have specified in our program.
3 in/sys/bus/my_bus/drivers/my_dev, we will also find DRV files, with cat to see the results are:
This is my driver!
Of course, this is what we specify in the program.