3 * drm_sysfs.c - Modifications to drm_sysfs_class.c to support
4 * extra sysfs attribute from DRM. Normal drm_sysfs_class
5 * does not allow adding attributes.
7 * Copyright (c) 2004 Jon Smirl <jonsmirl@gmail.com>
8 * Copyright (c) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
9 * Copyright (c) 2003-2004 IBM Corp.
11 * This file is released under the GPLv2
15 #include <linux/device.h>
16 #include <linux/kdev_t.h>
17 #include <linux/gfp.h>
18 #include <linux/err.h>
19 #include <linux/export.h>
21 #include <drm/drm_sysfs.h>
23 #include "drm_internal.h"
25 #define to_drm_minor(d) dev_get_drvdata(d)
26 #define to_drm_connector(d) dev_get_drvdata(d)
31 * DRM provides very little additional support to drivers for sysfs
32 * interactions, beyond just all the standard stuff. Drivers who want to expose
33 * additional sysfs properties and property groups can attach them at either
34 * &drm_device.dev or &drm_connector.kdev.
36 * Registration is automatically handled when calling drm_dev_register(), or
37 * drm_connector_register() in case of hot-plugged connectors. Unregistration is
38 * also automatically handled by drm_dev_unregister() and
39 * drm_connector_unregister().
42 static struct device_type drm_sysfs_device_minor = {
46 struct class *drm_class;
48 static char *drm_devnode(struct device *dev, umode_t *mode)
50 return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
53 static CLASS_ATTR_STRING(version, S_IRUGO, "drm 1.1.0 20060810");
56 * drm_sysfs_init - initialize sysfs helpers
58 * This is used to create the DRM class, which is the implicit parent of any
59 * other top-level DRM sysfs objects.
61 * You must call drm_sysfs_destroy() to release the allocated resources.
63 * Return: 0 on success, negative error code on failure.
65 int drm_sysfs_init(void)
69 drm_class = class_create(THIS_MODULE, "drm");
70 if (IS_ERR(drm_class))
71 return PTR_ERR(drm_class);
73 err = class_create_file(drm_class, &class_attr_version.attr);
75 class_destroy(drm_class);
80 drm_class->devnode = drm_devnode;
81 drm_setup_hdcp_srm(drm_class);
86 * drm_sysfs_destroy - destroys DRM class
88 * Destroy the DRM device class.
90 void drm_sysfs_destroy(void)
92 if (IS_ERR_OR_NULL(drm_class))
94 drm_teardown_hdcp_srm(drm_class);
95 class_remove_file(drm_class, &class_attr_version.attr);
96 class_destroy(drm_class);
101 * Connector properties
103 static ssize_t status_store(struct device *device,
104 struct device_attribute *attr,
105 const char *buf, size_t count)
107 struct drm_connector *connector = to_drm_connector(device);
108 struct drm_device *dev = connector->dev;
109 enum drm_connector_force old_force;
112 ret = mutex_lock_interruptible(&dev->mode_config.mutex);
116 old_force = connector->force;
118 if (sysfs_streq(buf, "detect"))
119 connector->force = 0;
120 else if (sysfs_streq(buf, "on"))
121 connector->force = DRM_FORCE_ON;
122 else if (sysfs_streq(buf, "on-digital"))
123 connector->force = DRM_FORCE_ON_DIGITAL;
124 else if (sysfs_streq(buf, "off"))
125 connector->force = DRM_FORCE_OFF;
129 if (old_force != connector->force || !connector->force) {
130 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force updated from %d to %d or reprobing\n",
133 old_force, connector->force);
135 connector->funcs->fill_modes(connector,
136 dev->mode_config.max_width,
137 dev->mode_config.max_height);
140 mutex_unlock(&dev->mode_config.mutex);
142 return ret ? ret : count;
145 static ssize_t status_show(struct device *device,
146 struct device_attribute *attr,
149 struct drm_connector *connector = to_drm_connector(device);
150 enum drm_connector_status status;
152 status = READ_ONCE(connector->status);
154 return snprintf(buf, PAGE_SIZE, "%s\n",
155 drm_get_connector_status_name(status));
158 static ssize_t dpms_show(struct device *device,
159 struct device_attribute *attr,
162 struct drm_connector *connector = to_drm_connector(device);
165 dpms = READ_ONCE(connector->dpms);
167 return snprintf(buf, PAGE_SIZE, "%s\n",
168 drm_get_dpms_name(dpms));
171 static ssize_t enabled_show(struct device *device,
172 struct device_attribute *attr,
175 struct drm_connector *connector = to_drm_connector(device);
178 enabled = READ_ONCE(connector->encoder);
180 return snprintf(buf, PAGE_SIZE, enabled ? "enabled\n" : "disabled\n");
183 static ssize_t edid_show(struct file *filp, struct kobject *kobj,
184 struct bin_attribute *attr, char *buf, loff_t off,
187 struct device *connector_dev = kobj_to_dev(kobj);
188 struct drm_connector *connector = to_drm_connector(connector_dev);
193 mutex_lock(&connector->dev->mode_config.mutex);
194 if (!connector->edid_blob_ptr)
197 edid = connector->edid_blob_ptr->data;
198 size = connector->edid_blob_ptr->length;
205 if (off + count > size)
207 memcpy(buf, edid + off, count);
211 mutex_unlock(&connector->dev->mode_config.mutex);
216 static ssize_t modes_show(struct device *device,
217 struct device_attribute *attr,
220 struct drm_connector *connector = to_drm_connector(device);
221 struct drm_display_mode *mode;
224 mutex_lock(&connector->dev->mode_config.mutex);
225 list_for_each_entry(mode, &connector->modes, head) {
226 written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
229 mutex_unlock(&connector->dev->mode_config.mutex);
234 static DEVICE_ATTR_RW(status);
235 static DEVICE_ATTR_RO(enabled);
236 static DEVICE_ATTR_RO(dpms);
237 static DEVICE_ATTR_RO(modes);
239 static struct attribute *connector_dev_attrs[] = {
240 &dev_attr_status.attr,
241 &dev_attr_enabled.attr,
243 &dev_attr_modes.attr,
247 static struct bin_attribute edid_attr = {
254 static struct bin_attribute *connector_bin_attrs[] = {
259 static const struct attribute_group connector_dev_group = {
260 .attrs = connector_dev_attrs,
261 .bin_attrs = connector_bin_attrs,
264 static const struct attribute_group *connector_dev_groups[] = {
265 &connector_dev_group,
269 int drm_sysfs_connector_add(struct drm_connector *connector)
271 struct drm_device *dev = connector->dev;
277 device_create_with_groups(drm_class, dev->primary->kdev, 0,
278 connector, connector_dev_groups,
279 "card%d-%s", dev->primary->index,
281 DRM_DEBUG("adding \"%s\" to sysfs\n",
284 if (IS_ERR(connector->kdev)) {
285 DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector->kdev));
286 return PTR_ERR(connector->kdev);
289 /* Let userspace know we have a new connector */
290 drm_sysfs_hotplug_event(dev);
295 void drm_sysfs_connector_remove(struct drm_connector *connector)
297 if (!connector->kdev)
299 DRM_DEBUG("removing \"%s\" from sysfs\n",
302 device_unregister(connector->kdev);
303 connector->kdev = NULL;
306 void drm_sysfs_lease_event(struct drm_device *dev)
308 char *event_string = "LEASE=1";
309 char *envp[] = { event_string, NULL };
311 DRM_DEBUG("generating lease event\n");
313 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
317 * drm_sysfs_hotplug_event - generate a DRM uevent
320 * Send a uevent for the DRM device specified by @dev. Currently we only
321 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
322 * deal with other types of events.
324 void drm_sysfs_hotplug_event(struct drm_device *dev)
326 char *event_string = "HOTPLUG=1";
327 char *envp[] = { event_string, NULL };
329 DRM_DEBUG("generating hotplug event\n");
331 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
333 EXPORT_SYMBOL(drm_sysfs_hotplug_event);
335 static void drm_sysfs_release(struct device *dev)
340 struct device *drm_sysfs_minor_alloc(struct drm_minor *minor)
342 const char *minor_str;
346 if (minor->type == DRM_MINOR_RENDER)
347 minor_str = "renderD%d";
349 minor_str = "card%d";
351 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
353 return ERR_PTR(-ENOMEM);
355 device_initialize(kdev);
356 kdev->devt = MKDEV(DRM_MAJOR, minor->index);
357 kdev->class = drm_class;
358 kdev->type = &drm_sysfs_device_minor;
359 kdev->parent = minor->dev->dev;
360 kdev->release = drm_sysfs_release;
361 dev_set_drvdata(kdev, minor);
363 r = dev_set_name(kdev, minor_str, minor->index);
375 * drm_class_device_register - register new device with the DRM sysfs class
376 * @dev: device to register
378 * Registers a new &struct device within the DRM sysfs class. Essentially only
379 * used by ttm to have a place for its global settings. Drivers should never use
382 int drm_class_device_register(struct device *dev)
384 if (!drm_class || IS_ERR(drm_class))
387 dev->class = drm_class;
388 return device_register(dev);
390 EXPORT_SYMBOL_GPL(drm_class_device_register);
393 * drm_class_device_unregister - unregister device with the DRM sysfs class
394 * @dev: device to unregister
396 * Unregisters a &struct device from the DRM sysfs class. Essentially only used
397 * by ttm to have a place for its global settings. Drivers should never use
400 void drm_class_device_unregister(struct device *dev)
402 return device_unregister(dev);
404 EXPORT_SYMBOL_GPL(drm_class_device_unregister);