blob: 9a37196c1bf1728b7ddbc25f0e66937376cd09bc [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
Thomas Hellstrom08e4d532009-08-20 19:02:31 +100028static struct device_type drm_sysfs_device_minor = {
29 .name = "drm_minor"
30};
31
David Herrmannfcc90212015-09-09 14:21:30 +020032struct class *drm_class;
33
Al Viro2c9ede52011-07-23 20:24:48 -040034static char *drm_devnode(struct device *dev, umode_t *mode)
Kay Sievers02200d02009-04-30 15:23:42 +020035{
36 return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
37}
38
David Herrmann82d5e732016-09-01 14:48:36 +020039static CLASS_ATTR_STRING(version, S_IRUGO, "drm 1.1.0 20060810");
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/**
David Herrmannfcc90212015-09-09 14:21:30 +020042 * drm_sysfs_init - initialize sysfs helpers
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 *
David Herrmannfcc90212015-09-09 14:21:30 +020044 * This is used to create the DRM class, which is the implicit parent of any
45 * other top-level DRM sysfs objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 *
David Herrmannfcc90212015-09-09 14:21:30 +020047 * You must call drm_sysfs_destroy() to release the allocated resources.
48 *
49 * Return: 0 on success, negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
David Herrmannfcc90212015-09-09 14:21:30 +020051int drm_sysfs_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Jeff Garzik24f73c92006-10-10 14:23:37 -070053 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David Herrmannfcc90212015-09-09 14:21:30 +020055 drm_class = class_create(THIS_MODULE, "drm");
56 if (IS_ERR(drm_class))
57 return PTR_ERR(drm_class);
58
David Herrmannfcc90212015-09-09 14:21:30 +020059 err = class_create_file(drm_class, &class_attr_version.attr);
60 if (err) {
61 class_destroy(drm_class);
62 drm_class = NULL;
63 return err;
Jeff Garzik24f73c92006-10-10 14:23:37 -070064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
David Herrmannfcc90212015-09-09 14:21:30 +020066 drm_class->devnode = drm_devnode;
67 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70/**
Jesse Barnese8b962b2007-11-22 14:02:38 +100071 * drm_sysfs_destroy - destroys DRM class
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *
Jesse Barnese8b962b2007-11-22 14:02:38 +100073 * Destroy the DRM device class.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
Jesse Barnese8b962b2007-11-22 14:02:38 +100075void drm_sysfs_destroy(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
David Herrmann26b91ae2015-09-09 14:21:29 +020077 if (IS_ERR_OR_NULL(drm_class))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return;
Andi Kleen0933e2d2010-01-05 12:48:09 +010079 class_remove_file(drm_class, &class_attr_version.attr);
Jesse Barnese8b962b2007-11-22 14:02:38 +100080 class_destroy(drm_class);
Dave Airlie49099c42012-07-06 18:06:42 +010081 drm_class = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Dave Airlief453ba02008-11-07 14:05:41 -080084/*
85 * Connector properties
86 */
Chris Wilsonc484f022015-03-06 12:36:42 +000087static ssize_t status_store(struct device *device,
88 struct device_attribute *attr,
89 const char *buf, size_t count)
90{
91 struct drm_connector *connector = to_drm_connector(device);
92 struct drm_device *dev = connector->dev;
Daniel Vettered293f72015-11-19 17:46:50 +010093 enum drm_connector_force old_force;
Chris Wilsonc484f022015-03-06 12:36:42 +000094 int ret;
95
96 ret = mutex_lock_interruptible(&dev->mode_config.mutex);
97 if (ret)
98 return ret;
99
Daniel Vettered293f72015-11-19 17:46:50 +0100100 old_force = connector->force;
Chris Wilsonc484f022015-03-06 12:36:42 +0000101
Daniel Vettered293f72015-11-19 17:46:50 +0100102 if (sysfs_streq(buf, "detect"))
Chris Wilsonc484f022015-03-06 12:36:42 +0000103 connector->force = 0;
Daniel Vettered293f72015-11-19 17:46:50 +0100104 else if (sysfs_streq(buf, "on"))
Chris Wilsonc484f022015-03-06 12:36:42 +0000105 connector->force = DRM_FORCE_ON;
Daniel Vettered293f72015-11-19 17:46:50 +0100106 else if (sysfs_streq(buf, "on-digital"))
Chris Wilsonc484f022015-03-06 12:36:42 +0000107 connector->force = DRM_FORCE_ON_DIGITAL;
Daniel Vettered293f72015-11-19 17:46:50 +0100108 else if (sysfs_streq(buf, "off"))
Chris Wilsonc484f022015-03-06 12:36:42 +0000109 connector->force = DRM_FORCE_OFF;
Daniel Vettered293f72015-11-19 17:46:50 +0100110 else
Chris Wilsonc484f022015-03-06 12:36:42 +0000111 ret = -EINVAL;
112
Daniel Vettered293f72015-11-19 17:46:50 +0100113 if (old_force != connector->force || !connector->force) {
114 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force updated from %d to %d or reprobing\n",
Chris Wilsonc484f022015-03-06 12:36:42 +0000115 connector->base.id,
116 connector->name,
Daniel Vettered293f72015-11-19 17:46:50 +0100117 old_force, connector->force);
Chris Wilsonc484f022015-03-06 12:36:42 +0000118
Daniel Vettered293f72015-11-19 17:46:50 +0100119 connector->funcs->fill_modes(connector,
120 dev->mode_config.max_width,
121 dev->mode_config.max_height);
Chris Wilsonc484f022015-03-06 12:36:42 +0000122 }
123
124 mutex_unlock(&dev->mode_config.mutex);
125
Russell King38d85712015-06-06 08:27:30 +1000126 return ret ? ret : count;
Chris Wilsonc484f022015-03-06 12:36:42 +0000127}
128
Dave Airlief453ba02008-11-07 14:05:41 -0800129static ssize_t status_show(struct device *device,
130 struct device_attribute *attr,
131 char *buf)
132{
133 struct drm_connector *connector = to_drm_connector(device);
Daniel Vetter4eb9b942016-03-30 11:45:13 +0200134 enum drm_connector_status status;
135
136 status = READ_ONCE(connector->status);
Chris Wilson007c80a2011-03-15 11:40:00 +0000137
Keith Packard75185c92009-05-30 20:42:25 -0700138 return snprintf(buf, PAGE_SIZE, "%s\n",
Daniel Vetter4eb9b942016-03-30 11:45:13 +0200139 drm_get_connector_status_name(status));
Dave Airlief453ba02008-11-07 14:05:41 -0800140}
141
142static ssize_t dpms_show(struct device *device,
143 struct device_attribute *attr,
144 char *buf)
145{
146 struct drm_connector *connector = to_drm_connector(device);
Daniel Vetter621bd0f2015-09-29 09:56:53 +0200147 int dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800148
Daniel Vetter621bd0f2015-09-29 09:56:53 +0200149 dpms = READ_ONCE(connector->dpms);
Dave Airlief453ba02008-11-07 14:05:41 -0800150
Keith Packard75185c92009-05-30 20:42:25 -0700151 return snprintf(buf, PAGE_SIZE, "%s\n",
Daniel Vetter621bd0f2015-09-29 09:56:53 +0200152 drm_get_dpms_name(dpms));
Dave Airlief453ba02008-11-07 14:05:41 -0800153}
154
155static ssize_t enabled_show(struct device *device,
156 struct device_attribute *attr,
157 char *buf)
158{
159 struct drm_connector *connector = to_drm_connector(device);
Daniel Vetter4eb9b942016-03-30 11:45:13 +0200160 bool enabled;
Dave Airlief453ba02008-11-07 14:05:41 -0800161
Daniel Vetter4eb9b942016-03-30 11:45:13 +0200162 enabled = READ_ONCE(connector->encoder);
163
164 return snprintf(buf, PAGE_SIZE, enabled ? "enabled\n" : "disabled\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800165}
166
Chris Wright2c3c8be2010-05-12 18:28:57 -0700167static ssize_t edid_show(struct file *filp, struct kobject *kobj,
168 struct bin_attribute *attr, char *buf, loff_t off,
169 size_t count)
Dave Airlief453ba02008-11-07 14:05:41 -0800170{
Geliang Tangd122cbf2016-01-13 22:48:41 +0800171 struct device *connector_dev = kobj_to_dev(kobj);
Dave Airlief453ba02008-11-07 14:05:41 -0800172 struct drm_connector *connector = to_drm_connector(connector_dev);
173 unsigned char *edid;
174 size_t size;
Daniel Vettera48a62b2015-10-02 13:01:02 +0200175 ssize_t ret = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800176
Daniel Vettera48a62b2015-10-02 13:01:02 +0200177 mutex_lock(&connector->dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800178 if (!connector->edid_blob_ptr)
Daniel Vettera48a62b2015-10-02 13:01:02 +0200179 goto unlock;
Dave Airlief453ba02008-11-07 14:05:41 -0800180
181 edid = connector->edid_blob_ptr->data;
182 size = connector->edid_blob_ptr->length;
183 if (!edid)
Daniel Vettera48a62b2015-10-02 13:01:02 +0200184 goto unlock;
Dave Airlief453ba02008-11-07 14:05:41 -0800185
186 if (off >= size)
Daniel Vettera48a62b2015-10-02 13:01:02 +0200187 goto unlock;
Dave Airlief453ba02008-11-07 14:05:41 -0800188
189 if (off + count > size)
190 count = size - off;
191 memcpy(buf, edid + off, count);
192
Daniel Vettera48a62b2015-10-02 13:01:02 +0200193 ret = count;
194unlock:
195 mutex_unlock(&connector->dev->mode_config.mutex);
196
197 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800198}
199
200static ssize_t modes_show(struct device *device,
201 struct device_attribute *attr,
202 char *buf)
203{
204 struct drm_connector *connector = to_drm_connector(device);
205 struct drm_display_mode *mode;
206 int written = 0;
207
Daniel Vettera48a62b2015-10-02 13:01:02 +0200208 mutex_lock(&connector->dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800209 list_for_each_entry(mode, &connector->modes, head) {
210 written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
211 mode->name);
212 }
Daniel Vettera48a62b2015-10-02 13:01:02 +0200213 mutex_unlock(&connector->dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800214
215 return written;
216}
217
Chris Wilsonc484f022015-03-06 12:36:42 +0000218static DEVICE_ATTR_RW(status);
Takashi Iwai335f1a62015-02-04 11:58:53 +0100219static DEVICE_ATTR_RO(enabled);
220static DEVICE_ATTR_RO(dpms);
221static DEVICE_ATTR_RO(modes);
222
223static struct attribute *connector_dev_attrs[] = {
224 &dev_attr_status.attr,
225 &dev_attr_enabled.attr,
226 &dev_attr_dpms.attr,
227 &dev_attr_modes.attr,
228 NULL
Dave Airlief453ba02008-11-07 14:05:41 -0800229};
230
Dave Airlief453ba02008-11-07 14:05:41 -0800231static struct bin_attribute edid_attr = {
232 .attr.name = "edid",
Keith Packarde36ebaf2009-05-30 20:42:26 -0700233 .attr.mode = 0444,
Adam Jackson7466f4c2010-03-29 21:43:23 +0000234 .size = 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800235 .read = edid_show,
236};
237
Takashi Iwai335f1a62015-02-04 11:58:53 +0100238static struct bin_attribute *connector_bin_attrs[] = {
239 &edid_attr,
240 NULL
241};
242
243static const struct attribute_group connector_dev_group = {
244 .attrs = connector_dev_attrs,
245 .bin_attrs = connector_bin_attrs,
246};
247
Takashi Iwai335f1a62015-02-04 11:58:53 +0100248static const struct attribute_group *connector_dev_groups[] = {
249 &connector_dev_group,
Takashi Iwai335f1a62015-02-04 11:58:53 +0100250 NULL
251};
252
Dave Airlief453ba02008-11-07 14:05:41 -0800253/**
Laurent Pinchart3b02ab82012-05-17 13:27:20 +0200254 * drm_sysfs_connector_add - add a connector to sysfs
Dave Airlief453ba02008-11-07 14:05:41 -0800255 * @connector: connector to add
256 *
Laurent Pinchart3b02ab82012-05-17 13:27:20 +0200257 * Create a connector device in sysfs, along with its associated connector
Dave Airlief453ba02008-11-07 14:05:41 -0800258 * properties (so far, connection status, dpms, mode list & edid) and
259 * generate a hotplug event so userspace knows there's a new connector
260 * available.
Dave Airlief453ba02008-11-07 14:05:41 -0800261 */
262int drm_sysfs_connector_add(struct drm_connector *connector)
263{
264 struct drm_device *dev = connector->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800265
Dave Airlie5bdebb12013-10-11 14:07:25 +1000266 if (connector->kdev)
267 return 0;
268
Takashi Iwai335f1a62015-02-04 11:58:53 +0100269 connector->kdev =
270 device_create_with_groups(drm_class, dev->primary->kdev, 0,
271 connector, connector_dev_groups,
272 "card%d-%s", dev->primary->index,
273 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800274 DRM_DEBUG("adding \"%s\" to sysfs\n",
Jani Nikula25933822014-06-03 14:56:20 +0300275 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800276
Dave Airlie5bdebb12013-10-11 14:07:25 +1000277 if (IS_ERR(connector->kdev)) {
278 DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector->kdev));
Takashi Iwai335f1a62015-02-04 11:58:53 +0100279 return PTR_ERR(connector->kdev);
Dave Airlief453ba02008-11-07 14:05:41 -0800280 }
281
Dave Airlief453ba02008-11-07 14:05:41 -0800282 /* Let userspace know we have a new connector */
283 drm_sysfs_hotplug_event(dev);
284
285 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800286}
Dave Airlief453ba02008-11-07 14:05:41 -0800287
288/**
289 * drm_sysfs_connector_remove - remove an connector device from sysfs
290 * @connector: connector to remove
291 *
292 * Remove @connector and its associated attributes from sysfs. Note that
293 * the device model core will take care of sending the "remove" uevent
294 * at this time, so we don't need to do it.
295 *
296 * Note:
297 * This routine should only be called if the connector was previously
298 * successfully registered. If @connector hasn't been registered yet,
299 * you'll likely see a panic somewhere deep in sysfs code when called.
300 */
301void drm_sysfs_connector_remove(struct drm_connector *connector)
302{
Dave Airlie5bdebb12013-10-11 14:07:25 +1000303 if (!connector->kdev)
Dave Airlie1828fe62012-02-20 14:15:02 +0000304 return;
Dave Airlief453ba02008-11-07 14:05:41 -0800305 DRM_DEBUG("removing \"%s\" from sysfs\n",
Jani Nikula25933822014-06-03 14:56:20 +0300306 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800307
Dave Airlie5bdebb12013-10-11 14:07:25 +1000308 device_unregister(connector->kdev);
309 connector->kdev = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800310}
Dave Airlief453ba02008-11-07 14:05:41 -0800311
312/**
313 * drm_sysfs_hotplug_event - generate a DRM uevent
314 * @dev: DRM device
315 *
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.
319 */
320void drm_sysfs_hotplug_event(struct drm_device *dev)
321{
322 char *event_string = "HOTPLUG=1";
323 char *envp[] = { event_string, NULL };
324
325 DRM_DEBUG("generating hotplug event\n");
326
Dave Airlie5bdebb12013-10-11 14:07:25 +1000327 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
Dave Airlief453ba02008-11-07 14:05:41 -0800328}
Jesse Barnes5ca58282009-03-31 14:11:15 -0700329EXPORT_SYMBOL(drm_sysfs_hotplug_event);
Dave Airlief453ba02008-11-07 14:05:41 -0800330
David Herrmann760c9602013-11-21 20:50:50 +1000331static void drm_sysfs_release(struct device *dev)
332{
333 kfree(dev);
334}
335
Jesse Barnese8b962b2007-11-22 14:02:38 +1000336/**
David Herrmanne1728072014-07-23 11:38:38 +0200337 * drm_sysfs_minor_alloc() - Allocate sysfs device for given minor
338 * @minor: minor to allocate sysfs device for
Jesse Barnese8b962b2007-11-22 14:02:38 +1000339 *
David Herrmanne1728072014-07-23 11:38:38 +0200340 * This allocates a new sysfs device for @minor and returns it. The device is
341 * not registered nor linked. The caller has to use device_add() and
342 * device_del() to register and unregister it.
343 *
344 * Note that dev_get_drvdata() on the new device will return the minor.
345 * However, the device does not hold a ref-count to the minor nor to the
346 * underlying drm_device. This is unproblematic as long as you access the
347 * private data only in sysfs callbacks. device_del() disables those
348 * synchronously, so they cannot be called after you cleanup a minor.
Jesse Barnese8b962b2007-11-22 14:02:38 +1000349 */
David Herrmanne1728072014-07-23 11:38:38 +0200350struct device *drm_sysfs_minor_alloc(struct drm_minor *minor)
Jesse Barnese8b962b2007-11-22 14:02:38 +1000351{
David Herrmanne1728072014-07-23 11:38:38 +0200352 const char *minor_str;
353 struct device *kdev;
David Herrmann760c9602013-11-21 20:50:50 +1000354 int r;
Jesse Barnese8b962b2007-11-22 14:02:38 +1000355
Dave Airlief453ba02008-11-07 14:05:41 -0800356 if (minor->type == DRM_MINOR_CONTROL)
357 minor_str = "controlD%d";
David Herrmanne1728072014-07-23 11:38:38 +0200358 else if (minor->type == DRM_MINOR_RENDER)
359 minor_str = "renderD%d";
360 else
361 minor_str = "card%d";
Jesse Barnese8b962b2007-11-22 14:02:38 +1000362
David Herrmanne1728072014-07-23 11:38:38 +0200363 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
364 if (!kdev)
365 return ERR_PTR(-ENOMEM);
David Herrmann760c9602013-11-21 20:50:50 +1000366
David Herrmanne1728072014-07-23 11:38:38 +0200367 device_initialize(kdev);
368 kdev->devt = MKDEV(DRM_MAJOR, minor->index);
369 kdev->class = drm_class;
370 kdev->type = &drm_sysfs_device_minor;
371 kdev->parent = minor->dev->dev;
372 kdev->release = drm_sysfs_release;
373 dev_set_drvdata(kdev, minor);
David Herrmann760c9602013-11-21 20:50:50 +1000374
David Herrmanne1728072014-07-23 11:38:38 +0200375 r = dev_set_name(kdev, minor_str, minor->index);
David Herrmann760c9602013-11-21 20:50:50 +1000376 if (r < 0)
David Herrmanne1728072014-07-23 11:38:38 +0200377 goto err_free;
David Herrmann760c9602013-11-21 20:50:50 +1000378
David Herrmanne1728072014-07-23 11:38:38 +0200379 return kdev;
David Herrmann760c9602013-11-21 20:50:50 +1000380
David Herrmanne1728072014-07-23 11:38:38 +0200381err_free:
382 put_device(kdev);
383 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386/**
Thomas Hellstrom327c2252009-08-17 16:28:37 +0200387 * drm_class_device_register - Register a struct device in the drm class.
388 *
389 * @dev: pointer to struct device to register.
390 *
391 * @dev should have all relevant members pre-filled with the exception
392 * of the class member. In particular, the device_type member must
393 * be set.
394 */
395
396int drm_class_device_register(struct device *dev)
397{
Dave Airlie49099c42012-07-06 18:06:42 +0100398 if (!drm_class || IS_ERR(drm_class))
399 return -ENOENT;
400
Thomas Hellstrom327c2252009-08-17 16:28:37 +0200401 dev->class = drm_class;
402 return device_register(dev);
403}
404EXPORT_SYMBOL_GPL(drm_class_device_register);
405
406void drm_class_device_unregister(struct device *dev)
407{
408 return device_unregister(dev);
409}
410EXPORT_SYMBOL_GPL(drm_class_device_unregister);