xref: /linux/drivers/gpu/drm/drm_sysfs.c (revision bd628c1bed7902ec1f24ba0fe70758949146abbe)
1 
2 /*
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 
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>
20 
21 #include <drm/drm_sysfs.h>
22 #include <drm/drmP.h>
23 #include "drm_internal.h"
24 
25 #define to_drm_minor(d) dev_get_drvdata(d)
26 #define to_drm_connector(d) dev_get_drvdata(d)
27 
28 /**
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 
42 static struct device_type drm_sysfs_device_minor = {
43 	.name = "drm_minor"
44 };
45 
46 struct class *drm_class;
47 
48 static char *drm_devnode(struct device *dev, umode_t *mode)
49 {
50 	return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
51 }
52 
53 static CLASS_ATTR_STRING(version, S_IRUGO, "drm 1.1.0 20060810");
54 
55 /**
56  * drm_sysfs_init - initialize sysfs helpers
57  *
58  * This is used to create the DRM class, which is the implicit parent of any
59  * other top-level DRM sysfs objects.
60  *
61  * You must call drm_sysfs_destroy() to release the allocated resources.
62  *
63  * Return: 0 on success, negative error code on failure.
64  */
65 int drm_sysfs_init(void)
66 {
67 	int err;
68 
69 	drm_class = class_create(THIS_MODULE, "drm");
70 	if (IS_ERR(drm_class))
71 		return PTR_ERR(drm_class);
72 
73 	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;
78 	}
79 
80 	drm_class->devnode = drm_devnode;
81 	return 0;
82 }
83 
84 /**
85  * drm_sysfs_destroy - destroys DRM class
86  *
87  * Destroy the DRM device class.
88  */
89 void drm_sysfs_destroy(void)
90 {
91 	if (IS_ERR_OR_NULL(drm_class))
92 		return;
93 	class_remove_file(drm_class, &class_attr_version.attr);
94 	class_destroy(drm_class);
95 	drm_class = NULL;
96 }
97 
98 /*
99  * Connector properties
100  */
101 static 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;
107 	enum drm_connector_force old_force;
108 	int ret;
109 
110 	ret = mutex_lock_interruptible(&dev->mode_config.mutex);
111 	if (ret)
112 		return ret;
113 
114 	old_force = connector->force;
115 
116 	if (sysfs_streq(buf, "detect"))
117 		connector->force = 0;
118 	else if (sysfs_streq(buf, "on"))
119 		connector->force = DRM_FORCE_ON;
120 	else if (sysfs_streq(buf, "on-digital"))
121 		connector->force = DRM_FORCE_ON_DIGITAL;
122 	else if (sysfs_streq(buf, "off"))
123 		connector->force = DRM_FORCE_OFF;
124 	else
125 		ret = -EINVAL;
126 
127 	if (old_force != connector->force || !connector->force) {
128 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force updated from %d to %d or reprobing\n",
129 			      connector->base.id,
130 			      connector->name,
131 			      old_force, connector->force);
132 
133 		connector->funcs->fill_modes(connector,
134 					     dev->mode_config.max_width,
135 					     dev->mode_config.max_height);
136 	}
137 
138 	mutex_unlock(&dev->mode_config.mutex);
139 
140 	return ret ? ret : count;
141 }
142 
143 static 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);
148 	enum drm_connector_status status;
149 
150 	status = READ_ONCE(connector->status);
151 
152 	return snprintf(buf, PAGE_SIZE, "%s\n",
153 			drm_get_connector_status_name(status));
154 }
155 
156 static 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);
161 	int dpms;
162 
163 	dpms = READ_ONCE(connector->dpms);
164 
165 	return snprintf(buf, PAGE_SIZE, "%s\n",
166 			drm_get_dpms_name(dpms));
167 }
168 
169 static 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);
174 	bool enabled;
175 
176 	enabled = READ_ONCE(connector->encoder);
177 
178 	return snprintf(buf, PAGE_SIZE, enabled ? "enabled\n" : "disabled\n");
179 }
180 
181 static ssize_t edid_show(struct file *filp, struct kobject *kobj,
182 			 struct bin_attribute *attr, char *buf, loff_t off,
183 			 size_t count)
184 {
185 	struct device *connector_dev = kobj_to_dev(kobj);
186 	struct drm_connector *connector = to_drm_connector(connector_dev);
187 	unsigned char *edid;
188 	size_t size;
189 	ssize_t ret = 0;
190 
191 	mutex_lock(&connector->dev->mode_config.mutex);
192 	if (!connector->edid_blob_ptr)
193 		goto unlock;
194 
195 	edid = connector->edid_blob_ptr->data;
196 	size = connector->edid_blob_ptr->length;
197 	if (!edid)
198 		goto unlock;
199 
200 	if (off >= size)
201 		goto unlock;
202 
203 	if (off + count > size)
204 		count = size - off;
205 	memcpy(buf, edid + off, count);
206 
207 	ret = count;
208 unlock:
209 	mutex_unlock(&connector->dev->mode_config.mutex);
210 
211 	return ret;
212 }
213 
214 static 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 
222 	mutex_lock(&connector->dev->mode_config.mutex);
223 	list_for_each_entry(mode, &connector->modes, head) {
224 		written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
225 				    mode->name);
226 	}
227 	mutex_unlock(&connector->dev->mode_config.mutex);
228 
229 	return written;
230 }
231 
232 static DEVICE_ATTR_RW(status);
233 static DEVICE_ATTR_RO(enabled);
234 static DEVICE_ATTR_RO(dpms);
235 static DEVICE_ATTR_RO(modes);
236 
237 static 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
243 };
244 
245 static struct bin_attribute edid_attr = {
246 	.attr.name = "edid",
247 	.attr.mode = 0444,
248 	.size = 0,
249 	.read = edid_show,
250 };
251 
252 static struct bin_attribute *connector_bin_attrs[] = {
253 	&edid_attr,
254 	NULL
255 };
256 
257 static const struct attribute_group connector_dev_group = {
258 	.attrs = connector_dev_attrs,
259 	.bin_attrs = connector_bin_attrs,
260 };
261 
262 static const struct attribute_group *connector_dev_groups[] = {
263 	&connector_dev_group,
264 	NULL
265 };
266 
267 int drm_sysfs_connector_add(struct drm_connector *connector)
268 {
269 	struct drm_device *dev = connector->dev;
270 
271 	if (connector->kdev)
272 		return 0;
273 
274 	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);
279 	DRM_DEBUG("adding \"%s\" to sysfs\n",
280 		  connector->name);
281 
282 	if (IS_ERR(connector->kdev)) {
283 		DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector->kdev));
284 		return PTR_ERR(connector->kdev);
285 	}
286 
287 	/* Let userspace know we have a new connector */
288 	drm_sysfs_hotplug_event(dev);
289 
290 	return 0;
291 }
292 
293 void drm_sysfs_connector_remove(struct drm_connector *connector)
294 {
295 	if (!connector->kdev)
296 		return;
297 	DRM_DEBUG("removing \"%s\" from sysfs\n",
298 		  connector->name);
299 
300 	device_unregister(connector->kdev);
301 	connector->kdev = NULL;
302 }
303 
304 void drm_sysfs_lease_event(struct drm_device *dev)
305 {
306 	char *event_string = "LEASE=1";
307 	char *envp[] = { event_string, NULL };
308 
309 	DRM_DEBUG("generating lease event\n");
310 
311 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
312 }
313 
314 /**
315  * drm_sysfs_hotplug_event - generate a DRM uevent
316  * @dev: DRM device
317  *
318  * Send a uevent for the DRM device specified by @dev.  Currently we only
319  * set HOTPLUG=1 in the uevent environment, but this could be expanded to
320  * deal with other types of events.
321  */
322 void drm_sysfs_hotplug_event(struct drm_device *dev)
323 {
324 	char *event_string = "HOTPLUG=1";
325 	char *envp[] = { event_string, NULL };
326 
327 	DRM_DEBUG("generating hotplug event\n");
328 
329 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
330 }
331 EXPORT_SYMBOL(drm_sysfs_hotplug_event);
332 
333 static void drm_sysfs_release(struct device *dev)
334 {
335 	kfree(dev);
336 }
337 
338 struct device *drm_sysfs_minor_alloc(struct drm_minor *minor)
339 {
340 	const char *minor_str;
341 	struct device *kdev;
342 	int r;
343 
344 	if (minor->type == DRM_MINOR_RENDER)
345 		minor_str = "renderD%d";
346 	else
347 		minor_str = "card%d";
348 
349 	kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
350 	if (!kdev)
351 		return ERR_PTR(-ENOMEM);
352 
353 	device_initialize(kdev);
354 	kdev->devt = MKDEV(DRM_MAJOR, minor->index);
355 	kdev->class = drm_class;
356 	kdev->type = &drm_sysfs_device_minor;
357 	kdev->parent = minor->dev->dev;
358 	kdev->release = drm_sysfs_release;
359 	dev_set_drvdata(kdev, minor);
360 
361 	r = dev_set_name(kdev, minor_str, minor->index);
362 	if (r < 0)
363 		goto err_free;
364 
365 	return kdev;
366 
367 err_free:
368 	put_device(kdev);
369 	return ERR_PTR(r);
370 }
371 
372 /**
373  * drm_class_device_register - register new device with the DRM sysfs class
374  * @dev: device to register
375  *
376  * Registers a new &struct device within the DRM sysfs class. Essentially only
377  * used by ttm to have a place for its global settings. Drivers should never use
378  * this.
379  */
380 int drm_class_device_register(struct device *dev)
381 {
382 	if (!drm_class || IS_ERR(drm_class))
383 		return -ENOENT;
384 
385 	dev->class = drm_class;
386 	return device_register(dev);
387 }
388 EXPORT_SYMBOL_GPL(drm_class_device_register);
389 
390 /**
391  * drm_class_device_unregister - unregister device with the DRM sysfs class
392  * @dev: device to unregister
393  *
394  * Unregisters a &struct device from the DRM sysfs class. Essentially only used
395  * by ttm to have a place for its global settings. Drivers should never use
396  * this.
397  */
398 void drm_class_device_unregister(struct device *dev)
399 {
400 	return device_unregister(dev);
401 }
402 EXPORT_SYMBOL_GPL(drm_class_device_unregister);
403