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

小结linux框架下函数如何可以使其被其他文件调用

1:通常export该函数。

2:获取结构体指针:在driver设个全局变量。

下面贴出自己写的xenon flash driver部分代码。

struct xenon_flash_chip {
    struct i2c_client   *xenon_flash_client;
};

static struct xenon_flash_chip *xenon_chip;

//this is just for debug
int xenon_flash_reread(void)
{
int err = 0;
u8 data;
struct xenon_flash_chip *chip;
if(xenon_chip==NULL)
return err;
chip=xenon_chip;
//read 07
err = xenon_flash_i2c_read(chip->xenon_flash_client,0x07,&data);
if (err < 0)
return err;
    CDBG("[xenon_flash]reg_control reg=0x07 data=0x0%x\n",data);
    //read 06
err = xenon_flash_i2c_read(chip->xenon_flash_client,0x06,&data);
if (err < 0)
return err;
    CDBG("[xenon_flash]reg_control reg=0x06 data=0x0%x\n",data);
//read 04
err = xenon_flash_i2c_read(chip->xenon_flash_client,0x04,&data);
if (err < 0)
return err;
    CDBG("[xenon_flash]reg_control reg=0x04 data=0x0%x\n",data);

return err;
}
EXPORT_SYMBOL(xenon_flash_reread);

static int __devinit xenon_flash_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
int rc = 0;
    static const u32 i2c_funcs = I2C_FUNC_SMBUS_BYTE_DATA |
I2C_FUNC_SMBUS_WRITE_WORD_DATA;

    struct xenon_flash_chip *chip;
struct xenon_flash_platform_data *pdata;
CDBG("xenon_flash_i2c_probe called!\n");

/* check */
if (client->dev.platform_data == NULL) {
dev_err(&client->dev, "xenon_flash platform data is NULL.
exiting.\n");
return -ENODEV;
}
/* Copy to global variable */
pdata = client->dev.platform_data;

    /*check i2c func*/
if (!i2c_check_functionality(client->adapter, i2c_funcs))
return -ENOSYS;

    /* Allocate memory for driver data */
chip = kzalloc(sizeof(struct xenon_flash_chip), GFP_KERNEL);
if (!chip)
return -ENOMEM;
xenon_chip = chip;
i2c_set_clientdata(client,chip);
chip->xenon_flash_client = client;
rc = xenon_flash_init_hw(chip,pdata);
if (rc < 0)
CDBG("xenon_flash_init_hw initlised failed!\n");

return 0;
}

在driver调试过程中,通常需要将关键函数export出来,在其他地方extern声明再调用调试。

以上摘自自己写的疝气闪光灯driver.