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>
22 #include <drm/drm_core.h>
24 #include "drm_internal.h"
26 #define to_drm_minor(d) dev_get_drvdata(d)
27 #define to_drm_connector(d) dev_get_drvdata(d)
29 static struct device_type drm_sysfs_device_minor
= {
34 * __drm_class_suspend - internal DRM class suspend routine
35 * @dev: Linux device to suspend
36 * @state: power state to enter
38 * Just figures out what the actual struct drm_device associated with
39 * @dev is and calls its suspend hook, if present.
41 static int __drm_class_suspend(struct device
*dev
, pm_message_t state
)
43 if (dev
->type
== &drm_sysfs_device_minor
) {
44 struct drm_minor
*drm_minor
= to_drm_minor(dev
);
45 struct drm_device
*drm_dev
= drm_minor
->dev
;
47 if (drm_minor
->type
== DRM_MINOR_LEGACY
&&
48 !drm_core_check_feature(drm_dev
, DRIVER_MODESET
) &&
49 drm_dev
->driver
->suspend
)
50 return drm_dev
->driver
->suspend(drm_dev
, state
);
56 * drm_class_suspend - internal DRM class suspend hook. Simply calls
57 * __drm_class_suspend() with the correct pm state.
58 * @dev: Linux device to suspend
60 static int drm_class_suspend(struct device
*dev
)
62 return __drm_class_suspend(dev
, PMSG_SUSPEND
);
66 * drm_class_freeze - internal DRM class freeze hook. Simply calls
67 * __drm_class_suspend() with the correct pm state.
68 * @dev: Linux device to freeze
70 static int drm_class_freeze(struct device
*dev
)
72 return __drm_class_suspend(dev
, PMSG_FREEZE
);
76 * drm_class_resume - DRM class resume hook
77 * @dev: Linux device to resume
79 * Just figures out what the actual struct drm_device associated with
80 * @dev is and calls its resume hook, if present.
82 static int drm_class_resume(struct device
*dev
)
84 if (dev
->type
== &drm_sysfs_device_minor
) {
85 struct drm_minor
*drm_minor
= to_drm_minor(dev
);
86 struct drm_device
*drm_dev
= drm_minor
->dev
;
88 if (drm_minor
->type
== DRM_MINOR_LEGACY
&&
89 !drm_core_check_feature(drm_dev
, DRIVER_MODESET
) &&
90 drm_dev
->driver
->resume
)
91 return drm_dev
->driver
->resume(drm_dev
);
96 static const struct dev_pm_ops drm_class_dev_pm_ops
= {
97 .suspend
= drm_class_suspend
,
98 .resume
= drm_class_resume
,
99 .freeze
= drm_class_freeze
,
102 static char *drm_devnode(struct device
*dev
, umode_t
*mode
)
104 return kasprintf(GFP_KERNEL
, "dri/%s", dev_name(dev
));
107 static CLASS_ATTR_STRING(version
, S_IRUGO
,
109 __stringify(CORE_MAJOR
) "."
110 __stringify(CORE_MINOR
) "."
111 __stringify(CORE_PATCHLEVEL
) " "
115 * drm_sysfs_create - create a struct drm_sysfs_class structure
116 * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
117 * @name: pointer to a string for the name of this class.
119 * This is used to create DRM class pointer that can then be used
120 * in calls to drm_sysfs_device_add().
122 * Note, the pointer created here is to be destroyed when finished by making a
123 * call to drm_sysfs_destroy().
125 struct class *drm_sysfs_create(struct module
*owner
, char *name
)
130 class = class_create(owner
, name
);
132 err
= PTR_ERR(class);
136 class->pm
= &drm_class_dev_pm_ops
;
138 err
= class_create_file(class, &class_attr_version
.attr
);
142 class->devnode
= drm_devnode
;
147 class_destroy(class);
153 * drm_sysfs_destroy - destroys DRM class
155 * Destroy the DRM device class.
157 void drm_sysfs_destroy(void)
159 if ((drm_class
== NULL
) || (IS_ERR(drm_class
)))
161 class_remove_file(drm_class
, &class_attr_version
.attr
);
162 class_destroy(drm_class
);
167 * Connector properties
169 static ssize_t
status_store(struct device
*device
,
170 struct device_attribute
*attr
,
171 const char *buf
, size_t count
)
173 struct drm_connector
*connector
= to_drm_connector(device
);
174 struct drm_device
*dev
= connector
->dev
;
175 enum drm_connector_status old_status
;
178 ret
= mutex_lock_interruptible(&dev
->mode_config
.mutex
);
182 old_status
= connector
->status
;
184 if (sysfs_streq(buf
, "detect")) {
185 connector
->force
= 0;
186 connector
->status
= connector
->funcs
->detect(connector
, true);
187 } else if (sysfs_streq(buf
, "on")) {
188 connector
->force
= DRM_FORCE_ON
;
189 } else if (sysfs_streq(buf
, "on-digital")) {
190 connector
->force
= DRM_FORCE_ON_DIGITAL
;
191 } else if (sysfs_streq(buf
, "off")) {
192 connector
->force
= DRM_FORCE_OFF
;
196 if (ret
== 0 && connector
->force
) {
197 if (connector
->force
== DRM_FORCE_ON
||
198 connector
->force
== DRM_FORCE_ON_DIGITAL
)
199 connector
->status
= connector_status_connected
;
201 connector
->status
= connector_status_disconnected
;
202 if (connector
->funcs
->force
)
203 connector
->funcs
->force(connector
);
206 if (old_status
!= connector
->status
) {
207 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
210 old_status
, connector
->status
);
212 dev
->mode_config
.delayed_event
= true;
213 if (dev
->mode_config
.poll_enabled
)
214 schedule_delayed_work(&dev
->mode_config
.output_poll_work
,
218 mutex_unlock(&dev
->mode_config
.mutex
);
220 return ret
? ret
: count
;
223 static ssize_t
status_show(struct device
*device
,
224 struct device_attribute
*attr
,
227 struct drm_connector
*connector
= to_drm_connector(device
);
229 return snprintf(buf
, PAGE_SIZE
, "%s\n",
230 drm_get_connector_status_name(connector
->status
));
233 static ssize_t
dpms_show(struct device
*device
,
234 struct device_attribute
*attr
,
237 struct drm_connector
*connector
= to_drm_connector(device
);
240 dpms
= READ_ONCE(connector
->dpms
);
242 return snprintf(buf
, PAGE_SIZE
, "%s\n",
243 drm_get_dpms_name(dpms
));
246 static ssize_t
enabled_show(struct device
*device
,
247 struct device_attribute
*attr
,
250 struct drm_connector
*connector
= to_drm_connector(device
);
252 return snprintf(buf
, PAGE_SIZE
, "%s\n", connector
->encoder
? "enabled" :
256 static ssize_t
edid_show(struct file
*filp
, struct kobject
*kobj
,
257 struct bin_attribute
*attr
, char *buf
, loff_t off
,
260 struct device
*connector_dev
= container_of(kobj
, struct device
, kobj
);
261 struct drm_connector
*connector
= to_drm_connector(connector_dev
);
265 if (!connector
->edid_blob_ptr
)
268 edid
= connector
->edid_blob_ptr
->data
;
269 size
= connector
->edid_blob_ptr
->length
;
276 if (off
+ count
> size
)
278 memcpy(buf
, edid
+ off
, count
);
283 static ssize_t
modes_show(struct device
*device
,
284 struct device_attribute
*attr
,
287 struct drm_connector
*connector
= to_drm_connector(device
);
288 struct drm_display_mode
*mode
;
291 list_for_each_entry(mode
, &connector
->modes
, head
) {
292 written
+= snprintf(buf
+ written
, PAGE_SIZE
- written
, "%s\n",
299 static ssize_t
subconnector_show(struct device
*device
,
300 struct device_attribute
*attr
,
303 struct drm_connector
*connector
= to_drm_connector(device
);
304 struct drm_device
*dev
= connector
->dev
;
305 struct drm_property
*prop
= NULL
;
306 uint64_t subconnector
;
310 switch (connector
->connector_type
) {
311 case DRM_MODE_CONNECTOR_DVII
:
312 prop
= dev
->mode_config
.dvi_i_subconnector_property
;
314 case DRM_MODE_CONNECTOR_Composite
:
315 case DRM_MODE_CONNECTOR_SVIDEO
:
316 case DRM_MODE_CONNECTOR_Component
:
317 case DRM_MODE_CONNECTOR_TV
:
318 prop
= dev
->mode_config
.tv_subconnector_property
;
322 DRM_ERROR("Wrong connector type for this property\n");
327 DRM_ERROR("Unable to find subconnector property\n");
331 ret
= drm_object_property_get_value(&connector
->base
, prop
, &subconnector
);
335 return snprintf(buf
, PAGE_SIZE
, "%s", is_tv
?
336 drm_get_tv_subconnector_name((int)subconnector
) :
337 drm_get_dvi_i_subconnector_name((int)subconnector
));
340 static ssize_t
select_subconnector_show(struct device
*device
,
341 struct device_attribute
*attr
,
344 struct drm_connector
*connector
= to_drm_connector(device
);
345 struct drm_device
*dev
= connector
->dev
;
346 struct drm_property
*prop
= NULL
;
347 uint64_t subconnector
;
351 switch (connector
->connector_type
) {
352 case DRM_MODE_CONNECTOR_DVII
:
353 prop
= dev
->mode_config
.dvi_i_select_subconnector_property
;
355 case DRM_MODE_CONNECTOR_Composite
:
356 case DRM_MODE_CONNECTOR_SVIDEO
:
357 case DRM_MODE_CONNECTOR_Component
:
358 case DRM_MODE_CONNECTOR_TV
:
359 prop
= dev
->mode_config
.tv_select_subconnector_property
;
363 DRM_ERROR("Wrong connector type for this property\n");
368 DRM_ERROR("Unable to find select subconnector property\n");
372 ret
= drm_object_property_get_value(&connector
->base
, prop
, &subconnector
);
376 return snprintf(buf
, PAGE_SIZE
, "%s", is_tv
?
377 drm_get_tv_select_name((int)subconnector
) :
378 drm_get_dvi_i_select_name((int)subconnector
));
381 static DEVICE_ATTR_RW(status
);
382 static DEVICE_ATTR_RO(enabled
);
383 static DEVICE_ATTR_RO(dpms
);
384 static DEVICE_ATTR_RO(modes
);
386 static struct attribute
*connector_dev_attrs
[] = {
387 &dev_attr_status
.attr
,
388 &dev_attr_enabled
.attr
,
390 &dev_attr_modes
.attr
,
394 /* These attributes are for both DVI-I connectors and all types of tv-out. */
395 static DEVICE_ATTR_RO(subconnector
);
396 static DEVICE_ATTR_RO(select_subconnector
);
398 static struct attribute
*connector_opt_dev_attrs
[] = {
399 &dev_attr_subconnector
.attr
,
400 &dev_attr_select_subconnector
.attr
,
404 static umode_t
connector_opt_dev_is_visible(struct kobject
*kobj
,
405 struct attribute
*attr
, int idx
)
407 struct device
*dev
= kobj_to_dev(kobj
);
408 struct drm_connector
*connector
= to_drm_connector(dev
);
411 * In the long run it maybe a good idea to make one set of
412 * optionals per connector type.
414 switch (connector
->connector_type
) {
415 case DRM_MODE_CONNECTOR_DVII
:
416 case DRM_MODE_CONNECTOR_Composite
:
417 case DRM_MODE_CONNECTOR_SVIDEO
:
418 case DRM_MODE_CONNECTOR_Component
:
419 case DRM_MODE_CONNECTOR_TV
:
426 static struct bin_attribute edid_attr
= {
433 static struct bin_attribute
*connector_bin_attrs
[] = {
438 static const struct attribute_group connector_dev_group
= {
439 .attrs
= connector_dev_attrs
,
440 .bin_attrs
= connector_bin_attrs
,
443 static const struct attribute_group connector_opt_dev_group
= {
444 .attrs
= connector_opt_dev_attrs
,
445 .is_visible
= connector_opt_dev_is_visible
,
448 static const struct attribute_group
*connector_dev_groups
[] = {
449 &connector_dev_group
,
450 &connector_opt_dev_group
,
455 * drm_sysfs_connector_add - add a connector to sysfs
456 * @connector: connector to add
458 * Create a connector device in sysfs, along with its associated connector
459 * properties (so far, connection status, dpms, mode list & edid) and
460 * generate a hotplug event so userspace knows there's a new connector
463 int drm_sysfs_connector_add(struct drm_connector
*connector
)
465 struct drm_device
*dev
= connector
->dev
;
471 device_create_with_groups(drm_class
, dev
->primary
->kdev
, 0,
472 connector
, connector_dev_groups
,
473 "card%d-%s", dev
->primary
->index
,
475 DRM_DEBUG("adding \"%s\" to sysfs\n",
478 if (IS_ERR(connector
->kdev
)) {
479 DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector
->kdev
));
480 return PTR_ERR(connector
->kdev
);
483 /* Let userspace know we have a new connector */
484 drm_sysfs_hotplug_event(dev
);
490 * drm_sysfs_connector_remove - remove an connector device from sysfs
491 * @connector: connector to remove
493 * Remove @connector and its associated attributes from sysfs. Note that
494 * the device model core will take care of sending the "remove" uevent
495 * at this time, so we don't need to do it.
498 * This routine should only be called if the connector was previously
499 * successfully registered. If @connector hasn't been registered yet,
500 * you'll likely see a panic somewhere deep in sysfs code when called.
502 void drm_sysfs_connector_remove(struct drm_connector
*connector
)
504 if (!connector
->kdev
)
506 DRM_DEBUG("removing \"%s\" from sysfs\n",
509 device_unregister(connector
->kdev
);
510 connector
->kdev
= NULL
;
514 * drm_sysfs_hotplug_event - generate a DRM uevent
517 * Send a uevent for the DRM device specified by @dev. Currently we only
518 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
519 * deal with other types of events.
521 void drm_sysfs_hotplug_event(struct drm_device
*dev
)
523 char *event_string
= "HOTPLUG=1";
524 char *envp
[] = { event_string
, NULL
};
526 DRM_DEBUG("generating hotplug event\n");
528 kobject_uevent_env(&dev
->primary
->kdev
->kobj
, KOBJ_CHANGE
, envp
);
530 EXPORT_SYMBOL(drm_sysfs_hotplug_event
);
532 static void drm_sysfs_release(struct device
*dev
)
538 * drm_sysfs_minor_alloc() - Allocate sysfs device for given minor
539 * @minor: minor to allocate sysfs device for
541 * This allocates a new sysfs device for @minor and returns it. The device is
542 * not registered nor linked. The caller has to use device_add() and
543 * device_del() to register and unregister it.
545 * Note that dev_get_drvdata() on the new device will return the minor.
546 * However, the device does not hold a ref-count to the minor nor to the
547 * underlying drm_device. This is unproblematic as long as you access the
548 * private data only in sysfs callbacks. device_del() disables those
549 * synchronously, so they cannot be called after you cleanup a minor.
551 struct device
*drm_sysfs_minor_alloc(struct drm_minor
*minor
)
553 const char *minor_str
;
557 if (minor
->type
== DRM_MINOR_CONTROL
)
558 minor_str
= "controlD%d";
559 else if (minor
->type
== DRM_MINOR_RENDER
)
560 minor_str
= "renderD%d";
562 minor_str
= "card%d";
564 kdev
= kzalloc(sizeof(*kdev
), GFP_KERNEL
);
566 return ERR_PTR(-ENOMEM
);
568 device_initialize(kdev
);
569 kdev
->devt
= MKDEV(DRM_MAJOR
, minor
->index
);
570 kdev
->class = drm_class
;
571 kdev
->type
= &drm_sysfs_device_minor
;
572 kdev
->parent
= minor
->dev
->dev
;
573 kdev
->release
= drm_sysfs_release
;
574 dev_set_drvdata(kdev
, minor
);
576 r
= dev_set_name(kdev
, minor_str
, minor
->index
);
588 * drm_class_device_register - Register a struct device in the drm class.
590 * @dev: pointer to struct device to register.
592 * @dev should have all relevant members pre-filled with the exception
593 * of the class member. In particular, the device_type member must
597 int drm_class_device_register(struct device
*dev
)
599 if (!drm_class
|| IS_ERR(drm_class
))
602 dev
->class = drm_class
;
603 return device_register(dev
);
605 EXPORT_SYMBOL_GPL(drm_class_device_register
);
607 void drm_class_device_unregister(struct device
*dev
)
609 return device_unregister(dev
);
611 EXPORT_SYMBOL_GPL(drm_class_device_unregister
);