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 /* Display the version of drm_core. This doesn't work right in current design */
74 static ssize_t version_show(struct class *dev, struct class_attribute *attr,
77 return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR,
78 CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
81 static char *drm_devnode(struct device *dev, mode_t *mode)
83 return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
86 static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
89 * drm_sysfs_create - create a struct drm_sysfs_class structure
90 * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
91 * @name: pointer to a string for the name of this class.
93 * This is used to create DRM class pointer that can then be used
94 * in calls to drm_sysfs_device_add().
96 * Note, the pointer created here is to be destroyed when finished by making a
97 * call to drm_sysfs_destroy().
99 struct class *drm_sysfs_create(struct module *owner, char *name)
104 class = class_create(owner, name);
106 err = PTR_ERR(class);
110 class->suspend = drm_class_suspend;
111 class->resume = drm_class_resume;
113 err = class_create_file(class, &class_attr_version);
117 class->devnode = drm_devnode;
122 class_destroy(class);
128 * drm_sysfs_destroy - destroys DRM class
130 * Destroy the DRM device class.
132 void drm_sysfs_destroy(void)
134 if ((drm_class == NULL) || (IS_ERR(drm_class)))
136 class_remove_file(drm_class, &class_attr_version);
137 class_destroy(drm_class);
141 * drm_sysfs_device_release - do nothing
144 * Normally, this would free the DRM device associated with @dev, along
145 * with cleaning up any other stuff. But we do that in the DRM core, so
146 * this function can just return and hope that the core does its job.
148 static void drm_sysfs_device_release(struct device *dev)
150 memset(dev, 0, sizeof(struct device));
155 * Connector properties
157 static ssize_t status_show(struct device *device,
158 struct device_attribute *attr,
161 struct drm_connector *connector = to_drm_connector(device);
162 enum drm_connector_status status;
164 status = connector->funcs->detect(connector);
165 return snprintf(buf, PAGE_SIZE, "%s\n",
166 drm_get_connector_status_name(status));
169 static ssize_t dpms_show(struct device *device,
170 struct device_attribute *attr,
173 struct drm_connector *connector = to_drm_connector(device);
174 struct drm_device *dev = connector->dev;
175 uint64_t dpms_status;
178 ret = drm_connector_property_get_value(connector,
179 dev->mode_config.dpms_property,
184 return snprintf(buf, PAGE_SIZE, "%s\n",
185 drm_get_dpms_name((int)dpms_status));
188 static ssize_t enabled_show(struct device *device,
189 struct device_attribute *attr,
192 struct drm_connector *connector = to_drm_connector(device);
194 return snprintf(buf, PAGE_SIZE, "%s\n", connector->encoder ? "enabled" :
198 static ssize_t edid_show(struct kobject *kobj, struct bin_attribute *attr,
199 char *buf, loff_t off, size_t count)
201 struct device *connector_dev = container_of(kobj, struct device, kobj);
202 struct drm_connector *connector = to_drm_connector(connector_dev);
206 if (!connector->edid_blob_ptr)
209 edid = connector->edid_blob_ptr->data;
210 size = connector->edid_blob_ptr->length;
217 if (off + count > size)
219 memcpy(buf, edid + off, count);
224 static ssize_t modes_show(struct device *device,
225 struct device_attribute *attr,
228 struct drm_connector *connector = to_drm_connector(device);
229 struct drm_display_mode *mode;
232 list_for_each_entry(mode, &connector->modes, head) {
233 written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
240 static ssize_t subconnector_show(struct device *device,
241 struct device_attribute *attr,
244 struct drm_connector *connector = to_drm_connector(device);
245 struct drm_device *dev = connector->dev;
246 struct drm_property *prop = NULL;
247 uint64_t subconnector;
251 switch (connector->connector_type) {
252 case DRM_MODE_CONNECTOR_DVII:
253 prop = dev->mode_config.dvi_i_subconnector_property;
255 case DRM_MODE_CONNECTOR_Composite:
256 case DRM_MODE_CONNECTOR_SVIDEO:
257 case DRM_MODE_CONNECTOR_Component:
258 case DRM_MODE_CONNECTOR_TV:
259 prop = dev->mode_config.tv_subconnector_property;
263 DRM_ERROR("Wrong connector type for this property\n");
268 DRM_ERROR("Unable to find subconnector property\n");
272 ret = drm_connector_property_get_value(connector, prop, &subconnector);
276 return snprintf(buf, PAGE_SIZE, "%s", is_tv ?
277 drm_get_tv_subconnector_name((int)subconnector) :
278 drm_get_dvi_i_subconnector_name((int)subconnector));
281 static ssize_t select_subconnector_show(struct device *device,
282 struct device_attribute *attr,
285 struct drm_connector *connector = to_drm_connector(device);
286 struct drm_device *dev = connector->dev;
287 struct drm_property *prop = NULL;
288 uint64_t subconnector;
292 switch (connector->connector_type) {
293 case DRM_MODE_CONNECTOR_DVII:
294 prop = dev->mode_config.dvi_i_select_subconnector_property;
296 case DRM_MODE_CONNECTOR_Composite:
297 case DRM_MODE_CONNECTOR_SVIDEO:
298 case DRM_MODE_CONNECTOR_Component:
299 case DRM_MODE_CONNECTOR_TV:
300 prop = dev->mode_config.tv_select_subconnector_property;
304 DRM_ERROR("Wrong connector type for this property\n");
309 DRM_ERROR("Unable to find select subconnector property\n");
313 ret = drm_connector_property_get_value(connector, prop, &subconnector);
317 return snprintf(buf, PAGE_SIZE, "%s", is_tv ?
318 drm_get_tv_select_name((int)subconnector) :
319 drm_get_dvi_i_select_name((int)subconnector));
322 static struct device_attribute connector_attrs[] = {
329 /* These attributes are for both DVI-I connectors and all types of tv-out. */
330 static struct device_attribute connector_attrs_opt1[] = {
331 __ATTR_RO(subconnector),
332 __ATTR_RO(select_subconnector),
335 static struct bin_attribute edid_attr = {
343 * drm_sysfs_connector_add - add an connector to sysfs
344 * @connector: connector to add
346 * Create an connector device in sysfs, along with its associated connector
347 * properties (so far, connection status, dpms, mode list & edid) and
348 * generate a hotplug event so userspace knows there's a new connector
352 * This routine should only be called *once* for each DRM minor registered.
353 * A second call for an already registered device will trigger the BUG_ON
356 int drm_sysfs_connector_add(struct drm_connector *connector)
358 struct drm_device *dev = connector->dev;
361 /* We shouldn't get called more than once for the same connector */
362 BUG_ON(device_is_registered(&connector->kdev));
364 connector->kdev.parent = &dev->primary->kdev;
365 connector->kdev.class = drm_class;
366 connector->kdev.release = drm_sysfs_device_release;
368 DRM_DEBUG("adding \"%s\" to sysfs\n",
369 drm_get_connector_name(connector));
371 dev_set_name(&connector->kdev, "card%d-%s",
372 dev->primary->index, drm_get_connector_name(connector));
373 ret = device_register(&connector->kdev);
376 DRM_ERROR("failed to register connector device: %d\n", ret);
380 /* Standard attributes */
382 for (i = 0; i < ARRAY_SIZE(connector_attrs); i++) {
383 ret = device_create_file(&connector->kdev, &connector_attrs[i]);
388 /* Optional attributes */
390 * In the long run it maybe a good idea to make one set of
391 * optionals per connector type.
393 switch (connector->connector_type) {
394 case DRM_MODE_CONNECTOR_DVII:
395 case DRM_MODE_CONNECTOR_Composite:
396 case DRM_MODE_CONNECTOR_SVIDEO:
397 case DRM_MODE_CONNECTOR_Component:
398 case DRM_MODE_CONNECTOR_TV:
399 for (i = 0; i < ARRAY_SIZE(connector_attrs_opt1); i++) {
400 ret = device_create_file(&connector->kdev, &connector_attrs_opt1[i]);
409 ret = sysfs_create_bin_file(&connector->kdev.kobj, &edid_attr);
413 /* Let userspace know we have a new connector */
414 drm_sysfs_hotplug_event(dev);
420 for (j = 0; j < i; j++)
421 device_remove_file(&connector->kdev,
422 &connector_attrs[i]);
423 device_unregister(&connector->kdev);
428 EXPORT_SYMBOL(drm_sysfs_connector_add);
431 * drm_sysfs_connector_remove - remove an connector device from sysfs
432 * @connector: connector to remove
434 * Remove @connector and its associated attributes from sysfs. Note that
435 * the device model core will take care of sending the "remove" uevent
436 * at this time, so we don't need to do it.
439 * This routine should only be called if the connector was previously
440 * successfully registered. If @connector hasn't been registered yet,
441 * you'll likely see a panic somewhere deep in sysfs code when called.
443 void drm_sysfs_connector_remove(struct drm_connector *connector)
447 DRM_DEBUG("removing \"%s\" from sysfs\n",
448 drm_get_connector_name(connector));
450 for (i = 0; i < ARRAY_SIZE(connector_attrs); i++)
451 device_remove_file(&connector->kdev, &connector_attrs[i]);
452 sysfs_remove_bin_file(&connector->kdev.kobj, &edid_attr);
453 device_unregister(&connector->kdev);
455 EXPORT_SYMBOL(drm_sysfs_connector_remove);
458 * drm_sysfs_hotplug_event - generate a DRM uevent
461 * Send a uevent for the DRM device specified by @dev. Currently we only
462 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
463 * deal with other types of events.
465 void drm_sysfs_hotplug_event(struct drm_device *dev)
467 char *event_string = "HOTPLUG=1";
468 char *envp[] = { event_string, NULL };
470 DRM_DEBUG("generating hotplug event\n");
472 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, envp);
474 EXPORT_SYMBOL(drm_sysfs_hotplug_event);
477 * drm_sysfs_device_add - adds a class device to sysfs for a character driver
478 * @dev: DRM device to be added
479 * @head: DRM head in question
481 * Add a DRM device to the DRM's device model class. We use @dev's PCI device
482 * as the parent for the Linux device, and make sure it has a file containing
483 * the driver we're using (for userspace compatibility).
485 int drm_sysfs_device_add(struct drm_minor *minor)
490 minor->kdev.parent = &minor->dev->pdev->dev;
491 minor->kdev.class = drm_class;
492 minor->kdev.release = drm_sysfs_device_release;
493 minor->kdev.devt = minor->device;
494 minor->kdev.type = &drm_sysfs_device_minor;
495 if (minor->type == DRM_MINOR_CONTROL)
496 minor_str = "controlD%d";
497 else if (minor->type == DRM_MINOR_RENDER)
498 minor_str = "renderD%d";
500 minor_str = "card%d";
502 dev_set_name(&minor->kdev, minor_str, minor->index);
504 err = device_register(&minor->kdev);
506 DRM_ERROR("device add failed: %d\n", err);
517 * drm_sysfs_device_remove - remove DRM device
518 * @dev: DRM device to remove
520 * This call unregisters and cleans up a class device that was created with a
521 * call to drm_sysfs_device_add()
523 void drm_sysfs_device_remove(struct drm_minor *minor)
525 device_unregister(&minor->kdev);
530 * drm_class_device_register - Register a struct device in the drm class.
532 * @dev: pointer to struct device to register.
534 * @dev should have all relevant members pre-filled with the exception
535 * of the class member. In particular, the device_type member must
539 int drm_class_device_register(struct device *dev)
541 dev->class = drm_class;
542 return device_register(dev);
544 EXPORT_SYMBOL_GPL(drm_class_device_register);
546 void drm_class_device_unregister(struct device *dev)
548 return device_unregister(dev);
550 EXPORT_SYMBOL_GPL(drm_class_device_unregister);