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>
25 #define to_drm_minor(d) container_of(d, struct drm_minor, kdev)
26 #define to_drm_connector(d) container_of(d, struct drm_connector, kdev)
28 static struct device_type drm_sysfs_device_minor
= {
33 * __drm_class_suspend - internal DRM class suspend routine
34 * @dev: Linux device to suspend
35 * @state: power state to enter
37 * Just figures out what the actual struct drm_device associated with
38 * @dev is and calls its suspend hook, if present.
40 static int __drm_class_suspend(struct device
*dev
, pm_message_t state
)
42 if (dev
->type
== &drm_sysfs_device_minor
) {
43 struct drm_minor
*drm_minor
= to_drm_minor(dev
);
44 struct drm_device
*drm_dev
= drm_minor
->dev
;
46 if (drm_minor
->type
== DRM_MINOR_LEGACY
&&
47 !drm_core_check_feature(drm_dev
, DRIVER_MODESET
) &&
48 drm_dev
->driver
->suspend
)
49 return drm_dev
->driver
->suspend(drm_dev
, state
);
55 * drm_class_suspend - internal DRM class suspend hook. Simply calls
56 * __drm_class_suspend() with the correct pm state.
57 * @dev: Linux device to suspend
59 static int drm_class_suspend(struct device
*dev
)
61 return __drm_class_suspend(dev
, PMSG_SUSPEND
);
65 * drm_class_freeze - internal DRM class freeze hook. Simply calls
66 * __drm_class_suspend() with the correct pm state.
67 * @dev: Linux device to freeze
69 static int drm_class_freeze(struct device
*dev
)
71 return __drm_class_suspend(dev
, PMSG_FREEZE
);
75 * drm_class_resume - DRM class resume hook
76 * @dev: Linux device to resume
78 * Just figures out what the actual struct drm_device associated with
79 * @dev is and calls its resume hook, if present.
81 static int drm_class_resume(struct device
*dev
)
83 if (dev
->type
== &drm_sysfs_device_minor
) {
84 struct drm_minor
*drm_minor
= to_drm_minor(dev
);
85 struct drm_device
*drm_dev
= drm_minor
->dev
;
87 if (drm_minor
->type
== DRM_MINOR_LEGACY
&&
88 !drm_core_check_feature(drm_dev
, DRIVER_MODESET
) &&
89 drm_dev
->driver
->resume
)
90 return drm_dev
->driver
->resume(drm_dev
);
95 static const struct dev_pm_ops drm_class_dev_pm_ops
= {
96 .suspend
= drm_class_suspend
,
97 .resume
= drm_class_resume
,
98 .freeze
= drm_class_freeze
,
101 static char *drm_devnode(struct device
*dev
, umode_t
*mode
)
103 return kasprintf(GFP_KERNEL
, "dri/%s", dev_name(dev
));
106 static CLASS_ATTR_STRING(version
, S_IRUGO
,
108 __stringify(CORE_MAJOR
) "."
109 __stringify(CORE_MINOR
) "."
110 __stringify(CORE_PATCHLEVEL
) " "
114 * drm_sysfs_create - create a struct drm_sysfs_class structure
115 * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
116 * @name: pointer to a string for the name of this class.
118 * This is used to create DRM class pointer that can then be used
119 * in calls to drm_sysfs_device_add().
121 * Note, the pointer created here is to be destroyed when finished by making a
122 * call to drm_sysfs_destroy().
124 struct class *drm_sysfs_create(struct module
*owner
, char *name
)
129 class = class_create(owner
, name
);
131 err
= PTR_ERR(class);
135 class->pm
= &drm_class_dev_pm_ops
;
137 err
= class_create_file(class, &class_attr_version
.attr
);
141 class->devnode
= drm_devnode
;
146 class_destroy(class);
152 * drm_sysfs_destroy - destroys DRM class
154 * Destroy the DRM device class.
156 void drm_sysfs_destroy(void)
158 if ((drm_class
== NULL
) || (IS_ERR(drm_class
)))
160 class_remove_file(drm_class
, &class_attr_version
.attr
);
161 class_destroy(drm_class
);
166 * drm_sysfs_device_release - do nothing
169 * Normally, this would free the DRM device associated with @dev, along
170 * with cleaning up any other stuff. But we do that in the DRM core, so
171 * this function can just return and hope that the core does its job.
173 static void drm_sysfs_device_release(struct device
*dev
)
175 memset(dev
, 0, sizeof(struct device
));
180 * Connector properties
182 static ssize_t
status_show(struct device
*device
,
183 struct device_attribute
*attr
,
186 struct drm_connector
*connector
= to_drm_connector(device
);
187 enum drm_connector_status status
;
190 ret
= mutex_lock_interruptible(&connector
->dev
->mode_config
.mutex
);
194 status
= connector
->funcs
->detect(connector
, true);
195 mutex_unlock(&connector
->dev
->mode_config
.mutex
);
197 return snprintf(buf
, PAGE_SIZE
, "%s\n",
198 drm_get_connector_status_name(status
));
201 static ssize_t
dpms_show(struct device
*device
,
202 struct device_attribute
*attr
,
205 struct drm_connector
*connector
= to_drm_connector(device
);
206 struct drm_device
*dev
= connector
->dev
;
207 uint64_t dpms_status
;
210 ret
= drm_object_property_get_value(&connector
->base
,
211 dev
->mode_config
.dpms_property
,
216 return snprintf(buf
, PAGE_SIZE
, "%s\n",
217 drm_get_dpms_name((int)dpms_status
));
220 static ssize_t
enabled_show(struct device
*device
,
221 struct device_attribute
*attr
,
224 struct drm_connector
*connector
= to_drm_connector(device
);
226 return snprintf(buf
, PAGE_SIZE
, "%s\n", connector
->encoder
? "enabled" :
230 static ssize_t
edid_show(struct file
*filp
, struct kobject
*kobj
,
231 struct bin_attribute
*attr
, char *buf
, loff_t off
,
234 struct device
*connector_dev
= container_of(kobj
, struct device
, kobj
);
235 struct drm_connector
*connector
= to_drm_connector(connector_dev
);
239 if (!connector
->edid_blob_ptr
)
242 edid
= connector
->edid_blob_ptr
->data
;
243 size
= connector
->edid_blob_ptr
->length
;
250 if (off
+ count
> size
)
252 memcpy(buf
, edid
+ off
, count
);
257 static ssize_t
modes_show(struct device
*device
,
258 struct device_attribute
*attr
,
261 struct drm_connector
*connector
= to_drm_connector(device
);
262 struct drm_display_mode
*mode
;
265 list_for_each_entry(mode
, &connector
->modes
, head
) {
266 written
+= snprintf(buf
+ written
, PAGE_SIZE
- written
, "%s\n",
273 static ssize_t
subconnector_show(struct device
*device
,
274 struct device_attribute
*attr
,
277 struct drm_connector
*connector
= to_drm_connector(device
);
278 struct drm_device
*dev
= connector
->dev
;
279 struct drm_property
*prop
= NULL
;
280 uint64_t subconnector
;
284 switch (connector
->connector_type
) {
285 case DRM_MODE_CONNECTOR_DVII
:
286 prop
= dev
->mode_config
.dvi_i_subconnector_property
;
288 case DRM_MODE_CONNECTOR_Composite
:
289 case DRM_MODE_CONNECTOR_SVIDEO
:
290 case DRM_MODE_CONNECTOR_Component
:
291 case DRM_MODE_CONNECTOR_TV
:
292 prop
= dev
->mode_config
.tv_subconnector_property
;
296 DRM_ERROR("Wrong connector type for this property\n");
301 DRM_ERROR("Unable to find subconnector property\n");
305 ret
= drm_object_property_get_value(&connector
->base
, prop
, &subconnector
);
309 return snprintf(buf
, PAGE_SIZE
, "%s", is_tv
?
310 drm_get_tv_subconnector_name((int)subconnector
) :
311 drm_get_dvi_i_subconnector_name((int)subconnector
));
314 static ssize_t
select_subconnector_show(struct device
*device
,
315 struct device_attribute
*attr
,
318 struct drm_connector
*connector
= to_drm_connector(device
);
319 struct drm_device
*dev
= connector
->dev
;
320 struct drm_property
*prop
= NULL
;
321 uint64_t subconnector
;
325 switch (connector
->connector_type
) {
326 case DRM_MODE_CONNECTOR_DVII
:
327 prop
= dev
->mode_config
.dvi_i_select_subconnector_property
;
329 case DRM_MODE_CONNECTOR_Composite
:
330 case DRM_MODE_CONNECTOR_SVIDEO
:
331 case DRM_MODE_CONNECTOR_Component
:
332 case DRM_MODE_CONNECTOR_TV
:
333 prop
= dev
->mode_config
.tv_select_subconnector_property
;
337 DRM_ERROR("Wrong connector type for this property\n");
342 DRM_ERROR("Unable to find select subconnector property\n");
346 ret
= drm_object_property_get_value(&connector
->base
, prop
, &subconnector
);
350 return snprintf(buf
, PAGE_SIZE
, "%s", is_tv
?
351 drm_get_tv_select_name((int)subconnector
) :
352 drm_get_dvi_i_select_name((int)subconnector
));
355 static struct device_attribute connector_attrs
[] = {
362 /* These attributes are for both DVI-I connectors and all types of tv-out. */
363 static struct device_attribute connector_attrs_opt1
[] = {
364 __ATTR_RO(subconnector
),
365 __ATTR_RO(select_subconnector
),
368 static struct bin_attribute edid_attr
= {
376 * drm_sysfs_connector_add - add a connector to sysfs
377 * @connector: connector to add
379 * Create a connector device in sysfs, along with its associated connector
380 * properties (so far, connection status, dpms, mode list & edid) and
381 * generate a hotplug event so userspace knows there's a new connector
385 * This routine should only be called *once* for each registered connector.
386 * A second call for an already registered connector will trigger the BUG_ON
389 int drm_sysfs_connector_add(struct drm_connector
*connector
)
391 struct drm_device
*dev
= connector
->dev
;
397 /* We shouldn't get called more than once for the same connector */
398 BUG_ON(device_is_registered(&connector
->kdev
));
400 connector
->kdev
.parent
= &dev
->primary
->kdev
;
401 connector
->kdev
.class = drm_class
;
402 connector
->kdev
.release
= drm_sysfs_device_release
;
404 DRM_DEBUG("adding \"%s\" to sysfs\n",
405 drm_get_connector_name(connector
));
407 dev_set_name(&connector
->kdev
, "card%d-%s",
408 dev
->primary
->index
, drm_get_connector_name(connector
));
409 ret
= device_register(&connector
->kdev
);
412 DRM_ERROR("failed to register connector device: %d\n", ret
);
416 /* Standard attributes */
418 for (attr_cnt
= 0; attr_cnt
< ARRAY_SIZE(connector_attrs
); attr_cnt
++) {
419 ret
= device_create_file(&connector
->kdev
, &connector_attrs
[attr_cnt
]);
424 /* Optional attributes */
426 * In the long run it maybe a good idea to make one set of
427 * optionals per connector type.
429 switch (connector
->connector_type
) {
430 case DRM_MODE_CONNECTOR_DVII
:
431 case DRM_MODE_CONNECTOR_Composite
:
432 case DRM_MODE_CONNECTOR_SVIDEO
:
433 case DRM_MODE_CONNECTOR_Component
:
434 case DRM_MODE_CONNECTOR_TV
:
435 for (opt_cnt
= 0; opt_cnt
< ARRAY_SIZE(connector_attrs_opt1
); opt_cnt
++) {
436 ret
= device_create_file(&connector
->kdev
, &connector_attrs_opt1
[opt_cnt
]);
445 ret
= sysfs_create_bin_file(&connector
->kdev
.kobj
, &edid_attr
);
449 /* Let userspace know we have a new connector */
450 drm_sysfs_hotplug_event(dev
);
455 for (i
= 0; i
< opt_cnt
; i
++)
456 device_remove_file(&connector
->kdev
, &connector_attrs_opt1
[i
]);
457 for (i
= 0; i
< attr_cnt
; i
++)
458 device_remove_file(&connector
->kdev
, &connector_attrs
[i
]);
459 device_unregister(&connector
->kdev
);
464 EXPORT_SYMBOL(drm_sysfs_connector_add
);
467 * drm_sysfs_connector_remove - remove an connector device from sysfs
468 * @connector: connector to remove
470 * Remove @connector and its associated attributes from sysfs. Note that
471 * the device model core will take care of sending the "remove" uevent
472 * at this time, so we don't need to do it.
475 * This routine should only be called if the connector was previously
476 * successfully registered. If @connector hasn't been registered yet,
477 * you'll likely see a panic somewhere deep in sysfs code when called.
479 void drm_sysfs_connector_remove(struct drm_connector
*connector
)
483 if (!connector
->kdev
.parent
)
485 DRM_DEBUG("removing \"%s\" from sysfs\n",
486 drm_get_connector_name(connector
));
488 for (i
= 0; i
< ARRAY_SIZE(connector_attrs
); i
++)
489 device_remove_file(&connector
->kdev
, &connector_attrs
[i
]);
490 sysfs_remove_bin_file(&connector
->kdev
.kobj
, &edid_attr
);
491 device_unregister(&connector
->kdev
);
492 connector
->kdev
.parent
= NULL
;
494 EXPORT_SYMBOL(drm_sysfs_connector_remove
);
497 * drm_sysfs_hotplug_event - generate a DRM uevent
500 * Send a uevent for the DRM device specified by @dev. Currently we only
501 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
502 * deal with other types of events.
504 void drm_sysfs_hotplug_event(struct drm_device
*dev
)
506 char *event_string
= "HOTPLUG=1";
507 char *envp
[] = { event_string
, NULL
};
509 DRM_DEBUG("generating hotplug event\n");
511 kobject_uevent_env(&dev
->primary
->kdev
.kobj
, KOBJ_CHANGE
, envp
);
513 EXPORT_SYMBOL(drm_sysfs_hotplug_event
);
516 * drm_sysfs_device_add - adds a class device to sysfs for a character driver
517 * @dev: DRM device to be added
518 * @head: DRM head in question
520 * Add a DRM device to the DRM's device model class. We use @dev's PCI device
521 * as the parent for the Linux device, and make sure it has a file containing
522 * the driver we're using (for userspace compatibility).
524 int drm_sysfs_device_add(struct drm_minor
*minor
)
529 minor
->kdev
.parent
= minor
->dev
->dev
;
531 minor
->kdev
.class = drm_class
;
532 minor
->kdev
.release
= drm_sysfs_device_release
;
533 minor
->kdev
.devt
= minor
->device
;
534 minor
->kdev
.type
= &drm_sysfs_device_minor
;
535 if (minor
->type
== DRM_MINOR_CONTROL
)
536 minor_str
= "controlD%d";
537 else if (minor
->type
== DRM_MINOR_RENDER
)
538 minor_str
= "renderD%d";
540 minor_str
= "card%d";
542 dev_set_name(&minor
->kdev
, minor_str
, minor
->index
);
544 err
= device_register(&minor
->kdev
);
546 DRM_ERROR("device add failed: %d\n", err
);
557 * drm_sysfs_device_remove - remove DRM device
558 * @dev: DRM device to remove
560 * This call unregisters and cleans up a class device that was created with a
561 * call to drm_sysfs_device_add()
563 void drm_sysfs_device_remove(struct drm_minor
*minor
)
565 if (minor
->kdev
.parent
)
566 device_unregister(&minor
->kdev
);
567 minor
->kdev
.parent
= NULL
;
572 * drm_class_device_register - Register a struct device in the drm class.
574 * @dev: pointer to struct device to register.
576 * @dev should have all relevant members pre-filled with the exception
577 * of the class member. In particular, the device_type member must
581 int drm_class_device_register(struct device
*dev
)
583 if (!drm_class
|| IS_ERR(drm_class
))
586 dev
->class = drm_class
;
587 return device_register(dev
);
589 EXPORT_SYMBOL_GPL(drm_class_device_register
);
591 void drm_class_device_unregister(struct device
*dev
)
593 return device_unregister(dev
);
595 EXPORT_SYMBOL_GPL(drm_class_device_unregister
);