ldd3的example是基于2.6.10内核写的,在新的2.6.3x上编译不过。下面是我做的修改:
1.Makefile:
把CFLAGS替换成EXTRA_CFLAGS.
2.新建一个空的config.h:
得到内核版本:uname -r
2.6.38-12-generic
cd /lib/modules/2.6.38-12-generic/build/include/linux
touch config.h
或者在config.h中增加以下内容:
3. lddbus.c:
#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include "lddbus.h"
MODULE_AUTHOR("Jonathan Corbet");
MODULE_LICENSE("Dual BSD/GPL");
static char *Version = "$Revision: 1.9 $";
/*
* Respond to hotplug events.
*/
static int ldd_hotplug(struct device *dev, char **envp, int num_envp,
char *buffer, int buffer_size)
{
envp[0] = buffer;
if (snprintf(buffer, buffer_size, "LDDBUS_VERSION=%s",
Version) >= buffer_size)
return -ENOMEM;
envp[1] = NULL;
return 0;
}
[b][color=red]static int ldd_uevent(struct device *dev, struct kobj_uevent_env *env) {
env->envp[0] = env->buf;
if (snprintf(env->buf, env->buflen, "LDDBUS_VERSION=%s", Version) >= env->buflen)
return -ENOMEM;
env->envp[1] = NULL;
return 0;
} [/color][/b]
/*
* Match LDD devices to drivers. Just do a simple name test.
*/
static int ldd_match(struct device *dev, struct device_driver *driver)
{
return !strncmp(dev->[color=red][b]init_name[/b][/color], driver->name, strlen(driver->name));
}
/*
* The LDD bus device.
*/
static void ldd_bus_release(struct device *dev)
{
printk(KERN_DEBUG "lddbus release\n");
}
struct device ldd_bus = {
.[color=red][b]init_name[/b][/color] = "ldd0",
.release = ldd_bus_release
};
/*
* And the bus type.
*/
struct bus_type ldd_bus_type = {
.name = "ldd",
.match = ldd_match,
.[color=red][b]uevent = ldd_uevent,[/b][/color]};
/*
* 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);
/*
* LDD devices.
*/
/*
* For now, no references to LDDbus devices go out which are not
* tracked via the module reference count, so we use a no-op
* release function.
*/
static void ldd_dev_release(struct device *dev)
{ }
int register_ldd_device(struct ldd_device *ldddev)
{
printk(KERN_ALERT "register_ldd_device in\n");
ldddev->dev.bus = &ldd_bus_type;
ldddev->dev.parent = &ldd_bus;
ldddev->dev.release = ldd_dev_release;
strncpy(ldddev->dev.[color=red][b]init_name[/b][/color], ldddev->name, [color=red][b]20[/b][/color]);
return device_register(&ldddev->dev);
}
EXPORT_SYMBOL(register_ldd_device);
void unregister_ldd_device(struct ldd_device *ldddev)
{
printk(KERN_ALERT "unregister_ldd_device in\n");
device_unregister(&ldddev->dev);
}
EXPORT_SYMBOL(unregister_ldd_device);
/*
* Crude driver interface.
*/
static ssize_t show_version(struct device_driver *driver, char *buf)
{
struct ldd_driver *ldriver = to_ldd_driver(driver);
sprintf(buf, "%s\n", ldriver->version);
return strlen(buf);
}
int register_ldd_driver(struct ldd_driver *driver)
{
printk(KERN_ALERT "register_ldd_driver in\n");
int ret;
driver->driver.bus = &ldd_bus_type;
ret = driver_register(&driver->driver);
if (ret)
return ret;
driver->version_attr.attr.name = "version";
[color=red][b]// driver->version_attr.attr.owner = driver->module;[/b][/color] driver->version_attr.attr.mode = S_IRUGO;
driver->version_attr.show = show_version;
driver->version_attr.store = NULL;
return driver_create_file(&driver->driver, &driver->version_attr);
}
void unregister_ldd_driver(struct ldd_driver *driver)
{
printk(KERN_ALERT "unregister_ldd_driver in\n");
driver_unregister(&driver->driver);
}
EXPORT_SYMBOL(register_ldd_driver);
EXPORT_SYMBOL(unregister_ldd_driver);
static int __init ldd_bus_init(void)
{
int ret;
printk(KERN_DEBUG "ldd_bus_init: register bus...\n");
ret = bus_register(&ldd_bus_type);
if (ret)
return ret;
printk(KERN_ALERT "ldd_bus_init: bus create file ...\n");
if (bus_create_file(&ldd_bus_type, &bus_attr_version))
printk(KERN_NOTICE "Unable to create version attribute\n");
printk(KERN_ALERT "ldd_bus_init: register device...\n");
ret = device_register(&ldd_bus);
if (ret)
printk(KERN_NOTICE "Unable to register ldd0\n");
printk(KERN_ALERT "ldd_bus_init: done\n");
return ret;
}
static void ldd_bus_exit(void)
{
device_unregister(&ldd_bus);
bus_unregister(&ldd_bus_type);
}
module_init(ldd_bus_init);
module_exit(ldd_bus_exit);
4. 编译,生成lddbus.ko.
make
5. 装载/卸载/显示module:
insmod lddbus.ko
rmmod lddbus
lsmod |grep lddbus
dmesg查看log:
[ 654.896657] ldd_bus_init: register bus...
[ 654.898749] ldd_bus_init: bus create file ...
[ 654.898762] ldd_bus_init: register device...
[ 654.898819] ldd_bus_init: done
或者在/var/log/kern.log中看log。
6. 结果:
ubuntu@ubuntu-VirtualBox:/sys/bus/ldd$ ls
devices drivers drivers_autoprobe drivers_probe uevent version
ubuntu@ubuntu-VirtualBox:/sys/devices/ldd0$ ls
power uevent
ldd3源码下载:
https://github.com/jesstess/ldd3-examples/
ldd3 PDF下载:
http://lwn.net/Kernel/LDD3/
ldd3 original example:
[url]http://dl.iteye.com/topics/download/6742d5d5-9421-361e-a40a-3ec87e781770[/url]
ldd3 github example:
[url]http://dl.iteye.com/topics/download/c5deb5fa-9c54-380f-906d-2e8c947780c0[/url]
1.Makefile:
把CFLAGS替换成EXTRA_CFLAGS.
2.新建一个空的config.h:
得到内核版本:uname -r
2.6.38-12-generic
cd /lib/modules/2.6.38-12-generic/build/include/linux
touch config.h
或者在config.h中增加以下内容:
#ifndef _LINUX_CONFIG_H
#define _LINUX_CONFIG_H
/* This file is no longer in use and kept only for backward compatibility.
* autoconf.h is now included via -imacros on the commandline
*/
#endif
3. lddbus.c:
#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include "lddbus.h"
MODULE_AUTHOR("Jonathan Corbet");
MODULE_LICENSE("Dual BSD/GPL");
static char *Version = "$Revision: 1.9 $";
/*
* Respond to hotplug events.
*/
static int ldd_hotplug(struct device *dev, char **envp, int num_envp,
char *buffer, int buffer_size)
{
envp[0] = buffer;
if (snprintf(buffer, buffer_size, "LDDBUS_VERSION=%s",
Version) >= buffer_size)
return -ENOMEM;
envp[1] = NULL;
return 0;
}
[b][color=red]static int ldd_uevent(struct device *dev, struct kobj_uevent_env *env) {
env->envp[0] = env->buf;
if (snprintf(env->buf, env->buflen, "LDDBUS_VERSION=%s", Version) >= env->buflen)
return -ENOMEM;
env->envp[1] = NULL;
return 0;
} [/color][/b]
/*
* Match LDD devices to drivers. Just do a simple name test.
*/
static int ldd_match(struct device *dev, struct device_driver *driver)
{
return !strncmp(dev->[color=red][b]init_name[/b][/color], driver->name, strlen(driver->name));
}
/*
* The LDD bus device.
*/
static void ldd_bus_release(struct device *dev)
{
printk(KERN_DEBUG "lddbus release\n");
}
struct device ldd_bus = {
.[color=red][b]init_name[/b][/color] = "ldd0",
.release = ldd_bus_release
};
/*
* And the bus type.
*/
struct bus_type ldd_bus_type = {
.name = "ldd",
.match = ldd_match,
.[color=red][b]uevent = ldd_uevent,[/b][/color]};
/*
* 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);
/*
* LDD devices.
*/
/*
* For now, no references to LDDbus devices go out which are not
* tracked via the module reference count, so we use a no-op
* release function.
*/
static void ldd_dev_release(struct device *dev)
{ }
int register_ldd_device(struct ldd_device *ldddev)
{
printk(KERN_ALERT "register_ldd_device in\n");
ldddev->dev.bus = &ldd_bus_type;
ldddev->dev.parent = &ldd_bus;
ldddev->dev.release = ldd_dev_release;
strncpy(ldddev->dev.[color=red][b]init_name[/b][/color], ldddev->name, [color=red][b]20[/b][/color]);
return device_register(&ldddev->dev);
}
EXPORT_SYMBOL(register_ldd_device);
void unregister_ldd_device(struct ldd_device *ldddev)
{
printk(KERN_ALERT "unregister_ldd_device in\n");
device_unregister(&ldddev->dev);
}
EXPORT_SYMBOL(unregister_ldd_device);
/*
* Crude driver interface.
*/
static ssize_t show_version(struct device_driver *driver, char *buf)
{
struct ldd_driver *ldriver = to_ldd_driver(driver);
sprintf(buf, "%s\n", ldriver->version);
return strlen(buf);
}
int register_ldd_driver(struct ldd_driver *driver)
{
printk(KERN_ALERT "register_ldd_driver in\n");
int ret;
driver->driver.bus = &ldd_bus_type;
ret = driver_register(&driver->driver);
if (ret)
return ret;
driver->version_attr.attr.name = "version";
[color=red][b]// driver->version_attr.attr.owner = driver->module;[/b][/color] driver->version_attr.attr.mode = S_IRUGO;
driver->version_attr.show = show_version;
driver->version_attr.store = NULL;
return driver_create_file(&driver->driver, &driver->version_attr);
}
void unregister_ldd_driver(struct ldd_driver *driver)
{
printk(KERN_ALERT "unregister_ldd_driver in\n");
driver_unregister(&driver->driver);
}
EXPORT_SYMBOL(register_ldd_driver);
EXPORT_SYMBOL(unregister_ldd_driver);
static int __init ldd_bus_init(void)
{
int ret;
printk(KERN_DEBUG "ldd_bus_init: register bus...\n");
ret = bus_register(&ldd_bus_type);
if (ret)
return ret;
printk(KERN_ALERT "ldd_bus_init: bus create file ...\n");
if (bus_create_file(&ldd_bus_type, &bus_attr_version))
printk(KERN_NOTICE "Unable to create version attribute\n");
printk(KERN_ALERT "ldd_bus_init: register device...\n");
ret = device_register(&ldd_bus);
if (ret)
printk(KERN_NOTICE "Unable to register ldd0\n");
printk(KERN_ALERT "ldd_bus_init: done\n");
return ret;
}
static void ldd_bus_exit(void)
{
device_unregister(&ldd_bus);
bus_unregister(&ldd_bus_type);
}
module_init(ldd_bus_init);
module_exit(ldd_bus_exit);
4. 编译,生成lddbus.ko.
make
5. 装载/卸载/显示module:
insmod lddbus.ko
rmmod lddbus
lsmod |grep lddbus
dmesg查看log:
[ 654.896657] ldd_bus_init: register bus...
[ 654.898749] ldd_bus_init: bus create file ...
[ 654.898762] ldd_bus_init: register device...
[ 654.898819] ldd_bus_init: done
或者在/var/log/kern.log中看log。
6. 结果:
ubuntu@ubuntu-VirtualBox:/sys/bus/ldd$ ls
devices drivers drivers_autoprobe drivers_probe uevent version
ubuntu@ubuntu-VirtualBox:/sys/devices/ldd0$ ls
power uevent
ldd3源码下载:
https://github.com/jesstess/ldd3-examples/
ldd3 PDF下载:
http://lwn.net/Kernel/LDD3/
ldd3 original example:
[url]http://dl.iteye.com/topics/download/6742d5d5-9421-361e-a40a-3ec87e781770[/url]
ldd3 github example:
[url]http://dl.iteye.com/topics/download/c5deb5fa-9c54-380f-906d-2e8c947780c0[/url]