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>
22 #define to_drm_minor(d) container_of(d, struct drm_minor, kdev)
23 #define to_drm_connector(d) container_of(d, struct drm_connector, kdev)
26 * drm_sysfs_suspend - DRM class suspend hook
27 * @dev: Linux device to suspend
28 * @state: power state to enter
30 * Just figures out what the actual struct drm_device associated with
31 * @dev is and calls its suspend hook, if present.
33 static int drm_sysfs_suspend(struct device
*dev
, pm_message_t state
)
35 struct drm_minor
*drm_minor
= to_drm_minor(dev
);
36 struct drm_device
*drm_dev
= drm_minor
->dev
;
38 if (drm_minor
->type
== DRM_MINOR_LEGACY
&&
39 !drm_core_check_feature(drm_dev
, DRIVER_MODESET
) &&
40 drm_dev
->driver
->suspend
)
41 return drm_dev
->driver
->suspend(drm_dev
, state
);
47 * drm_sysfs_resume - DRM class resume hook
48 * @dev: Linux device to resume
50 * Just figures out what the actual struct drm_device associated with
51 * @dev is and calls its resume hook, if present.
53 static int drm_sysfs_resume(struct device
*dev
)
55 struct drm_minor
*drm_minor
= to_drm_minor(dev
);
56 struct drm_device
*drm_dev
= drm_minor
->dev
;
58 if (drm_minor
->type
== DRM_MINOR_LEGACY
&&
59 !drm_core_check_feature(drm_dev
, DRIVER_MODESET
) &&
60 drm_dev
->driver
->resume
)
61 return drm_dev
->driver
->resume(drm_dev
);
66 /* Display the version of drm_core. This doesn't work right in current design */
67 static ssize_t
version_show(struct class *dev
, char *buf
)
69 return sprintf(buf
, "%s %d.%d.%d %s\n", CORE_NAME
, CORE_MAJOR
,
70 CORE_MINOR
, CORE_PATCHLEVEL
, CORE_DATE
);
73 static char *drm_nodename(struct device
*dev
)
75 return kasprintf(GFP_KERNEL
, "dri/%s", dev_name(dev
));
78 static CLASS_ATTR(version
, S_IRUGO
, version_show
, NULL
);
81 * drm_sysfs_create - create a struct drm_sysfs_class structure
82 * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
83 * @name: pointer to a string for the name of this class.
85 * This is used to create DRM class pointer that can then be used
86 * in calls to drm_sysfs_device_add().
88 * Note, the pointer created here is to be destroyed when finished by making a
89 * call to drm_sysfs_destroy().
91 struct class *drm_sysfs_create(struct module
*owner
, char *name
)
96 class = class_create(owner
, name
);
102 class->suspend
= drm_sysfs_suspend
;
103 class->resume
= drm_sysfs_resume
;
105 err
= class_create_file(class, &class_attr_version
);
109 class->nodename
= drm_nodename
;
114 class_destroy(class);
120 * drm_sysfs_destroy - destroys DRM class
122 * Destroy the DRM device class.
124 void drm_sysfs_destroy(void)
126 if ((drm_class
== NULL
) || (IS_ERR(drm_class
)))
128 class_remove_file(drm_class
, &class_attr_version
);
129 class_destroy(drm_class
);
133 * drm_sysfs_device_release - do nothing
136 * Normally, this would free the DRM device associated with @dev, along
137 * with cleaning up any other stuff. But we do that in the DRM core, so
138 * this function can just return and hope that the core does its job.
140 static void drm_sysfs_device_release(struct device
*dev
)
142 memset(dev
, 0, sizeof(struct device
));
147 * Connector properties
149 static ssize_t
status_show(struct device
*device
,
150 struct device_attribute
*attr
,
153 struct drm_connector
*connector
= to_drm_connector(device
);
154 enum drm_connector_status status
;
156 status
= connector
->funcs
->detect(connector
);
157 return snprintf(buf
, PAGE_SIZE
, "%s\n",
158 drm_get_connector_status_name(status
));
161 static ssize_t
dpms_show(struct device
*device
,
162 struct device_attribute
*attr
,
165 struct drm_connector
*connector
= to_drm_connector(device
);
166 struct drm_device
*dev
= connector
->dev
;
167 uint64_t dpms_status
;
170 ret
= drm_connector_property_get_value(connector
,
171 dev
->mode_config
.dpms_property
,
176 return snprintf(buf
, PAGE_SIZE
, "%s\n",
177 drm_get_dpms_name((int)dpms_status
));
180 static ssize_t
enabled_show(struct device
*device
,
181 struct device_attribute
*attr
,
184 struct drm_connector
*connector
= to_drm_connector(device
);
186 return snprintf(buf
, PAGE_SIZE
, "%s\n", connector
->encoder
? "enabled" :
190 static ssize_t
edid_show(struct kobject
*kobj
, struct bin_attribute
*attr
,
191 char *buf
, loff_t off
, size_t count
)
193 struct device
*connector_dev
= container_of(kobj
, struct device
, kobj
);
194 struct drm_connector
*connector
= to_drm_connector(connector_dev
);
198 if (!connector
->edid_blob_ptr
)
201 edid
= connector
->edid_blob_ptr
->data
;
202 size
= connector
->edid_blob_ptr
->length
;
209 if (off
+ count
> size
)
211 memcpy(buf
, edid
+ off
, count
);
216 static ssize_t
modes_show(struct device
*device
,
217 struct device_attribute
*attr
,
220 struct drm_connector
*connector
= to_drm_connector(device
);
221 struct drm_display_mode
*mode
;
224 list_for_each_entry(mode
, &connector
->modes
, head
) {
225 written
+= snprintf(buf
+ written
, PAGE_SIZE
- written
, "%s\n",
232 static ssize_t
subconnector_show(struct device
*device
,
233 struct device_attribute
*attr
,
236 struct drm_connector
*connector
= to_drm_connector(device
);
237 struct drm_device
*dev
= connector
->dev
;
238 struct drm_property
*prop
= NULL
;
239 uint64_t subconnector
;
243 switch (connector
->connector_type
) {
244 case DRM_MODE_CONNECTOR_DVII
:
245 prop
= dev
->mode_config
.dvi_i_subconnector_property
;
247 case DRM_MODE_CONNECTOR_Composite
:
248 case DRM_MODE_CONNECTOR_SVIDEO
:
249 case DRM_MODE_CONNECTOR_Component
:
250 prop
= dev
->mode_config
.tv_subconnector_property
;
254 DRM_ERROR("Wrong connector type for this property\n");
259 DRM_ERROR("Unable to find subconnector property\n");
263 ret
= drm_connector_property_get_value(connector
, prop
, &subconnector
);
267 return snprintf(buf
, PAGE_SIZE
, "%s", is_tv
?
268 drm_get_tv_subconnector_name((int)subconnector
) :
269 drm_get_dvi_i_subconnector_name((int)subconnector
));
272 static ssize_t
select_subconnector_show(struct device
*device
,
273 struct device_attribute
*attr
,
276 struct drm_connector
*connector
= to_drm_connector(device
);
277 struct drm_device
*dev
= connector
->dev
;
278 struct drm_property
*prop
= NULL
;
279 uint64_t subconnector
;
283 switch (connector
->connector_type
) {
284 case DRM_MODE_CONNECTOR_DVII
:
285 prop
= dev
->mode_config
.dvi_i_select_subconnector_property
;
287 case DRM_MODE_CONNECTOR_Composite
:
288 case DRM_MODE_CONNECTOR_SVIDEO
:
289 case DRM_MODE_CONNECTOR_Component
:
290 prop
= dev
->mode_config
.tv_select_subconnector_property
;
294 DRM_ERROR("Wrong connector type for this property\n");
299 DRM_ERROR("Unable to find select subconnector property\n");
303 ret
= drm_connector_property_get_value(connector
, prop
, &subconnector
);
307 return snprintf(buf
, PAGE_SIZE
, "%s", is_tv
?
308 drm_get_tv_select_name((int)subconnector
) :
309 drm_get_dvi_i_select_name((int)subconnector
));
312 static struct device_attribute connector_attrs
[] = {
319 /* These attributes are for both DVI-I connectors and all types of tv-out. */
320 static struct device_attribute connector_attrs_opt1
[] = {
321 __ATTR_RO(subconnector
),
322 __ATTR_RO(select_subconnector
),
325 static struct bin_attribute edid_attr
= {
333 * drm_sysfs_connector_add - add an connector to sysfs
334 * @connector: connector to add
336 * Create an connector device in sysfs, along with its associated connector
337 * properties (so far, connection status, dpms, mode list & edid) and
338 * generate a hotplug event so userspace knows there's a new connector
342 * This routine should only be called *once* for each DRM minor registered.
343 * A second call for an already registered device will trigger the BUG_ON
346 int drm_sysfs_connector_add(struct drm_connector
*connector
)
348 struct drm_device
*dev
= connector
->dev
;
351 /* We shouldn't get called more than once for the same connector */
352 BUG_ON(device_is_registered(&connector
->kdev
));
354 connector
->kdev
.parent
= &dev
->primary
->kdev
;
355 connector
->kdev
.class = drm_class
;
356 connector
->kdev
.release
= drm_sysfs_device_release
;
358 DRM_DEBUG("adding \"%s\" to sysfs\n",
359 drm_get_connector_name(connector
));
361 dev_set_name(&connector
->kdev
, "card%d-%s",
362 dev
->primary
->index
, drm_get_connector_name(connector
));
363 ret
= device_register(&connector
->kdev
);
366 DRM_ERROR("failed to register connector device: %d\n", ret
);
370 /* Standard attributes */
372 for (i
= 0; i
< ARRAY_SIZE(connector_attrs
); i
++) {
373 ret
= device_create_file(&connector
->kdev
, &connector_attrs
[i
]);
378 /* Optional attributes */
380 * In the long run it maybe a good idea to make one set of
381 * optionals per connector type.
383 switch (connector
->connector_type
) {
384 case DRM_MODE_CONNECTOR_DVII
:
385 case DRM_MODE_CONNECTOR_Composite
:
386 case DRM_MODE_CONNECTOR_SVIDEO
:
387 case DRM_MODE_CONNECTOR_Component
:
388 for (i
= 0; i
< ARRAY_SIZE(connector_attrs_opt1
); i
++) {
389 ret
= device_create_file(&connector
->kdev
, &connector_attrs_opt1
[i
]);
398 ret
= sysfs_create_bin_file(&connector
->kdev
.kobj
, &edid_attr
);
402 /* Let userspace know we have a new connector */
403 drm_sysfs_hotplug_event(dev
);
409 for (j
= 0; j
< i
; j
++)
410 device_remove_file(&connector
->kdev
,
411 &connector_attrs
[i
]);
412 device_unregister(&connector
->kdev
);
417 EXPORT_SYMBOL(drm_sysfs_connector_add
);
420 * drm_sysfs_connector_remove - remove an connector device from sysfs
421 * @connector: connector to remove
423 * Remove @connector and its associated attributes from sysfs. Note that
424 * the device model core will take care of sending the "remove" uevent
425 * at this time, so we don't need to do it.
428 * This routine should only be called if the connector was previously
429 * successfully registered. If @connector hasn't been registered yet,
430 * you'll likely see a panic somewhere deep in sysfs code when called.
432 void drm_sysfs_connector_remove(struct drm_connector
*connector
)
436 DRM_DEBUG("removing \"%s\" from sysfs\n",
437 drm_get_connector_name(connector
));
439 for (i
= 0; i
< ARRAY_SIZE(connector_attrs
); i
++)
440 device_remove_file(&connector
->kdev
, &connector_attrs
[i
]);
441 sysfs_remove_bin_file(&connector
->kdev
.kobj
, &edid_attr
);
442 device_unregister(&connector
->kdev
);
444 EXPORT_SYMBOL(drm_sysfs_connector_remove
);
447 * drm_sysfs_hotplug_event - generate a DRM uevent
450 * Send a uevent for the DRM device specified by @dev. Currently we only
451 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
452 * deal with other types of events.
454 void drm_sysfs_hotplug_event(struct drm_device
*dev
)
456 char *event_string
= "HOTPLUG=1";
457 char *envp
[] = { event_string
, NULL
};
459 DRM_DEBUG("generating hotplug event\n");
461 kobject_uevent_env(&dev
->primary
->kdev
.kobj
, KOBJ_CHANGE
, envp
);
463 EXPORT_SYMBOL(drm_sysfs_hotplug_event
);
466 * drm_sysfs_device_add - adds a class device to sysfs for a character driver
467 * @dev: DRM device to be added
468 * @head: DRM head in question
470 * Add a DRM device to the DRM's device model class. We use @dev's PCI device
471 * as the parent for the Linux device, and make sure it has a file containing
472 * the driver we're using (for userspace compatibility).
474 int drm_sysfs_device_add(struct drm_minor
*minor
)
479 minor
->kdev
.parent
= &minor
->dev
->pdev
->dev
;
480 minor
->kdev
.class = drm_class
;
481 minor
->kdev
.release
= drm_sysfs_device_release
;
482 minor
->kdev
.devt
= minor
->device
;
483 if (minor
->type
== DRM_MINOR_CONTROL
)
484 minor_str
= "controlD%d";
485 else if (minor
->type
== DRM_MINOR_RENDER
)
486 minor_str
= "renderD%d";
488 minor_str
= "card%d";
490 dev_set_name(&minor
->kdev
, minor_str
, minor
->index
);
492 err
= device_register(&minor
->kdev
);
494 DRM_ERROR("device add failed: %d\n", err
);
505 * drm_sysfs_device_remove - remove DRM device
506 * @dev: DRM device to remove
508 * This call unregisters and cleans up a class device that was created with a
509 * call to drm_sysfs_device_add()
511 void drm_sysfs_device_remove(struct drm_minor
*minor
)
513 device_unregister(&minor
->kdev
);