Search code examples
clinux-kernelkernel-modulei2c

i2c_register_board_info symbol is undefined


I am trying to write a kernel module for a BeagleBone Black that would communicate with my custom I2C slave device. I tried following several kernel module development tutorials, they all seem incomplete at some point, or assume that I know something I clearly don't... My current problem now is that the Makefile doesnt see the i2c_register_board_info symbol. I am writing this driver as a separate module, its not compiled during kernel compilation. Also, I have enabled the I2C tools when building using buildroot. Using the I2C tools I am able to detect and interface with my device. My Makefile looks as follows:

MODULE_NAME = PowerManagerDriver

PWD := $(shell pwd)
SRC_DIR = user_files
BUILD_DIR = build
BUILD_EXT = *.o .*.cmd *.ko *.mod.c *.order *.symvers *.dwo

SRCS = $(SRC_DIR)/main.c $(SRC_DIR)/pmd_i2c.c 
OBJS = $(SRCS:.c=.o)

obj-m += $(MODULE_NAME).o 
$(MODULE_NAME)-y = $(OBJS)

KERNELDIR ?= /home/lukasz/brl/Machine/beaglebone/build/linux-a75d8e93056181d512f6c818e8627bd4554aaf92

all: default

default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
    mv $(SRC_DIR)/*.o $(BUILD_DIR)/
    mv $(BUILD_EXT) $(BUILD_DIR)/ &> /dev/null

clean:
    rm -rf $(BUILD_DIR)/*
    rm -rf $(BUILD_EXT)

make output:

13:45:33 **** Incremental Build of configuration Default for project PowerManagerDriver ****
make ARCH=arm CROSS_COMPILE=arm-buildroot-linux-uclibcgnueabihf- -j6 all 
make -C /home/lukasz/brl/Machine/beaglebone/build/linux-a75d8e93056181d512f6c818e8627bd4554aaf92 M=/home/lukasz/eclipse-workspace/PowerManagerDriver modules
make[1]: Entering directory '/home/lukasz/brl/Machine/beaglebone/build/linux-a75d8e93056181d512f6c818e8627bd4554aaf92'
  CC [M]  /home/lukasz/eclipse-workspace/PowerManagerDriver/user_files/main.o
  CC [M]  /home/lukasz/eclipse-workspace/PowerManagerDriver/user_files/pmd_i2c.o
/home/lukasz/eclipse-workspace/PowerManagerDriver/user_files/pmd_i2c.c:99:26: warning: ‘pdm_i2cClient’ defined but not used [-Wunused-variable]
 static struct i2c_client pdm_i2cClient = { 0 };
                          ^
  LD [M]  /home/lukasz/eclipse-workspace/PowerManagerDriver/PowerManagerDriver.o
  Building modules, stage 2.
  MODPOST 1 modules
WARNING: "i2c_register_board_info" [/home/lukasz/eclipse-workspace/PowerManagerDriver/PowerManagerDriver.ko] undefined!
  CC      /home/lukasz/eclipse-workspace/PowerManagerDriver/PowerManagerDriver.mod.o
  LD [M]  /home/lukasz/eclipse-workspace/PowerManagerDriver/PowerManagerDriver.ko
make[1]: Leaving directory '/home/lukasz/brl/Machine/beaglebone/build/linux-a75d8e93056181d512f6c818e8627bd4554aaf92'
mv user_files/*.o build/
mv *.o .*.cmd *.ko *.mod.c *.order *.symvers *.dwo build/ &> /dev/null

13:45:35 Build Finished. 0 errors, 1 warnings. (took 1s.394ms)

So far I have only few functions for I2C interfacing:

static int pdm_i2cProbe(struct i2c_client* client,
                        const struct i2c_device_id* id)
{
    PMD_ASSERT(client);
    PMD_ASSERT(id);

    return 0;
}

static struct i2c_driver pdm_i2cDriver =
{
    .driver =
    {
        .name = "pdm-driver",
        .owner = THIS_MODULE,
    },

    .probe = pdm_i2cProbe,
};

static struct i2c_board_info pdm_i2cBoardInfo[] =
{
    {
        I2C_BOARD_INFO("pdm-driver", 0x30),
        .irq = 69,
    },
};

/**
 * @brief   Initializes the I2C module.
 * @param   busNr: The I2C peripheral number on which the device is connected.
 * @return  \ref e_pdmStatus_OK on succesfull init.
 */
pmdStatus_t pdm_i2cInit(const unsigned int busNr)
{
    if (i2c_register_board_info((int)busNr, pdm_i2cBoardInfo,
            ARRAY_SIZE(pdm_i2cBoardInfo)))
        return e_pmdStatus_BADPARAM;

    if (i2c_add_driver(&pdm_i2cDriver))
        return e_pmdStatus_EXE;

    return e_pmdStatus_OK;
}

At this point even though I dont probe anything, as the probe function is empty, I was hoping to at least load the module correctly. I dont even get to the testing phase since the i2c_register_board_info is not found. My .config file for when building the kernel consists of those lines:

# I2C support
#
CONFIG_I2C=y
CONFIG_I2C_BOARDINFO=y
CONFIG_I2C_COMPAT=y
CONFIG_I2C_CHARDEV=y
# CONFIG_I2C_MUX is not set
CONFIG_I2C_HELPER_AUTO=y

I have found this topic previously i2c registering macro not found? but I can't get anything out of it.

When trying to load the module with insmod as requested:

# uname -a
Linux buildroot 4.9.59 #1 SMP Fri Oct 5 11:55:54 CEST 2018 armv7l GNU/Linux
# insmod PowerManagerDriver.ko
[   39.438108] PowerManagerDriver: loading out-of-tree module taints kernel.
[   39.445800] PowerManagerDriver: Unknown symbol i2c_register_board_info (err 0)
[   39.455743] PowerManagerDriver: Unknown symbol i2c_register_board_info (err 0)
insmod: can't insert 'PowerManagerDriver.ko': unknown symbol in module, or unknown parameter
#

Solution

  • If your hardware is 'guaranteed' to be there, you do not need to define the device imperatively. You can use device tree (dtb) to declare the existence of your slave at a particular address on a particular bus, provide params to your driver etc. So your module never needs to do the register board info thing.

    This is explained in the docs: https://www.kernel.org/doc/Documentation/i2c/instantiating-devices

    In other words you should be able to write a driver for your slave device as a kernel module using only open firmware (of) hooks and a populated device tree (dtb).