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

bus device driver写测试驱动的时候 空指针出错
内核版本:linux-2.6.33 宿主机ubuntu9.04 板子fl2440
最近学到这,也是按着某教学机构的程序照着写的3个驱动bus,device,driver
贴下代码
bus.c
C/C++ code

#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>

MODULE_AUTHOR("Ying");
MODULE_LICENSE("Dual BSD/GPL");

static char *Version = "$Vying__1.0$" ;

static int ying_match(struct device * dev , struct device_driver * drv){
    return !strncmp(dev->init_name, drv->name, strlen(drv->name));
}

static void my_bus_release(struct device *dev)
{
    printk(KERN_DEBUG "my bus release\n");
}

struct device ying_bus = {
    .init_name   = "ying_bus0",
    .release  = my_bus_release,
};


struct bus_type my_bus_type = {
    .name = "ying_bus" ,
    .match = ying_match ,
};

EXPORT_SYMBOL(ying_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 __init my_bus_init(void){
    int ret;

    /*注册总线*/
    ret = bus_register(&my_bus_type);

    if(ret)
        return ret;
    printk("%d---",ret);
    /*注册属性文件*/
    if(bus_create_file(&my_bus_type , &bus_attr_version))
        printk("fail to attribute\n");

    /*注册总线设备*/
    ret = device_register(&ying_bus);
    if (ret)
        printk(KERN_NOTICE "Fail to register device:my_bus!\n");
        
    return ret;
}

static void my_bus_exit(void){
    device_unregister(&ying_bus);
    bus_unregister(&my_bus_type);
}
module_init(my_bus_init);
module_exit(my_bus_exit);



device.c
C/C++ code

#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>


MODULE_AUTHOR("ying");
MODULE_LICENSE("Dual BSD/GPL");

extern struct device ying_bus; 
extern struct bus_type my_bus_type;

static void my_dev_release (struct device *dev){
    printk("ying_dev release!!~\n");
}

struct device ying_dev = {
    .init_name = "ying_dev",
    .bus = &my_bus_type,
    .parent = &ying_bus,
    .release = my_dev_release,
};


static ssize_t mydev_show(struct device *dev,struct device_attribute *attr, char *buf)
{
    return sprintf(buf, "%s\n", "This is ying device!");
}

static DEVICE_ATTR(dev, S_IRUGO, mydev_show, NULL);

static int __init my_device_init(void){
    int ret = 0;
    
    ret = device_register(&ying_dev);
    
    if(ret){
        printk("fail to ying_dev\n");
        return ret;
    }
    device_create_file(&ying_dev,&dev_attr_dev);
    
    return ret ;
}

static void my_device_exit(void){
    void device_unregister(struct device* dev);
}


module_init(my_device_init);
module_exit(my_device_exit);



driver
C/C++ code

#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>

MODULE_AUTHOR("David Xie");
MODULE_LICENSE("Dual BSD/GPL");

extern struct bus_type my_bus_type;


static int my_probe(struct device *dev)
{
    printk("Driver found device which ying driver can handle!\n");
    return 0;
}

static int my_remove(struct device *dev)
{
    printk("Driver found device unpluged!\n");
    return