|
10 | 10 | #include <linux/device.h>
|
11 | 11 | #include <linux/leds.h>
|
12 | 12 |
|
| 13 | +#define LED_TEST_POST_REG_BRIGHTNESS 10 |
| 14 | + |
13 | 15 | struct led_test_ddata {
|
14 | 16 | struct led_classdev cdev;
|
15 | 17 | struct device *dev;
|
16 | 18 | };
|
17 | 19 |
|
| 20 | +static enum led_brightness led_test_brightness_get(struct led_classdev *cdev) |
| 21 | +{ |
| 22 | + return LED_TEST_POST_REG_BRIGHTNESS; |
| 23 | +} |
| 24 | + |
18 | 25 | static void led_test_class_register(struct kunit *test)
|
19 | 26 | {
|
20 | 27 | struct led_test_ddata *ddata = test->priv;
|
21 |
| - struct led_classdev *cdev = &ddata->cdev; |
| 28 | + struct led_classdev *cdev_clash, *cdev = &ddata->cdev; |
22 | 29 | struct device *dev = ddata->dev;
|
23 | 30 | int ret;
|
24 | 31 |
|
| 32 | + /* Register a LED class device */ |
25 | 33 | cdev->name = "led-test";
|
| 34 | + cdev->brightness_get = led_test_brightness_get; |
| 35 | + cdev->brightness = 0; |
26 | 36 |
|
27 | 37 | ret = devm_led_classdev_register(dev, cdev);
|
28 | 38 | KUNIT_ASSERT_EQ(test, ret, 0);
|
| 39 | + |
| 40 | + KUNIT_EXPECT_EQ(test, cdev->max_brightness, LED_FULL); |
| 41 | + KUNIT_EXPECT_EQ(test, cdev->brightness, LED_TEST_POST_REG_BRIGHTNESS); |
| 42 | + KUNIT_EXPECT_STREQ(test, cdev->dev->kobj.name, "led-test"); |
| 43 | + |
| 44 | + /* Register again with the same name - expect it to pass with the LED renamed */ |
| 45 | + cdev_clash = devm_kmemdup(dev, cdev, sizeof(*cdev), GFP_KERNEL); |
| 46 | + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cdev_clash); |
| 47 | + |
| 48 | + ret = devm_led_classdev_register(dev, cdev_clash); |
| 49 | + KUNIT_ASSERT_EQ(test, ret, 0); |
| 50 | + |
| 51 | + KUNIT_EXPECT_STREQ(test, cdev_clash->dev->kobj.name, "led-test_1"); |
| 52 | + KUNIT_EXPECT_STREQ(test, cdev_clash->name, "led-test"); |
| 53 | + |
| 54 | + /* Enable name conflict rejection and register with the same name again - expect failure */ |
| 55 | + cdev_clash->flags |= LED_REJECT_NAME_CONFLICT; |
| 56 | + ret = devm_led_classdev_register(dev, cdev_clash); |
| 57 | + KUNIT_EXPECT_EQ(test, ret, -EEXIST); |
29 | 58 | }
|
30 | 59 |
|
31 | 60 | static struct kunit_case led_test_cases[] = {
|
|
0 commit comments