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/err.h>
15 #include <linux/export.h>
16 #include <linux/gfp.h>
17 #include <linux/i2c.h>
18 #include <linux/kdev_t.h>
19 #include <linux/slab.h>
21 #include <drm/drm_connector.h>
22 #include <drm/drm_device.h>
23 #include <drm/drm_file.h>
24 #include <drm/drm_modes.h>
25 #include <drm/drm_print.h>
26 #include <drm/drm_property.h>
27 #include <drm/drm_sysfs.h>
29 #include "drm_internal.h"
31 #define to_drm_minor(d) dev_get_drvdata(d)
32 #define to_drm_connector(d) dev_get_drvdata(d)
37 * DRM provides very little additional support to drivers for sysfs
38 * interactions, beyond just all the standard stuff. Drivers who want to expose
39 * additional sysfs properties and property groups can attach them at either
40 * &drm_device.dev or &drm_connector.kdev.
42 * Registration is automatically handled when calling drm_dev_register(), or
43 * drm_connector_register() in case of hot-plugged connectors. Unregistration is
44 * also automatically handled by drm_dev_unregister() and
45 * drm_connector_unregister().
48 static struct device_type drm_sysfs_device_minor = {
52 struct class *drm_class;
54 static char *drm_devnode(struct device *dev, umode_t *mode)
56 return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
59 static CLASS_ATTR_STRING(version, S_IRUGO, "drm 1.1.0 20060810");
62 * drm_sysfs_init - initialize sysfs helpers
64 * This is used to create the DRM class, which is the implicit parent of any
65 * other top-level DRM sysfs objects.
67 * You must call drm_sysfs_destroy() to release the allocated resources.
69 * Return: 0 on success, negative error code on failure.
71 int drm_sysfs_init(void)
75 drm_class = class_create(THIS_MODULE, "drm");
76 if (IS_ERR(drm_class))
77 return PTR_ERR(drm_class);
79 err = class_create_file(drm_class, &class_attr_version.attr);
81 class_destroy(drm_class);
86 drm_class->devnode = drm_devnode;
87 drm_setup_hdcp_srm(drm_class);
92 * drm_sysfs_destroy - destroys DRM class
94 * Destroy the DRM device class.
96 void drm_sysfs_destroy(void)
98 if (IS_ERR_OR_NULL(drm_class))
100 drm_teardown_hdcp_srm(drm_class);
101 class_remove_file(drm_class, &class_attr_version.attr);
102 class_destroy(drm_class);
107 * Connector properties
109 static ssize_t status_store(struct device *device,
110 struct device_attribute *attr,
111 const char *buf, size_t count)
113 struct drm_connector *connector = to_drm_connector(device);
114 struct drm_device *dev = connector->dev;
115 enum drm_connector_force old_force;
118 ret = mutex_lock_interruptible(&dev->mode_config.mutex);
122 old_force = connector->force;
124 if (sysfs_streq(buf, "detect"))
125 connector->force = 0;
126 else if (sysfs_streq(buf, "on"))
127 connector->force = DRM_FORCE_ON;
128 else if (sysfs_streq(buf, "on-digital"))
129 connector->force = DRM_FORCE_ON_DIGITAL;
130 else if (sysfs_streq(buf, "off"))
131 connector->force = DRM_FORCE_OFF;
135 if (old_force != connector->force || !connector->force) {
136 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force updated from %d to %d or reprobing\n",
139 old_force, connector->force);
141 connector->funcs->fill_modes(connector,
142 dev->mode_config.max_width,
143 dev->mode_config.max_height);
146 mutex_unlock(&dev->mode_config.mutex);
148 return ret ? ret : count;
151 static ssize_t status_show(struct device *device,
152 struct device_attribute *attr,
155 struct drm_connector *connector = to_drm_connector(device);
156 enum drm_connector_status status;
158 status = READ_ONCE(connector->status);
160 return snprintf(buf, PAGE_SIZE, "%s\n",
161 drm_get_connector_status_name(status));
164 static ssize_t dpms_show(struct device *device,
165 struct device_attribute *attr,
168 struct drm_connector *connector = to_drm_connector(device);
171 dpms = READ_ONCE(connector->dpms);
173 return snprintf(buf, PAGE_SIZE, "%s\n",
174 drm_get_dpms_name(dpms));
177 static ssize_t enabled_show(struct device *device,
178 struct device_attribute *attr,
181 struct drm_connector *connector = to_drm_connector(device);
184 enabled = READ_ONCE(connector->encoder);
186 return snprintf(buf, PAGE_SIZE, enabled ? "enabled\n" : "disabled\n");
189 static ssize_t edid_show(struct file *filp, struct kobject *kobj,
190 struct bin_attribute *attr, char *buf, loff_t off,
193 struct device *connector_dev = kobj_to_dev(kobj);
194 struct drm_connector *connector = to_drm_connector(connector_dev);
199 mutex_lock(&connector->dev->mode_config.mutex);
200 if (!connector->edid_blob_ptr)
203 edid = connector->edid_blob_ptr->data;
204 size = connector->edid_blob_ptr->length;
211 if (off + count > size)
213 memcpy(buf, edid + off, count);
217 mutex_unlock(&connector->dev->mode_config.mutex);
222 static ssize_t modes_show(struct device *device,
223 struct device_attribute *attr,
226 struct drm_connector *connector = to_drm_connector(device);
227 struct drm_display_mode *mode;
230 mutex_lock(&connector->dev->mode_config.mutex);
231 list_for_each_entry(mode, &connector->modes, head) {
232 written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
235 mutex_unlock(&connector->dev->mode_config.mutex);
240 static DEVICE_ATTR_RW(status);
241 static DEVICE_ATTR_RO(enabled);
242 static DEVICE_ATTR_RO(dpms);
243 static DEVICE_ATTR_RO(modes);
245 static struct attribute *connector_dev_attrs[] = {
246 &dev_attr_status.attr,
247 &dev_attr_enabled.attr,
249 &dev_attr_modes.attr,
253 static struct bin_attribute edid_attr = {
260 static struct bin_attribute *connector_bin_attrs[] = {
265 static const struct attribute_group connector_dev_group = {
266 .attrs = connector_dev_attrs,
267 .bin_attrs = connector_bin_attrs,
270 static const struct attribute_group *connector_dev_groups[] = {
271 &connector_dev_group,
275 int drm_sysfs_connector_add(struct drm_connector *connector)
277 struct drm_device *dev = connector->dev;
283 device_create_with_groups(drm_class, dev->primary->kdev, 0,
284 connector, connector_dev_groups,
285 "card%d-%s", dev->primary->index,
287 DRM_DEBUG("adding \"%s\" to sysfs\n",
290 if (IS_ERR(connector->kdev)) {
291 DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector->kdev));
292 return PTR_ERR(connector->kdev);
295 /* Let userspace know we have a new connector */
296 drm_sysfs_hotplug_event(dev);
299 return sysfs_create_link(&connector->kdev->kobj,
300 &connector->ddc->dev.kobj, "ddc");
304 void drm_sysfs_connector_remove(struct drm_connector *connector)
306 if (!connector->kdev)
310 sysfs_remove_link(&connector->kdev->kobj, "ddc");
312 DRM_DEBUG("removing \"%s\" from sysfs\n",
315 device_unregister(connector->kdev);
316 connector->kdev = NULL;
319 void drm_sysfs_lease_event(struct drm_device *dev)
321 char *event_string = "LEASE=1";
322 char *envp[] = { event_string, NULL };
324 DRM_DEBUG("generating lease event\n");
326 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
330 * drm_sysfs_hotplug_event - generate a DRM uevent
333 * Send a uevent for the DRM device specified by @dev. Currently we only
334 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
335 * deal with other types of events.
337 void drm_sysfs_hotplug_event(struct drm_device *dev)
339 char *event_string = "HOTPLUG=1";
340 char *envp[] = { event_string, NULL };
342 DRM_DEBUG("generating hotplug event\n");
344 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
346 EXPORT_SYMBOL(drm_sysfs_hotplug_event);
348 static void drm_sysfs_release(struct device *dev)
353 struct device *drm_sysfs_minor_alloc(struct drm_minor *minor)
355 const char *minor_str;
359 if (minor->type == DRM_MINOR_RENDER)
360 minor_str = "renderD%d";
362 minor_str = "card%d";
364 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
366 return ERR_PTR(-ENOMEM);
368 device_initialize(kdev);
369 kdev->devt = MKDEV(DRM_MAJOR, minor->index);
370 kdev->class = drm_class;
371 kdev->type = &drm_sysfs_device_minor;
372 kdev->parent = minor->dev->dev;
373 kdev->release = drm_sysfs_release;
374 dev_set_drvdata(kdev, minor);
376 r = dev_set_name(kdev, minor_str, minor->index);
388 * drm_class_device_register - register new device with the DRM sysfs class
389 * @dev: device to register
391 * Registers a new &struct device within the DRM sysfs class. Essentially only
392 * used by ttm to have a place for its global settings. Drivers should never use
395 int drm_class_device_register(struct device *dev)
397 if (!drm_class || IS_ERR(drm_class))
400 dev->class = drm_class;
401 return device_register(dev);
403 EXPORT_SYMBOL_GPL(drm_class_device_register);
406 * drm_class_device_unregister - unregister device with the DRM sysfs class
407 * @dev: device to unregister
409 * Unregisters a &struct device from the DRM sysfs class. Essentially only used
410 * by ttm to have a place for its global settings. Drivers should never use
413 void drm_class_device_unregister(struct device *dev)
415 return device_unregister(dev);
417 EXPORT_SYMBOL_GPL(drm_class_device_unregister);