android linux内核分析(-)- led(上)

时间:2022-03-21 16:09:45

linux版本:version linux 3.x

android版本:android 4.4.2

分析实例:leds-s3c24xx.c

分析目录:dirvers/leds/

1.makefile

# LED Core
obj-$(CONFIG_NEW_LEDS) += led-core.o
obj-$(CONFIG_LEDS_CLASS) += led-class.o
obj-$(CONFIG_LEDS_TRIGGERS) += led-triggers.o

# LED Platform Drivers
obj-$(CONFIG_LEDS_88PM860X) += leds-88pm860x.o
obj-$(CONFIG_LEDS_ATMEL_PWM) += leds-atmel-pwm.o
obj-$(CONFIG_LEDS_BD2802) += leds-bd2802.o
obj-$(CONFIG_LEDS_LOCOMO) += leds-locomo.o
obj-$(CONFIG_LEDS_LM3530) += leds-lm3530.o
obj-$(CONFIG_LEDS_LM3533) += leds-lm3533.o
obj-$(CONFIG_LEDS_LM3642) += leds-lm3642.o
obj-$(CONFIG_LEDS_MIKROTIK_RB532) += leds-rb532.o
obj-$(CONFIG_LEDS_S3C24XX) += leds-s3c24xx.o
这个只是其中的一部分,无非是根据是否配置宏来决定是否编译对应的c文件。

2.Kconfig

config LEDS_S3C24XX
tristate "LED Support for Samsung S3C24XX GPIO LEDs"
depends on LEDS_CLASS
depends on ARCH_S3C24XX
help
This option enables support for LEDs connected to GPIO lines
on Samsung S3C24XX series CPUs, such as the S3C2410 and S3C2440.
这个其实是配置 CONFIG_LEDS_S3C24XX
但是一般标准的配置其实是在\android\common\common\arch\arm\configs/下有很多的def_config,找到对应的你平台上的config即可,看其中是否配置了这些选项,找不到的话有两种办法看你修改的这个文件是否编译了

(1)主动在你的这个文件中添加错误,编译一次看是否报错

(2)在andoid中编译的out/target/product/xxx/obj/KERNEL_OBJ/中的.config中可以找到是否已经配置了,前提是已经编译了。

还有一个方式是直接让这个makefile中修改,直接了当,即

obj-y +=leds-s3c24xx.o
这个有点暴力,不推荐

3.C文件

module_platform_driver(s3c24xx_led_driver);

MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
MODULE_DESCRIPTION("S3C24XX LED driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:s3c24xx_led");

下面的宏就不介绍了,看

module_platform_driver(s3c24xx_led_driver);
定义是这样的,Platform_device.h     \include\linux 

/* module_platform_driver() - Helper macro for drivers that don't do
* anything special in module init/exit. This eliminates a lot of
* boilerplate. Each module may only use this macro once, and
* calling it replaces module_init() and module_exit()
*/
#define module_platform_driver(__platform_driver) \
module_driver(__platform_driver, platform_driver_register, \
<pre name="code" class="cpp">#define module_driver(__driver, __register, __unregister, ...) \static int __init __driver##_init(void) \{ \	return __register(&(__driver) , ##__VA_ARGS__); \} \module_init(__driver##_init); \static void __exit __driver##_exit(void) \{ \	__unregister(&(__driver) , ##__VA_ARGS__); \} \module_exit(__driver##_exit);

 

唉,不过是换汤不换药,依然是__init,__exit,platform_driver_register(struct platform_driver *); platform_driver_unregister(struct platform_driver *);

好看platform_driver_register(struct platform_driver *);  在Platform.c (common\drivers\base)

int platform_driver_register(struct platform_driver *drv)
{
drv->driver.bus = &platform_bus_type;
if (drv->probe)
drv->driver.probe = platform_drv_probe;
if (drv->remove)
drv->driver.remove = platform_drv_remove;
if (drv->shutdown)
drv->driver.shutdown = platform_drv_shutdown;

return driver_register(&drv->driver);
}

@1:此时的drv为传入参数s3c24xx_led_driver,而s3c24xx_led_driver的定义是

static struct platform_driver s3c24xx_led_driver = {
.probe = s3c24xx_led_probe,
.remove = s3c24xx_led_remove,
.driver = {
.name = "s3c24xx_led",
.owner = THIS_MODULE,
},
};
而platform_driver 的定义是

struct platform_driver {
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_t state);
int (*resume)(struct platform_device *);
struct device_driver driver;
const struct platform_device_id *id_table;
};
需要注意的是struct device_driver driver; deice_driver定义是

struct device_driver {
const char *name;
struct bus_type *bus;

struct module *owner;
const char *mod_name; /* used for built-in modules */

bool suppress_bind_attrs; /* disables bind/unbind via sysfs */

const struct of_device_id *of_match_table;
const struct acpi_device_id *acpi_match_table;

int (*probe) (struct device *dev);
int (*remove) (struct device *dev);
void (*shutdown) (struct device *dev);
int (*suspend) (struct device *dev, pm_message_t state);
int (*resume) (struct device *dev);
const struct attribute_group **groups;

const struct dev_pm_ops *pm;

struct driver_private *p;
};

@2:对s3c24xx_led_driver中的driver字段中的bus初始化为platform_bus_type

即s3c24xx_led_driver.driver->bus->&platform_bus_type
    s3c24xx_led_driver.driver->probe->platform_drv_probe

    s3c24xx_led_driver->driver.remove ->platform_drv_remove

    s3c24xx_led_driver->driver.shutdown ->platform_drv_shutdown

@3:

   driver_register(&drv->driver);   即注册driver_register(s3c24xx_led_driver.driver)

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_type methods\n", drv->name);

other = driver_find(drv->name, drv->bus);
if (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;
}
kobject_uevent(&drv->p->kobj, KOBJ_ADD);

return ret;
}

@4:先检测一大堆函数指针,看是不是为NULL,为NULL的话会打出log;

@5:driver_find,是在寻找在bus上是否有相同的名字的设备驱动,假如有的话,那返回值不为空,说明之前已经注册过了,不能重复注册,为空,继续往下执行。

@6:真正重要的是bus_add_driver

/**
* bus_add_driver - Add a driver to the bus.
* @drv: driver.
*/
int bus_add_driver(struct device_driver *drv)
{
struct bus_type *bus;
struct driver_private *priv;
int error = 0;

bus = bus_get(drv->bus);
if (!bus)
return -EINVAL;

pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);

priv = kzalloc(sizeof(*priv), GFP_KERNEL);
if (!priv) {
error = -ENOMEM;
goto out_put_bus;
}
klist_init(&priv->klist_devices, NULL, NULL);
priv->driver = drv;
drv->p = priv;
priv->kobj.kset = bus->p->drivers_kset;
error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
"%s", drv->name);
if (error)
goto out_unregister;

klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
if (drv->bus->p->drivers_autoprobe) {
error = driver_attach(drv);
if (error)
goto out_unregister;
}
module_add_driver(drv->owner, drv);

error = driver_create_file(drv, &driver_attr_uevent);
if (error) {
printk(KERN_ERR "%s: uevent attr (%s) failed\n",
__func__, drv->name);
}
error = driver_add_attrs(bus, drv);
if (error) {
/* How the hell do we get out of this pickle? Give up */
printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
__func__, drv->name);
}

if (!drv->suppress_bind_attrs) {
error = add_bind_files(drv);
if (error) {
/* Ditto */
printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
__func__, drv->name);
}
}

return 0;

out_unregister:
kobject_put(&priv->kobj);
kfree(drv->p);
drv->p = NULL;
out_put_bus:
bus_put(bus);
return error;
}
@1:传入的参数是drv 实际上s3c24xx_led_driver.driver,通过前面的赋值操作

@2:klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers); 将node_bus的链表上加入驱动drivers

@3:判断drv->bus->p->drivers_autoprobe是否为真,这个值在bus_register中已经设置了。

@4: driver_attach(drv);  ->bus_for_each_dev(drv->bus, NULL, drv, __driver_attach); 

int bus_for_each_dev(struct bus_type *bus, struct device *start,
void *data, int (*fn)(struct device *, void *))
{
struct klist_iter i;
struct device *dev;
int error = 0;

if (!bus || !bus->p)
return -EINVAL;

klist_iter_init_node(&bus->p->klist_devices, &i,
(start ? &start->p->knode_bus : NULL));
while ((dev = next_device(&i)) && !error)
error = fn(dev, data);
klist_iter_exit(&i);
return error;
}
@1:Klist 的迭代器,start 为NULL,调用fn,即__driver_attach(dev,data);

static int __driver_attach(struct device *dev, void *data){	struct device_driver *drv = data;	/*	 * Lock device and try to bind to it. We drop the error	 * here and always return 0, because we need to keep trying	 * to bind to devices and some drivers will return an error	 * simply if it didn't support the device.	 *	 * driver_probe_device() will spit a warning if there	 * is an error.	 */	if (!driver_match_device(drv, dev))		return 0;	if (dev->parent)	/* Needed for USB */		device_lock(dev->parent);	device_lock(dev);	if (!dev->driver)		driver_probe_device(drv, dev);	device_unlock(dev);	if (dev->parent)		device_unlock(dev->parent);	return 0;}

@1:driver_match_device调用的实际是 platform_match

@2:driver_probe_device(drv, dev); 

int driver_probe_device(struct device_driver *drv, struct device *dev)
{
int ret = 0;

if (!device_is_registered(dev))
return -ENODEV;

pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
drv->bus->name, __func__, dev_name(dev), drv->name);

pm_runtime_barrier(dev);
ret = really_probe(dev, drv);
pm_request_idle(dev);

return ret;
}

@1:really_probe(dev, drv); 正真调用probe的地方。

static int really_probe(struct device *dev, struct device_driver *drv)
{
int ret = 0;

atomic_inc(&probe_count);
pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
drv->bus->name, __func__, drv->name, dev_name(dev));
WARN_ON(!list_empty(&dev->devres_head));

dev->driver = drv;

/* If using pinctrl, bind pins now before probing */
//ret = pinctrl_bind_pins(dev);
//if (ret)
//goto probe_failed;

if (driver_sysfs_add(dev)) {
printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
__func__, dev_name(dev));
goto probe_failed;
}

if (dev->bus->probe) {
ret = dev->bus->probe(dev);
if (ret)
goto probe_failed;
} else if (drv->probe) {
ret = drv->probe(dev);
if (ret)
goto probe_failed;
}

driver_bound(dev);
ret = 1;
pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
drv->bus->name, __func__, dev_name(dev), drv->name);
goto done;

probe_failed:
devres_release_all(dev);
driver_sysfs_remove(dev);
dev->driver = NULL;
dev_set_drvdata(dev, NULL);

if (ret == -EPROBE_DEFER) {
/* Driver requested deferred probing */
dev_info(dev, "Driver %s requests probe deferral\n", drv->name);
driver_deferred_probe_add(dev);
} else if (ret != -ENODEV && ret != -ENXIO) {
/* driver matched but the probe failed */
printk(KERN_WARNING
"%s: probe of %s failed with error %d\n",
drv->name, dev_name(dev), ret);
} else {
pr_debug("%s: probe of %s rejects match %d\n",
drv->name, dev_name(dev), ret);
}
/*
* Ignore errors returned by ->probe so that the next driver can try
* its luck.
*/
ret = 0;
done:
atomic_dec(&probe_count);
wake_up(&probe_waitqueue);
return ret;
}

@1:
else if (drv->probe) {
ret = drv->probe(dev);
if (ret)
goto probe_failed;
}

drv = s3c24xx_led_driver.driver 那么即s3c24xx_led_driver.driver ->probe,那么调用的是s3c24xx_led_probe,

@2“:dev.platform_data 这个从哪里来的,Common-smdk.c (common\arch\arm\mach-s3c24xx) 这个文件中定义的是smdk_machine_init

static struct platform_device __initdata *smdk_devs[] = {
&s3c_device_nand,
&smdk_led4,
&smdk_led5,
&smdk_led6,
&smdk_led7,
};

static struct platform_device smdk_led4 = {	.name		= "s3c24xx_led",	.id		= 0,	.dev		= {		.platform_data = &smdk_pdata_led4,	},};

static struct s3c24xx_led_platdata smdk_pdata_led4 = {	.gpio		= S3C2410_GPF(4),	.flags		= S3C24XX_LEDF_ACTLOW | S3C24XX_LEDF_TRISTATE,	.name		= "led4",	.def_trigger	= "timer",};


@3:led_classdev_register(&dev->dev, &led->cdev);

int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
{
led_cdev->dev = device_create(leds_class, parent, 0, led_cdev,
"%s", led_cdev->name);
if (IS_ERR(led_cdev->dev))
return PTR_ERR(led_cdev->dev);

#ifdef CONFIG_LEDS_TRIGGERS
init_rwsem(&led_cdev->trigger_lock);
#endif
/* add to the list of leds */
down_write(&leds_list_lock);
list_add_tail(&led_cdev->node, &leds_list);
up_write(&leds_list_lock);

if (!led_cdev->max_brightness)
led_cdev->max_brightness = LED_FULL;

led_update_brightness(led_cdev);

INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);

init_timer(&led_cdev->blink_timer);
led_cdev->blink_timer.function = led_timer_function;
led_cdev->blink_timer.data = (unsigned long)led_cdev;

#ifdef CONFIG_LEDS_TRIGGERS
led_trigger_set_default(led_cdev);
#endif

dev_dbg(parent, "Registered led device: %s\n",
led_cdev->name);

return 0;
}

@1:list_add_tail(&led_cdev->node, &leds_list); 将led_s3c24xx的led_cdev中添加到led_list 的list中。