前三步跟平台相关,从这里开始就只与具体的设备驱动相关了,这里我选取FLD00060 光距感的驱动进行说明。
// 3rdparty/lsensor/fld60/special/driver/FLD60.c
static int __init fld60_pls_init(void)
{
int temp=0;
printk("baker :%s\n",__func__);
fld60_pls_config_pins();
printk("after baker :%s\n",__func__);
temp = i2c_add_driver(&fld60_pls_driver);
printk("add driver result = %d--\n",temp);
return temp;
}
//kernel/include/linux/i2c.h
static inline int i2c_add_driver(struct i2c_driver *driver)
{
return i2c_register_driver(THIS_MODULE, driver);
}
/*
* An i2c_driver is used with one or morei2c_client (device) nodes to access
* i2c slave chips, on a bus instanceassociated with some i2c_adapter.
*/
//kernel/drivers/i2c/i2c-core.c
int i2c_register_driver(struct module *owner, struct i2c_driver*driver)
{
int res;
/* Can't register until after drivermodel init */
if (unlikely(WARN_ON(!i2c_bus_type.p)))
return -EAGAIN;
/* add the driver to the list of i2cdrivers in the driver core */
driver->driver.owner = owner;
driver->driver.bus =&i2c_bus_type;
/* Whenregistration returns, the driver core
* will have called probe() for allmatching-but-unbound devices.
*/
//i2c_driver结构体中的driver变量是个device_driver类型的结构体
res = driver_register(&driver->driver);
if (res)
return res;
pr_debug("i2c-core: driver [%s]registered\n", driver->driver.name);
INIT_LIST_HEAD(&driver->clients);
/* Walk the adapters that are alreadypresent */
mutex_lock(&core_lock);
bus_for_each_dev(&i2c_bus_type, NULL,driver, __process_new_driver);
mutex_unlock(&core_lock);
return 0;
}
/**
* driver_register - register driver with bus
* @drv: driver to register
*
* We pass off most of the work to thebus_add_driver() call,
* since most of the things we have to do dealwith the bus
* structures.
*/
//kernel/drivers/base/driver.c
int driver_register(struct device_driver *drv)
{
int ret;
struct device_driver *other;
BUG_ON(!drv->bus->p);
if ((drv->bus->probe &&drv->probe) ||
(drv->bus->remove && drv->remove) ||
(drv->bus->shutdown && drv->shutdown))
printk(KERN_WARNING "Driver'%s' needs updating - please use "
"bus_typemethods\n", drv->name);
other = driver_find(drv->name,drv->bus);
if (other) {
put_driver(other);
printk(KERN_ERR "Error:Driver '%s' is already registered, "
"aborting...\n",drv->name);
return -EBUSY;
}
ret = bus_add_driver(drv);
if (ret)
return ret;
ret = driver_add_groups(drv,drv->groups);
if (ret)
bus_remove_driver(drv);
return ret;
}
/**
* bus_add_driver - Add a driver to the bus.