blob: 1c5b5ce1fd7f4861f40064cfcaf73963ebd0fcee [file] [log] [blame]
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -08001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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.
6 *
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.
10 *
11 * This file is released under the GPLv2
12 *
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/device.h>
16#include <linux/kdev_t.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/err.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040019#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
David Howells760285e2012-10-02 18:01:07 +010021#include <drm/drm_sysfs.h>
David Howells760285e2012-10-02 18:01:07 +010022#include <drm/drmP.h>
Daniel Vetter67d0ec42014-09-10 12:43:53 +020023#include "drm_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Dave Airlie5bdebb12013-10-11 14:07:25 +100025#define to_drm_minor(d) dev_get_drvdata(d)
26#define to_drm_connector(d) dev_get_drvdata(d)
Jesse Barnese8b962b2007-11-22 14:02:38 +100027
Daniel Vettere2271702017-04-04 11:52:55 +020028/**
29 * DOC: overview
30 *
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.
35 *
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().
40 */
41
Thomas Hellstrom08e4d532009-08-20 19:02:31 +100042static struct device_type drm_sysfs_device_minor = {
43 .name = "drm_minor"
44};
45
David Herrmannfcc90212015-09-09 14:21:30 +020046struct class *drm_class;
47
Al Viro2c9ede52011-07-23 20:24:48 -040048static char *drm_devnode(struct device *dev, umode_t *mode)
Kay Sievers02200d02009-04-30 15:23:42 +020049{
50 return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
51}
52
David Herrmann82d5e732016-09-01 14:48:36 +020053static CLASS_ATTR_STRING(version, S_IRUGO, "drm 1.1.0 20060810");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/**
David Herrmannfcc90212015-09-09 14:21:30 +020056 * drm_sysfs_init - initialize sysfs helpers
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
David Herrmannfcc90212015-09-09 14:21:30 +020058 * This is used to create the DRM class, which is the implicit parent of any
59 * other top-level DRM sysfs objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 *
David Herrmannfcc90212015-09-09 14:21:30 +020061 * You must call drm_sysfs_destroy() to release the allocated resources.
62 *
63 * Return: 0 on success, negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
David Herrmannfcc90212015-09-09 14:21:30 +020065int drm_sysfs_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Jeff Garzik24f73c92006-10-10 14:23:37 -070067 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
David Herrmannfcc90212015-09-09 14:21:30 +020069 drm_class = class_create(THIS_MODULE, "drm");
70 if (IS_ERR(drm_class))
71 return PTR_ERR(drm_class);
72
David Herrmannfcc90212015-09-09 14:21:30 +020073 err = class_create_file(drm_class, &class_attr_version.attr);
74 if (err) {
75 class_destroy(drm_class);
76 drm_class = NULL;
77 return err;
Jeff Garzik24f73c92006-10-10 14:23:37 -070078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
David Herrmannfcc90212015-09-09 14:21:30 +020080 drm_class->devnode = drm_devnode;
81 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84/**
Jesse Barnese8b962b2007-11-22 14:02:38 +100085 * drm_sysfs_destroy - destroys DRM class
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 *
Jesse Barnese8b962b2007-11-22 14:02:38 +100087 * Destroy the DRM device class.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
Jesse Barnese8b962b2007-11-22 14:02:38 +100089void drm_sysfs_destroy(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
David Herrmann26b91ae2015-09-09 14:21:29 +020091 if (IS_ERR_OR_NULL(drm_class))
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return;
Andi Kleen0933e2d2010-01-05 12:48:09 +010093 class_remove_file(drm_class, &class_attr_version.attr);
Jesse Barnese8b962b2007-11-22 14:02:38 +100094 class_destroy(drm_class);
Dave Airlie49099c42012-07-06 18:06:42 +010095 drm_class = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Dave Airlief453ba02008-11-07 14:05:41 -080098/*
99 * Connector properties
100 */
Chris Wilsonc484f022015-03-06 12:36:42 +0000101static ssize_t status_store(struct device *device,
102 struct device_attribute *attr,
103 const char *buf, size_t count)
104{
105 struct drm_connector *connector = to_drm_connector(device);
106 struct drm_device *dev = connector->dev;
Daniel Vettered293f72015-11-19 17:46:50 +0100107 enum drm_connector_force old_force;
Chris Wilsonc484f022015-03-06 12:36:42 +0000108 int ret;
109
110 ret = mutex_lock_interruptible(&dev->mode_config.mutex);
111 if (ret)
112 return ret;
113
Daniel Vettered293f72015-11-19 17:46:50 +0100114 old_force = connector->force;
Chris Wilsonc484f022015-03-06 12:36:42 +0000115
Daniel Vettered293f72015-11-19 17:46:50 +0100116 if (sysfs_streq(buf, "detect"))
Chris Wilsonc484f022015-03-06 12:36:42 +0000117 connector->force = 0;
Daniel Vettered293f72015-11-19 17:46:50 +0100118 else if (sysfs_streq(buf, "on"))
Chris Wilsonc484f022015-03-06 12:36:42 +0000119 connector->force = DRM_FORCE_ON;
Daniel Vettered293f72015-11-19 17:46:50 +0100120 else if (sysfs_streq(buf, "on-digital"))
Chris Wilsonc484f022015-03-06 12:36:42 +0000121 connector->force = DRM_FORCE_ON_DIGITAL;
Daniel Vettered293f72015-11-19 17:46:50 +0100122 else if (sysfs_streq(buf, "off"))
Chris Wilsonc484f022015-03-06 12:36:42 +0000123 connector->force = DRM_FORCE_OFF;
Daniel Vettered293f72015-11-19 17:46:50 +0100124 else
Chris Wilsonc484f022015-03-06 12:36:42 +0000125 ret = -EINVAL;
126
Daniel Vettered293f72015-11-19 17:46:50 +0100127 if (old_force != connector->force || !connector->force) {
128 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force updated from %d to %d or reprobing\n",
Chris Wilsonc484f022015-03-06 12:36:42 +0000129 connector->base.id,
130 connector->name,
Daniel Vettered293f72015-11-19 17:46:50 +0100131 old_force, connector->force);
Chris Wilsonc484f022015-03-06 12:36:42 +0000132
Daniel Vettered293f72015-11-19 17:46:50 +0100133 connector->funcs->fill_modes(connector,
134 dev->mode_config.max_width,
135 dev->mode_config.max_height);
Chris Wilsonc484f022015-03-06 12:36:42 +0000136 }
137
138 mutex_unlock(&dev->mode_config.mutex);
139
Russell King38d85712015-06-06 08:27:30 +1000140 return ret ? ret : count;
Chris Wilsonc484f022015-03-06 12:36:42 +0000141}
142
Dave Airlief453ba02008-11-07 14:05:41 -0800143static ssize_t status_show(struct device *device,
144 struct device_attribute *attr,
145 char *buf)
146{
147 struct drm_connector *connector = to_drm_connector(device);
Daniel Vetter4eb9b942016-03-30 11:45:13 +0200148 enum drm_connector_status status;
149
150 status = READ_ONCE(connector->status);
Chris Wilson007c80a2011-03-15 11:40:00 +0000151
Keith Packard75185c92009-05-30 20:42:25 -0700152 return snprintf(buf, PAGE_SIZE, "%s\n",
Daniel Vetter4eb9b942016-03-30 11:45:13 +0200153 drm_get_connector_status_name(status));
Dave Airlief453ba02008-11-07 14:05:41 -0800154}
155
156static ssize_t dpms_show(struct device *device,
157 struct device_attribute *attr,
158 char *buf)
159{
160 struct drm_connector *connector = to_drm_connector(device);
Daniel Vetter621bd0f2015-09-29 09:56:53 +0200161 int dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800162
Daniel Vetter621bd0f2015-09-29 09:56:53 +0200163 dpms = READ_ONCE(connector->dpms);
Dave Airlief453ba02008-11-07 14:05:41 -0800164
Keith Packard75185c92009-05-30 20:42:25 -0700165 return snprintf(buf, PAGE_SIZE, "%s\n",
Daniel Vetter621bd0f2015-09-29 09:56:53 +0200166 drm_get_dpms_name(dpms));
Dave Airlief453ba02008-11-07 14:05:41 -0800167}
168
169static ssize_t enabled_show(struct device *device,
170 struct device_attribute *attr,
171 char *buf)
172{
173 struct drm_connector *connector = to_drm_connector(device);
Daniel Vetter4eb9b942016-03-30 11:45:13 +0200174 bool enabled;
Dave Airlief453ba02008-11-07 14:05:41 -0800175
Daniel Vetter4eb9b942016-03-30 11:45:13 +0200176 enabled = READ_ONCE(connector->encoder);
177
178 return snprintf(buf, PAGE_SIZE, enabled ? "enabled\n" : "disabled\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800179}
180
Chris Wright2c3c8be2010-05-12 18:28:57 -0700181static ssize_t edid_show(struct file *filp, struct kobject *kobj,
182 struct bin_attribute *attr, char *buf, loff_t off,
183 size_t count)
Dave Airlief453ba02008-11-07 14:05:41 -0800184{
Geliang Tangd122cbf2016-01-13 22:48:41 +0800185 struct device *connector_dev = kobj_to_dev(kobj);
Dave Airlief453ba02008-11-07 14:05:41 -0800186 struct drm_connector *connector = to_drm_connector(connector_dev);
187 unsigned char *edid;
188 size_t size;
Daniel Vettera48a62b2015-10-02 13:01:02 +0200189 ssize_t ret = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800190
Daniel Vettera48a62b2015-10-02 13:01:02 +0200191 mutex_lock(&connector->dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800192 if (!connector->edid_blob_ptr)
Daniel Vettera48a62b2015-10-02 13:01:02 +0200193 goto unlock;
Dave Airlief453ba02008-11-07 14:05:41 -0800194
195 edid = connector->edid_blob_ptr->data;
196 size = connector->edid_blob_ptr->length;
197 if (!edid)
Daniel Vettera48a62b2015-10-02 13:01:02 +0200198 goto unlock;
Dave Airlief453ba02008-11-07 14:05:41 -0800199
200 if (off >= size)
Daniel Vettera48a62b2015-10-02 13:01:02 +0200201 goto unlock;
Dave Airlief453ba02008-11-07 14:05:41 -0800202
203 if (off + count > size)
204 count = size - off;
205 memcpy(buf, edid + off, count);
206
Daniel Vettera48a62b2015-10-02 13:01:02 +0200207 ret = count;
208unlock:
209 mutex_unlock(&connector->dev->mode_config.mutex);
210
211 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800212}
213
214static ssize_t modes_show(struct device *device,
215 struct device_attribute *attr,
216 char *buf)
217{
218 struct drm_connector *connector = to_drm_connector(device);
219 struct drm_display_mode *mode;
220 int written = 0;
221
Daniel Vettera48a62b2015-10-02 13:01:02 +0200222 mutex_lock(&connector->dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800223 list_for_each_entry(mode, &connector->modes, head) {
224 written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
225 mode->name);
226 }
Daniel Vettera48a62b2015-10-02 13:01:02 +0200227 mutex_unlock(&connector->dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800228
229 return written;
230}
231
Chris Wilsonc484f022015-03-06 12:36:42 +0000232static DEVICE_ATTR_RW(status);
Takashi Iwai335f1a62015-02-04 11:58:53 +0100233static DEVICE_ATTR_RO(enabled);
234static DEVICE_ATTR_RO(dpms);
235static DEVICE_ATTR_RO(modes);
236
237static struct attribute *connector_dev_attrs[] = {
238 &dev_attr_status.attr,
239 &dev_attr_enabled.attr,
240 &dev_attr_dpms.attr,
241 &dev_attr_modes.attr,
242 NULL
Dave Airlief453ba02008-11-07 14:05:41 -0800243};
244
Dave Airlief453ba02008-11-07 14:05:41 -0800245static struct bin_attribute edid_attr = {
246 .attr.name = "edid",
Keith Packarde36ebaf2009-05-30 20:42:26 -0700247 .attr.mode = 0444,
Adam Jackson7466f4c2010-03-29 21:43:23 +0000248 .size = 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800249 .read = edid_show,
250};
251
Takashi Iwai335f1a62015-02-04 11:58:53 +0100252static struct bin_attribute *connector_bin_attrs[] = {
253 &edid_attr,
254 NULL
255};
256
257static const struct attribute_group connector_dev_group = {
258 .attrs = connector_dev_attrs,
259 .bin_attrs = connector_bin_attrs,
260};
261
Takashi Iwai335f1a62015-02-04 11:58:53 +0100262static const struct attribute_group *connector_dev_groups[] = {
263 &connector_dev_group,
Takashi Iwai335f1a62015-02-04 11:58:53 +0100264 NULL
265};
266
Dave Airlief453ba02008-11-07 14:05:41 -0800267int drm_sysfs_connector_add(struct drm_connector *connector)
268{
269 struct drm_device *dev = connector->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800270
Dave Airlie5bdebb12013-10-11 14:07:25 +1000271 if (connector->kdev)
272 return 0;
273
Takashi Iwai335f1a62015-02-04 11:58:53 +0100274 connector->kdev =
275 device_create_with_groups(drm_class, dev->primary->kdev, 0,
276 connector, connector_dev_groups,
277 "card%d-%s", dev->primary->index,
278 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800279 DRM_DEBUG("adding \"%s\" to sysfs\n",
Jani Nikula25933822014-06-03 14:56:20 +0300280 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800281
Dave Airlie5bdebb12013-10-11 14:07:25 +1000282 if (IS_ERR(connector->kdev)) {
283 DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector->kdev));
Takashi Iwai335f1a62015-02-04 11:58:53 +0100284 return PTR_ERR(connector->kdev);
Dave Airlief453ba02008-11-07 14:05:41 -0800285 }
286
Dave Airlief453ba02008-11-07 14:05:41 -0800287 /* Let userspace know we have a new connector */
288 drm_sysfs_hotplug_event(dev);
289
290 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800291}
Dave Airlief453ba02008-11-07 14:05:41 -0800292
Dave Airlief453ba02008-11-07 14:05:41 -0800293void drm_sysfs_connector_remove(struct drm_connector *connector)
294{
Dave Airlie5bdebb12013-10-11 14:07:25 +1000295 if (!connector->kdev)
Dave Airlie1828fe62012-02-20 14:15:02 +0000296 return;
Dave Airlief453ba02008-11-07 14:05:41 -0800297 DRM_DEBUG("removing \"%s\" from sysfs\n",
Jani Nikula25933822014-06-03 14:56:20 +0300298 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800299
Dave Airlie5bdebb12013-10-11 14:07:25 +1000300 device_unregister(connector->kdev);
301 connector->kdev = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800302}
Dave Airlief453ba02008-11-07 14:05:41 -0800303
304/**
305 * drm_sysfs_hotplug_event - generate a DRM uevent
306 * @dev: DRM device
307 *
308 * Send a uevent for the DRM device specified by @dev. Currently we only
309 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
310 * deal with other types of events.
311 */
312void drm_sysfs_hotplug_event(struct drm_device *dev)
313{
314 char *event_string = "HOTPLUG=1";
315 char *envp[] = { event_string, NULL };
316
317 DRM_DEBUG("generating hotplug event\n");
318
Dave Airlie5bdebb12013-10-11 14:07:25 +1000319 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
Dave Airlief453ba02008-11-07 14:05:41 -0800320}
Jesse Barnes5ca58282009-03-31 14:11:15 -0700321EXPORT_SYMBOL(drm_sysfs_hotplug_event);
Dave Airlief453ba02008-11-07 14:05:41 -0800322
David Herrmann760c9602013-11-21 20:50:50 +1000323static void drm_sysfs_release(struct device *dev)
324{
325 kfree(dev);
326}
327
David Herrmanne1728072014-07-23 11:38:38 +0200328struct device *drm_sysfs_minor_alloc(struct drm_minor *minor)
Jesse Barnese8b962b2007-11-22 14:02:38 +1000329{
David Herrmanne1728072014-07-23 11:38:38 +0200330 const char *minor_str;
331 struct device *kdev;
David Herrmann760c9602013-11-21 20:50:50 +1000332 int r;
Jesse Barnese8b962b2007-11-22 14:02:38 +1000333
Dave Airlief453ba02008-11-07 14:05:41 -0800334 if (minor->type == DRM_MINOR_CONTROL)
335 minor_str = "controlD%d";
David Herrmanne1728072014-07-23 11:38:38 +0200336 else if (minor->type == DRM_MINOR_RENDER)
337 minor_str = "renderD%d";
338 else
339 minor_str = "card%d";
Jesse Barnese8b962b2007-11-22 14:02:38 +1000340
David Herrmanne1728072014-07-23 11:38:38 +0200341 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
342 if (!kdev)
343 return ERR_PTR(-ENOMEM);
David Herrmann760c9602013-11-21 20:50:50 +1000344
David Herrmanne1728072014-07-23 11:38:38 +0200345 device_initialize(kdev);
346 kdev->devt = MKDEV(DRM_MAJOR, minor->index);
347 kdev->class = drm_class;
348 kdev->type = &drm_sysfs_device_minor;
349 kdev->parent = minor->dev->dev;
350 kdev->release = drm_sysfs_release;
351 dev_set_drvdata(kdev, minor);
David Herrmann760c9602013-11-21 20:50:50 +1000352
David Herrmanne1728072014-07-23 11:38:38 +0200353 r = dev_set_name(kdev, minor_str, minor->index);
David Herrmann760c9602013-11-21 20:50:50 +1000354 if (r < 0)
David Herrmanne1728072014-07-23 11:38:38 +0200355 goto err_free;
David Herrmann760c9602013-11-21 20:50:50 +1000356
David Herrmanne1728072014-07-23 11:38:38 +0200357 return kdev;
David Herrmann760c9602013-11-21 20:50:50 +1000358
David Herrmanne1728072014-07-23 11:38:38 +0200359err_free:
360 put_device(kdev);
361 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
364/**
Daniel Vettere2271702017-04-04 11:52:55 +0200365 * drm_class_device_register - register new device with the DRM sysfs class
366 * @dev: device to register
Thomas Hellstrom327c2252009-08-17 16:28:37 +0200367 *
Daniel Vettere2271702017-04-04 11:52:55 +0200368 * Registers a new &struct device within the DRM sysfs class. Essentially only
369 * used by ttm to have a place for its global settings. Drivers should never use
370 * this.
Thomas Hellstrom327c2252009-08-17 16:28:37 +0200371 */
Thomas Hellstrom327c2252009-08-17 16:28:37 +0200372int drm_class_device_register(struct device *dev)
373{
Dave Airlie49099c42012-07-06 18:06:42 +0100374 if (!drm_class || IS_ERR(drm_class))
375 return -ENOENT;
376
Thomas Hellstrom327c2252009-08-17 16:28:37 +0200377 dev->class = drm_class;
378 return device_register(dev);
379}
380EXPORT_SYMBOL_GPL(drm_class_device_register);
381
Daniel Vettere2271702017-04-04 11:52:55 +0200382/**
383 * drm_class_device_unregister - unregister device with the DRM sysfs class
384 * @dev: device to unregister
385 *
386 * Unregisters a &struct device from the DRM sysfs class. Essentially only used
387 * by ttm to have a place for its global settings. Drivers should never use
388 * this.
389 */
Thomas Hellstrom327c2252009-08-17 16:28:37 +0200390void drm_class_device_unregister(struct device *dev)
391{
392 return device_unregister(dev);
393}
394EXPORT_SYMBOL_GPL(drm_class_device_unregister);