drivers/leds/led-test.c
Source file repositories/reference/linux-study-clean/drivers/leds/led-test.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/leds/led-test.c- Extension
.c- Size
- 3478 bytes
- Lines
- 133
- Domain
- Driver Families
- Bucket
- drivers/leds
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
kunit/device.hkunit/test.hlinux/device.hlinux/leds.h
Detected Declarations
struct led_test_ddatafunction led_test_brightness_getfunction led_test_class_registerfunction led_test_class_add_lookup_and_getfunction led_test_initfunction led_test_exit
Annotated Snippet
struct led_test_ddata {
struct led_classdev cdev;
struct device *dev;
};
static enum led_brightness led_test_brightness_get(struct led_classdev *cdev)
{
return LED_TEST_POST_REG_BRIGHTNESS;
}
static void led_test_class_register(struct kunit *test)
{
struct led_test_ddata *ddata = test->priv;
struct led_classdev *cdev_clash, *cdev = &ddata->cdev;
struct device *dev = ddata->dev;
int ret;
/* Register a LED class device */
cdev->name = "led-test";
cdev->brightness_get = led_test_brightness_get;
cdev->brightness = 0;
ret = devm_led_classdev_register(dev, cdev);
KUNIT_ASSERT_EQ(test, ret, 0);
KUNIT_EXPECT_EQ(test, cdev->max_brightness, LED_FULL);
KUNIT_EXPECT_EQ(test, cdev->brightness, LED_TEST_POST_REG_BRIGHTNESS);
KUNIT_EXPECT_STREQ(test, cdev->dev->kobj.name, "led-test");
/* Register again with the same name - expect it to pass with the LED renamed */
cdev_clash = devm_kmemdup(dev, cdev, sizeof(*cdev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cdev_clash);
ret = devm_led_classdev_register(dev, cdev_clash);
KUNIT_ASSERT_EQ(test, ret, 0);
KUNIT_EXPECT_STREQ(test, cdev_clash->dev->kobj.name, "led-test_1");
KUNIT_EXPECT_STREQ(test, cdev_clash->name, "led-test");
/* Enable name conflict rejection and register with the same name again - expect failure */
cdev_clash->flags |= LED_REJECT_NAME_CONFLICT;
ret = devm_led_classdev_register(dev, cdev_clash);
KUNIT_EXPECT_EQ(test, ret, -EEXIST);
}
static void led_test_class_add_lookup_and_get(struct kunit *test)
{
struct led_test_ddata *ddata = test->priv;
struct led_classdev *cdev = &ddata->cdev, *cdev_get;
struct device *dev = ddata->dev;
struct led_lookup_data lookup;
int ret;
/* First, register a LED class device */
cdev->name = "led-test";
ret = devm_led_classdev_register(dev, cdev);
KUNIT_ASSERT_EQ(test, ret, 0);
/* Then make the LED available for lookup */
lookup.provider = cdev->name;
lookup.dev_id = dev_name(dev);
lookup.con_id = "led-test-1";
led_add_lookup(&lookup);
/* Finally, attempt to look it up via the API - imagine this was an orthogonal driver */
cdev_get = devm_led_get(dev, "led-test-1");
KUNIT_ASSERT_FALSE(test, IS_ERR(cdev_get));
KUNIT_EXPECT_STREQ(test, cdev_get->name, cdev->name);
led_remove_lookup(&lookup);
}
static struct kunit_case led_test_cases[] = {
KUNIT_CASE(led_test_class_register),
KUNIT_CASE(led_test_class_add_lookup_and_get),
{ }
};
static int led_test_init(struct kunit *test)
{
struct led_test_ddata *ddata;
struct device *dev;
ddata = kunit_kzalloc(test, sizeof(*ddata), GFP_KERNEL);
if (!ddata)
return -ENOMEM;
test->priv = ddata;
Annotation
- Immediate include surface: `kunit/device.h`, `kunit/test.h`, `linux/device.h`, `linux/leds.h`.
- Detected declarations: `struct led_test_ddata`, `function led_test_brightness_get`, `function led_test_class_register`, `function led_test_class_add_lookup_and_get`, `function led_test_init`, `function led_test_exit`.
- Atlas domain: Driver Families / drivers/leds.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.