内核设备驱动API之device_add

int device_add(struct device *dev)用于添加一个设备到linux的设备数
其源码分析如下:
int device_add(struct device *dev)
{
	struct device *parent;
	struct kobject *kobj;
	struct class_interface *class_intf;
	int error = -EINVAL;
	struct kobject *glue_dir = NULL;
	#得到这个设备
	dev = get_device(dev);
	if (!dev)
		goto done;
	#检查设备是否有私有的初始化函数
	if (!dev->p) {
		error = device_private_init(dev);
		if (error)
			goto done;
	}

	/*
	 * for statically allocated devices, which should all be converted
	 * some day, we need to initialize the name. We prevent reading back
	 * the name, and force the use of dev_name()
	 */
	 #设置device的name 我们平时通过cat 看到的device的name就是在这里设定的
	if (dev->init_name) {
		dev_set_name(dev, "%s", dev->init_name);
		dev->init_name = NULL;
	}

	/* subsystems can specify simple device enumeration */
	#如果没有指定device的init_name 就用bus 中的dev_name和dev id来组成device name
	if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
		dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
	#如果前两部都没有成功设置device name则退出
	if (!dev_name(dev)) {
		error = -EINVAL;
		goto name_error;
	}
	#通过dmesg 可以看到下面打印出的name
	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
	#获得这个设备的父设备
	parent = get_device(dev->parent);
	kobj = get_device_parent(dev, parent);
	if (kobj)
		dev->kobj.parent = kobj;

	/* use parent numa_node */
	#子设备会复用父设备的numa 节点
	if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
		set_dev_node(dev, dev_to_node(parent));

	/* first, register with generic layer. */
	/* we require the name to be set before, and pass NULL */
	error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
	if (error) {
		glue_dir = get_glue_dir(dev);
		goto Error;
	}

	/* notify platform of device entry */
	if (platform_notify)
		platform_notify(dev);
	#新建attr_uevent
	error = device_create_file(dev, &dev_attr_uevent);
	if (error)
		goto attrError;
	#建立symbol link
	error = device_add_class_symlinks(dev);
	if (error)
		goto SymlinkError;
	#添加attr
	error = device_add_attrs(dev);
	if (error)
		goto AttrsError;
	#在bus中添加这个device
	error = bus_add_device(dev);
	if (error)
		goto BusError;
	#在power manager中增加这个设备
	error = dpm_sysfs_add(dev);
	if (error)
		goto DPMError;
	device_pm_add(dev);
	#在sys中增加入口
	if (MAJOR(dev->devt)) {
		error = device_create_file(dev, &dev_attr_dev);
		if (error)
			goto DevAttrError;

		error = device_create_sys_dev_entry(dev);
		if (error)
			goto SysEntryError;

		devtmpfs_create_node(dev);
	}

	/* Notify clients of device addition.  This call must come
	 * after dpm_sysfs_add() and before kobject_uevent().
	 */
	if (dev->bus)
		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
					     BUS_NOTIFY_ADD_DEVICE, dev);
	#向user space 发送uevent 命令
	kobject_uevent(&dev->kobj, KOBJ_ADD);
	#开始bus的probe 过程
	bus_probe_device(dev);
	#如果这个设备有父设备,则将这个device 添加到父设备的klist中
	if (parent)
		klist_add_tail(&dev->p->knode_parent,
			       &parent->p->klist_children);
	#如果这个device属于某个class,也添加到父设备的对应的class 对应klist中
	if (dev->class) {
		mutex_lock(&dev->class->p->mutex);
		/* tie the class to the device */
		klist_add_tail(&dev->knode_class,
			       &dev->class->p->klist_devices);

		/* notify any interfaces that the device is here */
		list_for_each_entry(class_intf,
				    &dev->class->p->interfaces, node)
			if (class_intf->add_dev)
				class_intf->add_dev(dev, class_intf);
		mutex_unlock(&dev->class->p->mutex);
	}
done:
	put_device(dev);
	return error;

}

<think>好的,用户想了解如何在操作系统中添加LED设备,特别是关于led_add_device函数的用法和示例。我需要先回忆相关的知识,并结合提供的引用内容来组织回答。 首先,用户提到了几个引用,其中引用[1]和[2]提到device_adddevice_register函数,用于设备注册和添加到总线。引用[3]涉及设备属性的创建,引用[4]讨论了设备树到platform_device的转换。这些可能和Linux驱动模型相关。而引用[5]是MySQL的函数,可能无关,暂时忽略。 在Linux内核中,添加设备通常涉及驱动模型,可能需要注册platform_device或使用设备树。对于LED设备,常见的有两种方式:通过设备树描述,或者在代码中手动注册设备。 LED子系统在Linux中通常使用leds-gpio驱动,需要配置GPIO引脚和对应的LED属性。用户提到的led_add_device可能不是标准内核函数,可能是特定于某个框架或示例代码的函数。因此,可能需要解释标准的添加LED设备的方法,比如使用设备树或platform_device_register。 根据引用[4],设备树的节点会被转换为platform_device,所以在设备树中添加LED节点,并确保驱动匹配,可能是方法之一。例如,在设备树中添加led节点,指定compatible为"gpio-leds",然后内核在启动时会自动生成对应的platform_device。 另外,引用[1]和[2]提到的device_register和bus_add_device,说明设备注册的流程。在编写LED驱动时,可能需要调用platform_device_register或类似的函数来注册设备结构体。 引用[3]提到的sysfs属性文件,LED子系统通常会自动创建sysfs节点,用户可以通过/sys/class/leds/控制LED。但如果是自定义驱动,可能需要使用device_create_file来添加属性。 因此,回答应该包括两种方法:设备树配置和代码注册。并给出示例,比如设备树片段和模块初始化代码。同时,需要提到如何测试,比如通过echo命令控制LED状态。 可能的问题:用户是否混淆了led_add_device和标准内核API?需要确认是否存在这样的函数,或者是否是用户代码中的自定义函数。根据经验,标准内核中可能没有led_add_device,而是使用platform_device_register或of_platform_populate。因此,回答时应以标准方法为主,并建议用户检查他们的具体环境和内核版本。 总结步骤: 1. 使用设备树定义LED,适用于嵌入式系统。 2. 在代码中注册platform_device,适用于动态添加设备。 3. 编写驱动模块,处理设备的初始化和控制。 4. 示例代码展示设备树和模块注册的方式。 5. 测试方法,通过sysfs或直接控制GPIO。 需要确保示例代码正确,比如在设备树中的节点格式,以及模块中注册设备的函数调用。同时,注意引用相关的引用内容,如device_register和总线添加的过程[^2],以及属性文件的创建[^3]。</think>在操作系统中添加LED设备通常涉及内核驱动开发与设备注册流程。以下是基于Linux内核的LED设备添加方法及关键函数说明: ### 一、LED设备添加的核心步骤 1. **设备定义** 在设备树(Device Tree)中定义LED硬件信息,例如: ```dts leds { compatible = "gpio-leds"; led0 { label = "sys_led"; gpios = <&gpio0 15 GPIO_ACTIVE_HIGH>; default-state = "off"; }; }; ``` 该节点通过`of_platform_default_populate_init`函数转换为`platform_device`[^4]。 2. **设备注册** 使用`platform_device_register()`或`device_register()`函数将设备添加到内核设备模型: ```c struct platform_device led_device = { .name = "my_led", .id = -1, }; platform_device_register(&led_device); // 触发bus_add_device调用 ``` 3. **属性文件创建** 通过`DEVICE_ATTR`宏定义属性,并调用`device_create_file()`创建sysfs接口: ```c static ssize_t led_state_show(struct device *dev, ...); static DEVICE_ATTR(state, 0644, led_state_show, NULL); device_create_file(&led_device.dev, &dev_attr_state); // 生成/sys接口 ``` ### 二、LED设备添加代码示例 ```c #include <linux/platform_device.h> #include <linux/leds.h> static struct gpio_led my_led = { .name = "custom_led", .gpio = 15, // 对应GPIO引脚号 .default_trigger = "heartbeat", }; static struct gpio_led_platform_data led_data = { .num_leds = 1, .leds = &my_led, }; static struct platform_device led_dev = { .name = "leds-gpio", .id = -1, .dev = { .platform_data = &led_data, } }; static int __init led_init(void) { platform_device_register(&led_dev); // 注册设备到总线 return 0; } module_init(led_init); ``` ### 三、验证方法 1. 查看设备是否存在: ```bash ls /sys/class/leds/ ``` 2. 通过sysfs控制LED: ```bash echo 1 > /sys/class/leds/custom_led/brightness ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值