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/err.h>
19 #include "drm_sysfs.h"
23 #define to_drm_minor(d) container_of(d, struct drm_minor, kdev)
24 #define to_drm_connector(d) container_of(d, struct drm_connector, kdev)
26 static struct device_type drm_sysfs_device_minor
= {
31 * drm_class_suspend - DRM class suspend hook
32 * @dev: Linux device to suspend
33 * @state: power state to enter
35 * Just figures out what the actual struct drm_device associated with
36 * @dev is and calls its suspend hook, if present.
38 static int drm_class_suspend(struct device
*dev
, pm_message_t state
)
40 if (dev
->type
== &drm_sysfs_device_minor
) {
41 struct drm_minor
*drm_minor
= to_drm_minor(dev
);
42 struct drm_device
*drm_dev
= drm_minor
->dev
;
44 if (drm_minor
->type
== DRM_MINOR_LEGACY
&&
45 !drm_core_check_feature(drm_dev
, DRIVER_MODESET
) &&
46 drm_dev
->driver
->suspend
)
47 return drm_dev
->driver
->suspend(drm_dev
, state
);
53 * drm_class_resume - DRM class resume hook
54 * @dev: Linux device to resume
56 * Just figures out what the actual struct drm_device associated with
57 * @dev is and calls its resume hook, if present.
59 static int drm_class_resume(struct device
*dev
)
61 if (dev
->type
== &drm_sysfs_device_minor
) {
62 struct drm_minor
*drm_minor
= to_drm_minor(dev
);
63 struct drm_device
*drm_dev
= drm_minor
->dev
;
65 if (drm_minor
->type
== DRM_MINOR_LEGACY
&&
66 !drm_core_check_feature(drm_dev
, DRIVER_MODESET
) &&
67 drm_dev
->driver
->resume
)
68 return drm_dev
->driver
->resume(drm_dev
);
73 static char *drm_devnode(struct device
*dev
, mode_t
*mode
)
75 return kasprintf(GFP_KERNEL
, "dri/%s", dev_name(dev
));
78 static CLASS_ATTR_STRING(version
, S_IRUGO
,
80 __stringify(CORE_MAJOR
) "."
81 __stringify(CORE_MINOR
) "."
82 __stringify(CORE_PATCHLEVEL
) " "
86 * drm_sysfs_create - create a struct drm_sysfs_class structure
87 * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
88 * @name: pointer to a string for the name of this class.
90 * This is used to create DRM class pointer that can then be used
91 * in calls to drm_sysfs_device_add().
93 * Note, the pointer created here is to be destroyed when finished by making a
94 * call to drm_sysfs_destroy().
96 struct class *drm_sysfs_create(struct module
*owner
, char *name
)
101 class = class_create(owner
, name
);
103 err
= PTR_ERR(class);
107 class->suspend
= drm_class_suspend
;
108 class->resume
= drm_class_resume
;
110 err
= class_create_file(class, &class_attr_version
.attr
);
114 class->devnode
= drm_devnode
;
119 class_destroy(class);
125 * drm_sysfs_destroy - destroys DRM class
127 * Destroy the DRM device class.
129 void drm_sysfs_destroy(void)
131 if ((drm_class
== NULL
) || (IS_ERR(drm_class
)))
133 class_remove_file(drm_class
, &class_attr_version
.attr
);
134 class_destroy(drm_class
);
138 * drm_sysfs_device_release - do nothing
141 * Normally, this would free the DRM device associated with @dev, along
142 * with cleaning up any other stuff. But we do that in the DRM core, so
143 * this function can just return and hope that the core does its job.
145 static void drm_sysfs_device_release(struct device
*dev
)
147 memset(dev
, 0, sizeof(struct device
));
152 * Connector properties
154 static ssize_t
status_show(struct device
*device
,
155 struct device_attribute
*attr
,
158 struct drm_connector
*connector
= to_drm_connector(device
);
159 enum drm_connector_status status
;
161 status
= connector
->funcs
->detect(connector
);
162 return snprintf(buf
, PAGE_SIZE
, "%s\n",
163 drm_get_connector_status_name(status
));
166 static ssize_t
dpms_show(struct device
*device
,
167 struct device_attribute
*attr
,
170 struct drm_connector
*connector
= to_drm_connector(device
);
171 struct drm_device
*dev
= connector
->dev
;
172 uint64_t dpms_status
;
175 ret
= drm_connector_property_get_value(connector
,
176 dev
->mode_config
.dpms_property
,
181 return snprintf(buf
, PAGE_SIZE
, "%s\n",
182 drm_get_dpms_name((int)dpms_status
));
185 static ssize_t
enabled_show(struct device
*device
,
186 struct device_attribute
*attr
,
189 struct drm_connector
*connector
= to_drm_connector(device
);
191 return snprintf(buf
, PAGE_SIZE
, "%s\n", connector
->encoder
? "enabled" :
195 static ssize_t
edid_show(struct kobject
*kobj
, struct bin_attribute
*attr
,
196 char *buf
, loff_t off
, size_t count
)
198 struct device
*connector_dev
= container_of(kobj
, struct device
, kobj
);
199 struct drm_connector
*connector
= to_drm_connector(connector_dev
);
203 if (!connector
->edid_blob_ptr
)
206 edid
= connector
->edid_blob_ptr
->data
;
207 size
= connector
->edid_blob_ptr
->length
;
214 if (off
+ count
> size
)
216 memcpy(buf
, edid
+ off
, count
);
221 static ssize_t
modes_show(struct device
*device
,
222 struct device_attribute
*attr
,
225 struct drm_connector
*connector
= to_drm_connector(device
);
226 struct drm_display_mode
*mode
;
229 list_for_each_entry(mode
, &connector
->modes
, head
) {
230 written
+= snprintf(buf
+ written
, PAGE_SIZE
- written
, "%s\n",
237 static ssize_t
subconnector_show(struct device
*device
,
238 struct device_attribute
*attr
,
241 struct drm_connector
*connector
= to_drm_connector(device
);
242 struct drm_device
*dev
= connector
->dev
;
243 struct drm_property
*prop
= NULL
;
244 uint64_t subconnector
;
248 switch (connector
->connector_type
) {
249 case DRM_MODE_CONNECTOR_DVII
:
250 prop
= dev
->mode_config
.dvi_i_subconnector_property
;
252 case DRM_MODE_CONNECTOR_Composite
:
253 case DRM_MODE_CONNECTOR_SVIDEO
:
254 case DRM_MODE_CONNECTOR_Component
:
255 case DRM_MODE_CONNECTOR_TV
:
256 prop
= dev
->mode_config
.tv_subconnector_property
;
260 DRM_ERROR("Wrong connector type for this property\n");
265 DRM_ERROR("Unable to find subconnector property\n");
269 ret
= drm_connector_property_get_value(connector
, prop
, &subconnector
);
273 return snprintf(buf
, PAGE_SIZE
, "%s", is_tv
?
274 drm_get_tv_subconnector_name((int)subconnector
) :
275 drm_get_dvi_i_subconnector_name((int)subconnector
));
278 static ssize_t
select_subconnector_show(struct device
*device
,
279 struct device_attribute
*attr
,
282 struct drm_connector
*connector
= to_drm_connector(device
);
283 struct drm_device
*dev
= connector
->dev
;
284 struct drm_property
*prop
= NULL
;
285 uint64_t subconnector
;
289 switch (connector
->connector_type
) {
290 case DRM_MODE_CONNECTOR_DVII
:
291 prop
= dev
->mode_config
.dvi_i_select_subconnector_property
;
293 case DRM_MODE_CONNECTOR_Composite
:
294 case DRM_MODE_CONNECTOR_SVIDEO
:
295 case DRM_MODE_CONNECTOR_Component
:
296 case DRM_MODE_CONNECTOR_TV
:
297 prop
= dev
->mode_config
.tv_select_subconnector_property
;
301 DRM_ERROR("Wrong connector type for this property\n");
306 DRM_ERROR("Unable to find select subconnector property\n");
310 ret
= drm_connector_property_get_value(connector
, prop
, &subconnector
);
314 return snprintf(buf
, PAGE_SIZE
, "%s", is_tv
?
315 drm_get_tv_select_name((int)subconnector
) :
316 drm_get_dvi_i_select_name((int)subconnector
));
319 static struct device_attribute connector_attrs
[] = {
326 /* These attributes are for both DVI-I connectors and all types of tv-out. */
327 static struct device_attribute connector_attrs_opt1
[] = {
328 __ATTR_RO(subconnector
),
329 __ATTR_RO(select_subconnector
),
332 static struct bin_attribute edid_attr
= {
340 * drm_sysfs_connector_add - add an connector to sysfs
341 * @connector: connector to add
343 * Create an connector device in sysfs, along with its associated connector
344 * properties (so far, connection status, dpms, mode list & edid) and
345 * generate a hotplug event so userspace knows there's a new connector
349 * This routine should only be called *once* for each DRM minor registered.
350 * A second call for an already registered device will trigger the BUG_ON
353 int drm_sysfs_connector_add(struct drm_connector
*connector
)
355 struct drm_device
*dev
= connector
->dev
;
358 /* We shouldn't get called more than once for the same connector */
359 BUG_ON(device_is_registered(&connector
->kdev
));
361 connector
->kdev
.parent
= &dev
->primary
->kdev
;
362 connector
->kdev
.class = drm_class
;
363 connector
->kdev
.release
= drm_sysfs_device_release
;
365 DRM_DEBUG("adding \"%s\" to sysfs\n",
366 drm_get_connector_name(connector
));
368 dev_set_name(&connector
->kdev
, "card%d-%s",
369 dev
->primary
->index
, drm_get_connector_name(connector
));
370 ret
= device_register(&connector
->kdev
);
373 DRM_ERROR("failed to register connector device: %d\n", ret
);
377 /* Standard attributes */
379 for (i
= 0; i
< ARRAY_SIZE(connector_attrs
); i
++) {
380 ret
= device_create_file(&connector
->kdev
, &connector_attrs
[i
]);
385 /* Optional attributes */
387 * In the long run it maybe a good idea to make one set of
388 * optionals per connector type.
390 switch (connector
->connector_type
) {
391 case DRM_MODE_CONNECTOR_DVII
:
392 case DRM_MODE_CONNECTOR_Composite
:
393 case DRM_MODE_CONNECTOR_SVIDEO
:
394 case DRM_MODE_CONNECTOR_Component
:
395 case DRM_MODE_CONNECTOR_TV
:
396 for (i
= 0; i
< ARRAY_SIZE(connector_attrs_opt1
); i
++) {
397 ret
= device_create_file(&connector
->kdev
, &connector_attrs_opt1
[i
]);
406 ret
= sysfs_create_bin_file(&connector
->kdev
.kobj
, &edid_attr
);
410 /* Let userspace know we have a new connector */
411 drm_sysfs_hotplug_event(dev
);
417 for (j
= 0; j
< i
; j
++)
418 device_remove_file(&connector
->kdev
,
419 &connector_attrs
[i
]);
420 device_unregister(&connector
->kdev
);
425 EXPORT_SYMBOL(drm_sysfs_connector_add
);
428 * drm_sysfs_connector_remove - remove an connector device from sysfs
429 * @connector: connector to remove
431 * Remove @connector and its associated attributes from sysfs. Note that
432 * the device model core will take care of sending the "remove" uevent
433 * at this time, so we don't need to do it.
436 * This routine should only be called if the connector was previously
437 * successfully registered. If @connector hasn't been registered yet,
438 * you'll likely see a panic somewhere deep in sysfs code when called.
440 void drm_sysfs_connector_remove(struct drm_connector
*connector
)
444 DRM_DEBUG("removing \"%s\" from sysfs\n",
445 drm_get_connector_name(connector
));
447 for (i
= 0; i
< ARRAY_SIZE(connector_attrs
); i
++)
448 device_remove_file(&connector
->kdev
, &connector_attrs
[i
]);
449 sysfs_remove_bin_file(&connector
->kdev
.kobj
, &edid_attr
);
450 device_unregister(&connector
->kdev
);
452 EXPORT_SYMBOL(drm_sysfs_connector_remove
);
455 * drm_sysfs_hotplug_event - generate a DRM uevent
458 * Send a uevent for the DRM device specified by @dev. Currently we only
459 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
460 * deal with other types of events.
462 void drm_sysfs_hotplug_event(struct drm_device
*dev
)
464 char *event_string
= "HOTPLUG=1";
465 char *envp
[] = { event_string
, NULL
};
467 DRM_DEBUG("generating hotplug event\n");
469 kobject_uevent_env(&dev
->primary
->kdev
.kobj
, KOBJ_CHANGE
, envp
);
471 EXPORT_SYMBOL(drm_sysfs_hotplug_event
);
474 * drm_sysfs_device_add - adds a class device to sysfs for a character driver
475 * @dev: DRM device to be added
476 * @head: DRM head in question
478 * Add a DRM device to the DRM's device model class. We use @dev's PCI device
479 * as the parent for the Linux device, and make sure it has a file containing
480 * the driver we're using (for userspace compatibility).
482 int drm_sysfs_device_add(struct drm_minor
*minor
)
487 minor
->kdev
.parent
= &minor
->dev
->pdev
->dev
;
488 minor
->kdev
.class = drm_class
;
489 minor
->kdev
.release
= drm_sysfs_device_release
;
490 minor
->kdev
.devt
= minor
->device
;
491 minor
->kdev
.type
= &drm_sysfs_device_minor
;
492 if (minor
->type
== DRM_MINOR_CONTROL
)
493 minor_str
= "controlD%d";
494 else if (minor
->type
== DRM_MINOR_RENDER
)
495 minor_str
= "renderD%d";
497 minor_str
= "card%d";
499 dev_set_name(&minor
->kdev
, minor_str
, minor
->index
);
501 err
= device_register(&minor
->kdev
);
503 DRM_ERROR("device add failed: %d\n", err
);
514 * drm_sysfs_device_remove - remove DRM device
515 * @dev: DRM device to remove
517 * This call unregisters and cleans up a class device that was created with a
518 * call to drm_sysfs_device_add()
520 void drm_sysfs_device_remove(struct drm_minor
*minor
)
522 device_unregister(&minor
->kdev
);
527 * drm_class_device_register - Register a struct device in the drm class.
529 * @dev: pointer to struct device to register.
531 * @dev should have all relevant members pre-filled with the exception
532 * of the class member. In particular, the device_type member must
536 int drm_class_device_register(struct device
*dev
)
538 dev
->class = drm_class
;
539 return device_register(dev
);
541 EXPORT_SYMBOL_GPL(drm_class_device_register
);
543 void drm_class_device_unregister(struct device
*dev
)
545 return device_unregister(dev
);
547 EXPORT_SYMBOL_GPL(drm_class_device_unregister
);