• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 /*
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.
7  *
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.
11  */
12 
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>
20 
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>
28 
29 #include "drm_internal.h"
30 #include "drm_crtc_internal.h"
31 
32 #define to_drm_minor(d) dev_get_drvdata(d)
33 #define to_drm_connector(d) dev_get_drvdata(d)
34 
35 /**
36  * DOC: overview
37  *
38  * DRM provides very little additional support to drivers for sysfs
39  * interactions, beyond just all the standard stuff. Drivers who want to expose
40  * additional sysfs properties and property groups can attach them at either
41  * &drm_device.dev or &drm_connector.kdev.
42  *
43  * Registration is automatically handled when calling drm_dev_register(), or
44  * drm_connector_register() in case of hot-plugged connectors. Unregistration is
45  * also automatically handled by drm_dev_unregister() and
46  * drm_connector_unregister().
47  */
48 
49 static struct device_type drm_sysfs_device_minor = {
50 	.name = "drm_minor"
51 };
52 
53 static struct device_type drm_sysfs_device_connector = {
54 	.name = "drm_connector",
55 };
56 
57 struct class *drm_class;
58 
drm_devnode(struct device * dev,umode_t * mode)59 static char *drm_devnode(struct device *dev, umode_t *mode)
60 {
61 	return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
62 }
63 
64 static CLASS_ATTR_STRING(version, S_IRUGO, "drm 1.1.0 20060810");
65 
66 /**
67  * drm_sysfs_init - initialize sysfs helpers
68  *
69  * This is used to create the DRM class, which is the implicit parent of any
70  * other top-level DRM sysfs objects.
71  *
72  * You must call drm_sysfs_destroy() to release the allocated resources.
73  *
74  * Return: 0 on success, negative error code on failure.
75  */
drm_sysfs_init(void)76 int drm_sysfs_init(void)
77 {
78 	int err;
79 
80 	drm_class = class_create(THIS_MODULE, "drm");
81 	if (IS_ERR(drm_class))
82 		return PTR_ERR(drm_class);
83 
84 	err = class_create_file(drm_class, &class_attr_version.attr);
85 	if (err) {
86 		class_destroy(drm_class);
87 		drm_class = NULL;
88 		return err;
89 	}
90 
91 	drm_class->devnode = drm_devnode;
92 	return 0;
93 }
94 
95 /**
96  * drm_sysfs_destroy - destroys DRM class
97  *
98  * Destroy the DRM device class.
99  */
drm_sysfs_destroy(void)100 void drm_sysfs_destroy(void)
101 {
102 	if (IS_ERR_OR_NULL(drm_class))
103 		return;
104 	class_remove_file(drm_class, &class_attr_version.attr);
105 	class_destroy(drm_class);
106 	drm_class = NULL;
107 }
108 
drm_sysfs_release(struct device * dev)109 static void drm_sysfs_release(struct device *dev)
110 {
111 	kfree(dev);
112 }
113 
114 /*
115  * Connector properties
116  */
status_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)117 static ssize_t status_store(struct device *device,
118 			   struct device_attribute *attr,
119 			   const char *buf, size_t count)
120 {
121 	struct drm_connector *connector = to_drm_connector(device);
122 	struct drm_device *dev = connector->dev;
123 	enum drm_connector_force old_force;
124 	int ret;
125 
126 	ret = mutex_lock_interruptible(&dev->mode_config.mutex);
127 	if (ret)
128 		return ret;
129 
130 	old_force = connector->force;
131 
132 	if (sysfs_streq(buf, "detect"))
133 		connector->force = 0;
134 	else if (sysfs_streq(buf, "on"))
135 		connector->force = DRM_FORCE_ON;
136 	else if (sysfs_streq(buf, "on-digital"))
137 		connector->force = DRM_FORCE_ON_DIGITAL;
138 	else if (sysfs_streq(buf, "off"))
139 		connector->force = DRM_FORCE_OFF;
140 	else
141 		ret = -EINVAL;
142 
143 	if (old_force != connector->force || !connector->force) {
144 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force updated from %d to %d or reprobing\n",
145 			      connector->base.id,
146 			      connector->name,
147 			      old_force, connector->force);
148 
149 		connector->funcs->fill_modes(connector,
150 					     dev->mode_config.max_width,
151 					     dev->mode_config.max_height);
152 	}
153 
154 	mutex_unlock(&dev->mode_config.mutex);
155 
156 	return ret ? ret : count;
157 }
158 
status_show(struct device * device,struct device_attribute * attr,char * buf)159 static ssize_t status_show(struct device *device,
160 			   struct device_attribute *attr,
161 			   char *buf)
162 {
163 	struct drm_connector *connector = to_drm_connector(device);
164 	enum drm_connector_status status;
165 
166 	status = READ_ONCE(connector->status);
167 
168 	return snprintf(buf, PAGE_SIZE, "%s\n",
169 			drm_get_connector_status_name(status));
170 }
171 
dpms_show(struct device * device,struct device_attribute * attr,char * buf)172 static ssize_t dpms_show(struct device *device,
173 			   struct device_attribute *attr,
174 			   char *buf)
175 {
176 	struct drm_connector *connector = to_drm_connector(device);
177 	int dpms;
178 
179 	dpms = READ_ONCE(connector->dpms);
180 
181 	return snprintf(buf, PAGE_SIZE, "%s\n",
182 			drm_get_dpms_name(dpms));
183 }
184 
enabled_show(struct device * device,struct device_attribute * attr,char * buf)185 static ssize_t enabled_show(struct device *device,
186 			    struct device_attribute *attr,
187 			   char *buf)
188 {
189 	struct drm_connector *connector = to_drm_connector(device);
190 	bool enabled;
191 
192 	enabled = READ_ONCE(connector->encoder);
193 
194 	return snprintf(buf, PAGE_SIZE, enabled ? "enabled\n" : "disabled\n");
195 }
196 
edid_show(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)197 static ssize_t edid_show(struct file *filp, struct kobject *kobj,
198 			 struct bin_attribute *attr, char *buf, loff_t off,
199 			 size_t count)
200 {
201 	struct device *connector_dev = kobj_to_dev(kobj);
202 	struct drm_connector *connector = to_drm_connector(connector_dev);
203 	unsigned char *edid;
204 	size_t size;
205 	ssize_t ret = 0;
206 
207 	mutex_lock(&connector->dev->mode_config.mutex);
208 	if (!connector->edid_blob_ptr)
209 		goto unlock;
210 
211 	edid = connector->edid_blob_ptr->data;
212 	size = connector->edid_blob_ptr->length;
213 	if (!edid)
214 		goto unlock;
215 
216 	if (off >= size)
217 		goto unlock;
218 
219 	if (off + count > size)
220 		count = size - off;
221 	memcpy(buf, edid + off, count);
222 
223 	ret = count;
224 unlock:
225 	mutex_unlock(&connector->dev->mode_config.mutex);
226 
227 	return ret;
228 }
229 
modes_show(struct device * device,struct device_attribute * attr,char * buf)230 static ssize_t modes_show(struct device *device,
231 			   struct device_attribute *attr,
232 			   char *buf)
233 {
234 	struct drm_connector *connector = to_drm_connector(device);
235 	struct drm_display_mode *mode;
236 	int written = 0;
237 
238 	mutex_lock(&connector->dev->mode_config.mutex);
239 	list_for_each_entry(mode, &connector->modes, head) {
240 		written += scnprintf(buf + written, PAGE_SIZE - written, "%s\n",
241 				    mode->name);
242 	}
243 	mutex_unlock(&connector->dev->mode_config.mutex);
244 
245 	return written;
246 }
247 
248 static DEVICE_ATTR_RW(status);
249 static DEVICE_ATTR_RO(enabled);
250 static DEVICE_ATTR_RO(dpms);
251 static DEVICE_ATTR_RO(modes);
252 
253 static struct attribute *connector_dev_attrs[] = {
254 	&dev_attr_status.attr,
255 	&dev_attr_enabled.attr,
256 	&dev_attr_dpms.attr,
257 	&dev_attr_modes.attr,
258 	NULL
259 };
260 
261 static struct bin_attribute edid_attr = {
262 	.attr.name = "edid",
263 	.attr.mode = 0444,
264 	.size = 0,
265 	.read = edid_show,
266 };
267 
268 static struct bin_attribute *connector_bin_attrs[] = {
269 	&edid_attr,
270 	NULL
271 };
272 
273 static const struct attribute_group connector_dev_group = {
274 	.attrs = connector_dev_attrs,
275 	.bin_attrs = connector_bin_attrs,
276 };
277 
278 static const struct attribute_group *connector_dev_groups[] = {
279 	&connector_dev_group,
280 	NULL
281 };
282 
drm_sysfs_connector_add(struct drm_connector * connector)283 int drm_sysfs_connector_add(struct drm_connector *connector)
284 {
285 	struct drm_device *dev = connector->dev;
286 	struct device *kdev;
287 	int r;
288 
289 	if (connector->kdev)
290 		return 0;
291 
292 	kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
293 	if (!kdev)
294 		return -ENOMEM;
295 
296 	device_initialize(kdev);
297 	kdev->class = drm_class;
298 	kdev->type = &drm_sysfs_device_connector;
299 	kdev->parent = dev->primary->kdev;
300 	kdev->groups = connector_dev_groups;
301 	kdev->release = drm_sysfs_release;
302 	dev_set_drvdata(kdev, connector);
303 
304 	r = dev_set_name(kdev, "card%d-%s", dev->primary->index, connector->name);
305 	if (r)
306 		goto err_free;
307 
308 	DRM_DEBUG("adding \"%s\" to sysfs\n",
309 		  connector->name);
310 
311 	r = device_add(kdev);
312 	if (r) {
313 		drm_err(dev, "failed to register connector device: %d\n", r);
314 		goto err_free;
315 	}
316 
317 	connector->kdev = kdev;
318 
319 	if (connector->ddc)
320 		return sysfs_create_link(&connector->kdev->kobj,
321 				 &connector->ddc->dev.kobj, "ddc");
322 	return 0;
323 
324 err_free:
325 	put_device(kdev);
326 	return r;
327 }
328 
drm_sysfs_connector_remove(struct drm_connector * connector)329 void drm_sysfs_connector_remove(struct drm_connector *connector)
330 {
331 	if (!connector->kdev)
332 		return;
333 
334 	if (connector->ddc)
335 		sysfs_remove_link(&connector->kdev->kobj, "ddc");
336 
337 	DRM_DEBUG("removing \"%s\" from sysfs\n",
338 		  connector->name);
339 
340 	device_unregister(connector->kdev);
341 	connector->kdev = NULL;
342 }
343 
drm_sysfs_lease_event(struct drm_device * dev)344 void drm_sysfs_lease_event(struct drm_device *dev)
345 {
346 	char *event_string = "LEASE=1";
347 	char *envp[] = { event_string, NULL };
348 
349 	DRM_DEBUG("generating lease event\n");
350 
351 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
352 }
353 
354 /**
355  * drm_sysfs_hotplug_event - generate a DRM uevent
356  * @dev: DRM device
357  *
358  * Send a uevent for the DRM device specified by @dev.  Currently we only
359  * set HOTPLUG=1 in the uevent environment, but this could be expanded to
360  * deal with other types of events.
361  *
362  * Any new uapi should be using the drm_sysfs_connector_status_event()
363  * for uevents on connector status change.
364  */
drm_sysfs_hotplug_event(struct drm_device * dev)365 void drm_sysfs_hotplug_event(struct drm_device *dev)
366 {
367 	char *event_string = "HOTPLUG=1";
368 	char *envp[] = { event_string, NULL };
369 
370 	DRM_DEBUG("generating hotplug event\n");
371 
372 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
373 }
374 EXPORT_SYMBOL(drm_sysfs_hotplug_event);
375 
376 /**
377  * drm_sysfs_connector_status_event - generate a DRM uevent for connector
378  * property status change
379  * @connector: connector on which property status changed
380  * @property: connector property whose status changed.
381  *
382  * Send a uevent for the DRM device specified by @dev.  Currently we
383  * set HOTPLUG=1 and connector id along with the attached property id
384  * related to the status change.
385  */
drm_sysfs_connector_status_event(struct drm_connector * connector,struct drm_property * property)386 void drm_sysfs_connector_status_event(struct drm_connector *connector,
387 				      struct drm_property *property)
388 {
389 	struct drm_device *dev = connector->dev;
390 	char hotplug_str[] = "HOTPLUG=1", conn_id[21], prop_id[21];
391 	char *envp[4] = { hotplug_str, conn_id, prop_id, NULL };
392 
393 	WARN_ON(!drm_mode_obj_find_prop_id(&connector->base,
394 					   property->base.id));
395 
396 	snprintf(conn_id, ARRAY_SIZE(conn_id),
397 		 "CONNECTOR=%u", connector->base.id);
398 	snprintf(prop_id, ARRAY_SIZE(prop_id),
399 		 "PROPERTY=%u", property->base.id);
400 
401 	DRM_DEBUG("generating connector status event\n");
402 
403 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
404 }
405 EXPORT_SYMBOL(drm_sysfs_connector_status_event);
406 
drm_sysfs_minor_alloc(struct drm_minor * minor)407 struct device *drm_sysfs_minor_alloc(struct drm_minor *minor)
408 {
409 	const char *minor_str;
410 	struct device *kdev;
411 	int r;
412 
413 	if (minor->type == DRM_MINOR_RENDER)
414 		minor_str = "renderD%d";
415 	else
416 		minor_str = "card%d";
417 
418 	kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
419 	if (!kdev)
420 		return ERR_PTR(-ENOMEM);
421 
422 	device_initialize(kdev);
423 	kdev->devt = MKDEV(DRM_MAJOR, minor->index);
424 	kdev->class = drm_class;
425 	kdev->type = &drm_sysfs_device_minor;
426 	kdev->parent = minor->dev->dev;
427 	kdev->release = drm_sysfs_release;
428 	dev_set_drvdata(kdev, minor);
429 
430 	r = dev_set_name(kdev, minor_str, minor->index);
431 	if (r < 0)
432 		goto err_free;
433 
434 	return kdev;
435 
436 err_free:
437 	put_device(kdev);
438 	return ERR_PTR(r);
439 }
440 
441 /**
442  * drm_class_device_register - register new device with the DRM sysfs class
443  * @dev: device to register
444  *
445  * Registers a new &struct device within the DRM sysfs class. Essentially only
446  * used by ttm to have a place for its global settings. Drivers should never use
447  * this.
448  */
drm_class_device_register(struct device * dev)449 int drm_class_device_register(struct device *dev)
450 {
451 	if (!drm_class || IS_ERR(drm_class))
452 		return -ENOENT;
453 
454 	dev->class = drm_class;
455 	return device_register(dev);
456 }
457 EXPORT_SYMBOL_GPL(drm_class_device_register);
458 
459 /**
460  * drm_class_device_unregister - unregister device with the DRM sysfs class
461  * @dev: device to unregister
462  *
463  * Unregisters a &struct device from the DRM sysfs class. Essentially only used
464  * by ttm to have a place for its global settings. Drivers should never use
465  * this.
466  */
drm_class_device_unregister(struct device * dev)467 void drm_class_device_unregister(struct device *dev)
468 {
469 	return device_unregister(dev);
470 }
471 EXPORT_SYMBOL_GPL(drm_class_device_unregister);
472