1 // SPDX-License-Identifier: GPL-2.0-only
4 * drm_sysfs.c - Modifications to drm_sysfs_class.c to support
5 * extra sysfs attribute from DRM. Normal drm_sysfs_class
6 * does not allow adding attributes.
8 * Copyright (c) 2004 Jon Smirl <jonsmirl@gmail.com>
9 * Copyright (c) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
10 * Copyright (c) 2003-2004 IBM Corp.
13 #include <linux/device.h>
14 #include <linux/kdev_t.h>
15 #include <linux/gfp.h>
16 #include <linux/err.h>
17 #include <linux/export.h>
19 #include <drm/drm_sysfs.h>
21 #include "drm_internal.h"
23 #define to_drm_minor(d) dev_get_drvdata(d)
24 #define to_drm_connector(d) dev_get_drvdata(d)
29 * DRM provides very little additional support to drivers for sysfs
30 * interactions, beyond just all the standard stuff. Drivers who want to expose
31 * additional sysfs properties and property groups can attach them at either
32 * &drm_device.dev or &drm_connector.kdev.
34 * Registration is automatically handled when calling drm_dev_register(), or
35 * drm_connector_register() in case of hot-plugged connectors. Unregistration is
36 * also automatically handled by drm_dev_unregister() and
37 * drm_connector_unregister().
40 static struct device_type drm_sysfs_device_minor
= {
44 struct class *drm_class
;
46 static char *drm_devnode(struct device
*dev
, umode_t
*mode
)
48 return kasprintf(GFP_KERNEL
, "dri/%s", dev_name(dev
));
51 static CLASS_ATTR_STRING(version
, S_IRUGO
, "drm 1.1.0 20060810");
54 * drm_sysfs_init - initialize sysfs helpers
56 * This is used to create the DRM class, which is the implicit parent of any
57 * other top-level DRM sysfs objects.
59 * You must call drm_sysfs_destroy() to release the allocated resources.
61 * Return: 0 on success, negative error code on failure.
63 int drm_sysfs_init(void)
67 drm_class
= class_create(THIS_MODULE
, "drm");
68 if (IS_ERR(drm_class
))
69 return PTR_ERR(drm_class
);
71 err
= class_create_file(drm_class
, &class_attr_version
.attr
);
73 class_destroy(drm_class
);
78 drm_class
->devnode
= drm_devnode
;
83 * drm_sysfs_destroy - destroys DRM class
85 * Destroy the DRM device class.
87 void drm_sysfs_destroy(void)
89 if (IS_ERR_OR_NULL(drm_class
))
91 class_remove_file(drm_class
, &class_attr_version
.attr
);
92 class_destroy(drm_class
);
97 * Connector properties
99 static ssize_t
status_store(struct device
*device
,
100 struct device_attribute
*attr
,
101 const char *buf
, size_t count
)
103 struct drm_connector
*connector
= to_drm_connector(device
);
104 struct drm_device
*dev
= connector
->dev
;
105 enum drm_connector_force old_force
;
108 ret
= mutex_lock_interruptible(&dev
->mode_config
.mutex
);
112 old_force
= connector
->force
;
114 if (sysfs_streq(buf
, "detect"))
115 connector
->force
= 0;
116 else if (sysfs_streq(buf
, "on"))
117 connector
->force
= DRM_FORCE_ON
;
118 else if (sysfs_streq(buf
, "on-digital"))
119 connector
->force
= DRM_FORCE_ON_DIGITAL
;
120 else if (sysfs_streq(buf
, "off"))
121 connector
->force
= DRM_FORCE_OFF
;
125 if (old_force
!= connector
->force
|| !connector
->force
) {
126 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force updated from %d to %d or reprobing\n",
129 old_force
, connector
->force
);
131 connector
->funcs
->fill_modes(connector
,
132 dev
->mode_config
.max_width
,
133 dev
->mode_config
.max_height
);
136 mutex_unlock(&dev
->mode_config
.mutex
);
138 return ret
? ret
: count
;
141 static ssize_t
status_show(struct device
*device
,
142 struct device_attribute
*attr
,
145 struct drm_connector
*connector
= to_drm_connector(device
);
146 enum drm_connector_status status
;
148 status
= READ_ONCE(connector
->status
);
150 return snprintf(buf
, PAGE_SIZE
, "%s\n",
151 drm_get_connector_status_name(status
));
154 static ssize_t
dpms_show(struct device
*device
,
155 struct device_attribute
*attr
,
158 struct drm_connector
*connector
= to_drm_connector(device
);
161 dpms
= READ_ONCE(connector
->dpms
);
163 return snprintf(buf
, PAGE_SIZE
, "%s\n",
164 drm_get_dpms_name(dpms
));
167 static ssize_t
enabled_show(struct device
*device
,
168 struct device_attribute
*attr
,
171 struct drm_connector
*connector
= to_drm_connector(device
);
174 enabled
= READ_ONCE(connector
->encoder
);
176 return snprintf(buf
, PAGE_SIZE
, enabled
? "enabled\n" : "disabled\n");
179 static ssize_t
edid_show(struct file
*filp
, struct kobject
*kobj
,
180 struct bin_attribute
*attr
, char *buf
, loff_t off
,
183 struct device
*connector_dev
= kobj_to_dev(kobj
);
184 struct drm_connector
*connector
= to_drm_connector(connector_dev
);
189 mutex_lock(&connector
->dev
->mode_config
.mutex
);
190 if (!connector
->edid_blob_ptr
)
193 edid
= connector
->edid_blob_ptr
->data
;
194 size
= connector
->edid_blob_ptr
->length
;
201 if (off
+ count
> size
)
203 memcpy(buf
, edid
+ off
, count
);
207 mutex_unlock(&connector
->dev
->mode_config
.mutex
);
212 static ssize_t
modes_show(struct device
*device
,
213 struct device_attribute
*attr
,
216 struct drm_connector
*connector
= to_drm_connector(device
);
217 struct drm_display_mode
*mode
;
220 mutex_lock(&connector
->dev
->mode_config
.mutex
);
221 list_for_each_entry(mode
, &connector
->modes
, head
) {
222 written
+= snprintf(buf
+ written
, PAGE_SIZE
- written
, "%s\n",
225 mutex_unlock(&connector
->dev
->mode_config
.mutex
);
230 static DEVICE_ATTR_RW(status
);
231 static DEVICE_ATTR_RO(enabled
);
232 static DEVICE_ATTR_RO(dpms
);
233 static DEVICE_ATTR_RO(modes
);
235 static struct attribute
*connector_dev_attrs
[] = {
236 &dev_attr_status
.attr
,
237 &dev_attr_enabled
.attr
,
239 &dev_attr_modes
.attr
,
243 static struct bin_attribute edid_attr
= {
250 static struct bin_attribute
*connector_bin_attrs
[] = {
255 static const struct attribute_group connector_dev_group
= {
256 .attrs
= connector_dev_attrs
,
257 .bin_attrs
= connector_bin_attrs
,
260 static const struct attribute_group
*connector_dev_groups
[] = {
261 &connector_dev_group
,
265 int drm_sysfs_connector_add(struct drm_connector
*connector
)
267 struct drm_device
*dev
= connector
->dev
;
273 device_create_with_groups(drm_class
, dev
->primary
->kdev
, 0,
274 connector
, connector_dev_groups
,
275 "card%d-%s", dev
->primary
->index
,
277 DRM_DEBUG("adding \"%s\" to sysfs\n",
280 if (IS_ERR(connector
->kdev
)) {
281 DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector
->kdev
));
282 return PTR_ERR(connector
->kdev
);
285 /* Let userspace know we have a new connector */
286 drm_sysfs_hotplug_event(dev
);
291 void drm_sysfs_connector_remove(struct drm_connector
*connector
)
293 if (!connector
->kdev
)
295 DRM_DEBUG("removing \"%s\" from sysfs\n",
298 device_unregister(connector
->kdev
);
299 connector
->kdev
= NULL
;
302 void drm_sysfs_lease_event(struct drm_device
*dev
)
304 char *event_string
= "LEASE=1";
305 char *envp
[] = { event_string
, NULL
};
307 DRM_DEBUG("generating lease event\n");
309 kobject_uevent_env(&dev
->primary
->kdev
->kobj
, KOBJ_CHANGE
, envp
);
313 * drm_sysfs_hotplug_event - generate a DRM uevent
316 * Send a uevent for the DRM device specified by @dev. Currently we only
317 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
318 * deal with other types of events.
320 void drm_sysfs_hotplug_event(struct drm_device
*dev
)
322 char *event_string
= "HOTPLUG=1";
323 char *envp
[] = { event_string
, NULL
};
325 DRM_DEBUG("generating hotplug event\n");
327 kobject_uevent_env(&dev
->primary
->kdev
->kobj
, KOBJ_CHANGE
, envp
);
329 EXPORT_SYMBOL(drm_sysfs_hotplug_event
);
331 static void drm_sysfs_release(struct device
*dev
)
336 struct device
*drm_sysfs_minor_alloc(struct drm_minor
*minor
)
338 const char *minor_str
;
342 if (minor
->type
== DRM_MINOR_RENDER
)
343 minor_str
= "renderD%d";
345 minor_str
= "card%d";
347 kdev
= kzalloc(sizeof(*kdev
), GFP_KERNEL
);
349 return ERR_PTR(-ENOMEM
);
351 device_initialize(kdev
);
352 kdev
->devt
= MKDEV(DRM_MAJOR
, minor
->index
);
353 kdev
->class = drm_class
;
354 kdev
->type
= &drm_sysfs_device_minor
;
355 kdev
->parent
= minor
->dev
->dev
;
356 kdev
->release
= drm_sysfs_release
;
357 dev_set_drvdata(kdev
, minor
);
359 r
= dev_set_name(kdev
, minor_str
, minor
->index
);
371 * drm_class_device_register - register new device with the DRM sysfs class
372 * @dev: device to register
374 * Registers a new &struct device within the DRM sysfs class. Essentially only
375 * used by ttm to have a place for its global settings. Drivers should never use
378 int drm_class_device_register(struct device
*dev
)
380 if (!drm_class
|| IS_ERR(drm_class
))
383 dev
->class = drm_class
;
384 return device_register(dev
);
386 EXPORT_SYMBOL_GPL(drm_class_device_register
);
389 * drm_class_device_unregister - unregister device with the DRM sysfs class
390 * @dev: device to unregister
392 * Unregisters a &struct device from the DRM sysfs class. Essentially only used
393 * by ttm to have a place for its global settings. Drivers should never use
396 void drm_class_device_unregister(struct device
*dev
)
398 return device_unregister(dev
);
400 EXPORT_SYMBOL_GPL(drm_class_device_unregister
);