1、首先定义一个bus,完成insmod后可以在sys/bus下看见
- #include<linux/module.h>
-
#include<linux/kernel.h>
-
#include<linux/init.h>
-
-
#include<linux/string.h>
-
#include<linux/device.h>
-
-
MODULE_AUTHOR("AW");
-
MODULE_LICENSE("GPL");
-
-
static char *Version="imlink 2.0";
-
static int my_match(struct device *dev,struct device_driver *driver)
-
{
-
int result=0;
-
printk("<1>Now prepare to compare!!!!");
-
printk("<1>the device init_name is %s\n",dev->init_name);
-
printk("<1>the new ini_name is %s\n",(dev_name(dev)));
-
printk("<1>the driver name is %s\n",driver->name);
-
result=strncmp(dev_name(dev),driver->name,strlen(driver->name));
-
-
if(result==0)
-
return 1;
-
else
-
return 0;
-
}
-
-
static void my_bus_release(struct device *dev)
-
{
-
printk("<1>my bus released!");
-
}
-
-
-
struct device my_bus=
-
{
-
.init_name="my_bus0",
-
.release=my_bus_release,
-
};
-
-
-
struct bus_type my_bus_type=
-
{
-
.name="my_bus2",
-
.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 my_bus_init(void)
-
{
-
int ret=bus_register(&my_bus_type);
-
if(ret)
-
{
-
return ret;
-
}
-
-
if(bus_create_file(&my_bus_type,&bus_attr_version))
-
{
-
printk("<1>Fail to create version attribute!\n");
-
}
-
-
ret=device_register(&my_bus);
-
-
if(ret)
-
{
-
printk("<1>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、然后注册1个设备
- #include<linux/kernel.h>
-
#include<linux/module.h>
-
#include<linux/init.h>
-
-
#include<linux/string.h>
-
#include<linux/device.h>
-
-
MODULE_AUTHOR("AW");
-
MODULE_LICENSE("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_dev1",
-
.bus=&my_bus_type,
-
.parent=&my_bus,
-
.release=my_dev_release,
-
};
-
-
static ssize_t my_dev_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,my_dev_show,NULL);
-
-
static int my_device_init(void)
-
{
-
int ret=0;
-
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);
3、然后注册1个驱动
- #include <linux/device.h>
-
#include <linux/module.h>
-
#include <linux/kernel.h>
-
#include <linux/init.h>
-
#include <linux/string.h>
-
-
MODULE_AUTHOR("AW");
-
MODULE_LICENSE("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_dev1",
-
.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 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);
其中2,3的顺序可以反,没有影响
阅读(746) | 评论(0) | 转发(0) |