Skip to content

Commit db1a0ae

Browse files
pierremoreauBen Skeggs
authored andcommitted
drm/nouveau/bl: Assign different names to interfaces
Currently, every backlight interface created by Nouveau uses the same name, nv_backlight. This leads to a sysfs warning as it tries to create an already existing folder. This patch adds a incremented number to the name, but keeps the initial name as nv_backlight, to avoid possibly breaking userspace; the second interface will be named nv_backlight1, and so on. Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=86539 v2: * Switch to using ida for generating unique IDs, as suggested by Ilia Mirkin; * Allocate backlight name on the stack, as suggested by Ilia Mirkin; * Move `nouveau_get_backlight_name()` to avoid forward declaration, as suggested by Ilia Mirkin; * Fix reference to bug report formatting, as reported by Nick Tenney. v3: * Define a macro for the size of the backlight name, to avoid defining it multiple times; * Use snprintf in place of sprintf. v4: * Do not create similarly named interfaces when reaching the maximum amount of unique names, but fail instead, as pointed out by Lukas Wunner Signed-off-by: Pierre Moreau <pierre.morrow@free.fr> Signed-off-by: Ben Skeggs <bskeggs@redhat.com>
1 parent 79d48da commit db1a0ae

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

drivers/gpu/drm/nouveau/nouveau_backlight.c

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,35 @@
3131
*/
3232

3333
#include <linux/backlight.h>
34+
#include <linux/idr.h>
3435

3536
#include "nouveau_drv.h"
3637
#include "nouveau_reg.h"
3738
#include "nouveau_encoder.h"
3839

40+
static struct ida bl_ida;
41+
#define BL_NAME_SIZE 15 // 12 for name + 2 for digits + 1 for '\0'
42+
43+
struct backlight_connector {
44+
struct list_head head;
45+
int id;
46+
};
47+
48+
static bool
49+
nouveau_get_backlight_name(char backlight_name[BL_NAME_SIZE], struct backlight_connector
50+
*connector)
51+
{
52+
const int nb = ida_simple_get(&bl_ida, 0, 0, GFP_KERNEL);
53+
if (nb < 0 || nb >= 100)
54+
return false;
55+
if (nb > 0)
56+
snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight%d", nb);
57+
else
58+
snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight");
59+
connector->id = nb;
60+
return true;
61+
}
62+
3963
static int
4064
nv40_get_intensity(struct backlight_device *bd)
4165
{
@@ -74,17 +98,28 @@ nv40_backlight_init(struct drm_connector *connector)
7498
struct nvif_object *device = &drm->device.object;
7599
struct backlight_properties props;
76100
struct backlight_device *bd;
101+
struct backlight_connector bl_connector;
102+
char backlight_name[BL_NAME_SIZE];
77103

78104
if (!(nvif_rd32(device, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK))
79105
return 0;
80106

81107
memset(&props, 0, sizeof(struct backlight_properties));
82108
props.type = BACKLIGHT_RAW;
83109
props.max_brightness = 31;
84-
bd = backlight_device_register("nv_backlight", connector->kdev, drm,
110+
if (!nouveau_get_backlight_name(backlight_name, &bl_connector)) {
111+
NV_ERROR(drm, "Failed to retrieve a unique name for the backlight interface\n");
112+
return 0;
113+
}
114+
bd = backlight_device_register(backlight_name , connector->kdev, drm,
85115
&nv40_bl_ops, &props);
86-
if (IS_ERR(bd))
116+
117+
if (IS_ERR(bd)) {
118+
if (bl_connector.id > 0)
119+
ida_simple_remove(&bl_ida, bl_connector.id);
87120
return PTR_ERR(bd);
121+
}
122+
list_add(&bl_connector.head, &drm->bl_connectors);
88123
drm->backlight = bd;
89124
bd->props.brightness = nv40_get_intensity(bd);
90125
backlight_update_status(bd);
@@ -182,6 +217,8 @@ nv50_backlight_init(struct drm_connector *connector)
182217
struct backlight_properties props;
183218
struct backlight_device *bd;
184219
const struct backlight_ops *ops;
220+
struct backlight_connector bl_connector;
221+
char backlight_name[BL_NAME_SIZE];
185222

186223
nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
187224
if (!nv_encoder) {
@@ -203,11 +240,20 @@ nv50_backlight_init(struct drm_connector *connector)
203240
memset(&props, 0, sizeof(struct backlight_properties));
204241
props.type = BACKLIGHT_RAW;
205242
props.max_brightness = 100;
206-
bd = backlight_device_register("nv_backlight", connector->kdev,
243+
if (!nouveau_get_backlight_name(backlight_name, &bl_connector)) {
244+
NV_ERROR(drm, "Failed to retrieve a unique name for the backlight interface\n");
245+
return 0;
246+
}
247+
bd = backlight_device_register(backlight_name , connector->kdev,
207248
nv_encoder, ops, &props);
208-
if (IS_ERR(bd))
249+
250+
if (IS_ERR(bd)) {
251+
if (bl_connector.id > 0)
252+
ida_simple_remove(&bl_ida, bl_connector.id);
209253
return PTR_ERR(bd);
254+
}
210255

256+
list_add(&bl_connector.head, &drm->bl_connectors);
211257
drm->backlight = bd;
212258
bd->props.brightness = bd->ops->get_brightness(bd);
213259
backlight_update_status(bd);
@@ -221,6 +267,8 @@ nouveau_backlight_init(struct drm_device *dev)
221267
struct nvif_device *device = &drm->device;
222268
struct drm_connector *connector;
223269

270+
INIT_LIST_HEAD(&drm->bl_connectors);
271+
224272
list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
225273
if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS &&
226274
connector->connector_type != DRM_MODE_CONNECTOR_eDP)
@@ -247,9 +295,27 @@ void
247295
nouveau_backlight_exit(struct drm_device *dev)
248296
{
249297
struct nouveau_drm *drm = nouveau_drm(dev);
298+
struct backlight_connector *connector;
299+
300+
list_for_each_entry(connector, &drm->bl_connectors, head) {
301+
if (connector->id >= 0)
302+
ida_simple_remove(&bl_ida, connector->id);
303+
}
250304

251305
if (drm->backlight) {
252306
backlight_device_unregister(drm->backlight);
253307
drm->backlight = NULL;
254308
}
255309
}
310+
311+
void
312+
nouveau_backlight_ctor(void)
313+
{
314+
ida_init(&bl_ida);
315+
}
316+
317+
void
318+
nouveau_backlight_dtor(void)
319+
{
320+
ida_destroy(&bl_ida);
321+
}

drivers/gpu/drm/nouveau/nouveau_display.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ int nouveau_crtc_set_config(struct drm_mode_set *set);
9191
#ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
9292
extern int nouveau_backlight_init(struct drm_device *);
9393
extern void nouveau_backlight_exit(struct drm_device *);
94+
extern void nouveau_backlight_ctor(void);
95+
extern void nouveau_backlight_dtor(void);
9496
#else
9597
static inline int
9698
nouveau_backlight_init(struct drm_device *dev)
@@ -101,6 +103,14 @@ nouveau_backlight_init(struct drm_device *dev)
101103
static inline void
102104
nouveau_backlight_exit(struct drm_device *dev) {
103105
}
106+
107+
static inline void
108+
nouveau_backlight_ctor(void) {
109+
}
110+
111+
static inline void
112+
nouveau_backlight_dtor(void) {
113+
}
104114
#endif
105115

106116
struct drm_framebuffer *

drivers/gpu/drm/nouveau/nouveau_drm.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,7 @@ nouveau_drm_init(void)
11221122
#endif
11231123

11241124
nouveau_register_dsm_handler();
1125+
nouveau_backlight_ctor();
11251126
return drm_pci_init(&driver_pci, &nouveau_drm_pci_driver);
11261127
}
11271128

@@ -1132,6 +1133,7 @@ nouveau_drm_exit(void)
11321133
return;
11331134

11341135
drm_pci_exit(&driver_pci, &nouveau_drm_pci_driver);
1136+
nouveau_backlight_dtor();
11351137
nouveau_unregister_dsm_handler();
11361138

11371139
#ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER

drivers/gpu/drm/nouveau/nouveau_drv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ struct nouveau_drm {
163163
struct nvbios vbios;
164164
struct nouveau_display *display;
165165
struct backlight_device *backlight;
166+
struct list_head bl_connectors;
166167
struct work_struct hpd_work;
167168
#ifdef CONFIG_ACPI
168169
struct notifier_block acpi_nb;

0 commit comments

Comments
 (0)