日期:2014-05-16  浏览次数:20475 次

ldd3中的lddbus修改
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中增加以下内容:
#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;
}

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; 

/*
* 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->init_name, 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 = {
.init_name   = "ldd0",
.release  = ldd_bus_release
};


/*
* And the bus type.
*/
struct bus_type ldd_bus_type = {
.name = "ldd",
.match = ldd_match,
.uevent  = ldd_uevent,};

/*
* 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.init_name, ldddev->name, 20);
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);