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)
27 * drm_sysfs_suspend - DRM class suspend hook
28 * @dev: Linux device to suspend
29 * @state: power state to enter
31 * Just figures out what the actual struct drm_device associated with
32 * @dev is and calls its suspend hook, if present.
34 static int drm_sysfs_suspend(struct device *dev, pm_message_t state)
36 struct drm_minor *drm_minor = to_drm_minor(dev);
37 struct drm_device *drm_dev = drm_minor->dev;
39 if (drm_minor->type == DRM_MINOR_LEGACY &&
40 !drm_core_check_feature(drm_dev, DRIVER_MODESET) &&
41 drm_dev->driver->suspend)
42 return drm_dev->driver->suspend(drm_dev, state);
48 * drm_sysfs_resume - DRM class resume hook
49 * @dev: Linux device to resume
51 * Just figures out what the actual struct drm_device associated with
52 * @dev is and calls its resume hook, if present.
54 static int drm_sysfs_resume(struct device *dev)
56 struct drm_minor *drm_minor = to_drm_minor(dev);
57 struct drm_device *drm_dev = drm_minor->dev;
59 if (drm_minor->type == DRM_MINOR_LEGACY &&
60 !drm_core_check_feature(drm_dev, DRIVER_MODESET) &&
61 drm_dev->driver->resume)
62 return drm_dev->driver->resume(drm_dev);
67 /* Display the version of drm_core. This doesn't work right in current design */
68 static ssize_t version_show(struct class *dev, char *buf)
70 return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR,
71 CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
74 static char *drm_nodename(struct device *dev)
76 return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
79 static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
82 * drm_sysfs_create - create a struct drm_sysfs_class structure
83 * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
84 * @name: pointer to a string for the name of this class.
86 * This is used to create DRM class pointer that can then be used
87 * in calls to drm_sysfs_device_add().
89 * Note, the pointer created here is to be destroyed when finished by making a
90 * call to drm_sysfs_destroy().
92 struct class *drm_sysfs_create(struct module *owner, char *name)
97 class = class_create(owner, name);
103 class->suspend = drm_sysfs_suspend;
104 class->resume = drm_sysfs_resume;
106 err = class_create_file(class, &class_attr_version);
110 class->nodename = drm_nodename;
115 class_destroy(class);
121 * drm_sysfs_destroy - destroys DRM class
123 * Destroy the DRM device class.
125 void drm_sysfs_destroy(void)
127 if ((drm_class == NULL) || (IS_ERR(drm_class)))
129 class_remove_file(drm_class, &class_attr_version);
130 class_destroy(drm_class);
134 * drm_sysfs_device_release - do nothing
137 * Normally, this would free the DRM device associated with @dev, along
138 * with cleaning up any other stuff. But we do that in the DRM core, so
139 * this function can just return and hope that the core does its job.
141 static void drm_sysfs_device_release(struct device *dev)
143 memset(dev, 0, sizeof(struct device));
148 * Connector properties
150 static ssize_t status_show(struct device *device,
151 struct device_attribute *attr,
154 struct drm_connector *connector = to_drm_connector(device);
155 enum drm_connector_status status;
157 status = connector->funcs->detect(connector);
158 return snprintf(buf, PAGE_SIZE, "%s\n",
159 drm_get_connector_status_name(status));
162 static ssize_t dpms_show(struct device *device,
163 struct device_attribute *attr,
166 struct drm_connector *connector = to_drm_connector(device);
167 struct drm_device *dev = connector->dev;
168 uint64_t dpms_status;
171 ret = drm_connector_property_get_value(connector,
172 dev->mode_config.dpms_property,
177 return snprintf(buf, PAGE_SIZE, "%s\n",
178 drm_get_dpms_name((int)dpms_status));
181 static ssize_t enabled_show(struct device *device,
182 struct device_attribute *attr,
185 struct drm_connector *connector = to_drm_connector(device);
187 return snprintf(buf, PAGE_SIZE, "%s\n", connector->encoder ? "enabled" :
191 static ssize_t edid_show(struct kobject *kobj, struct bin_attribute *attr,
192 char *buf, loff_t off, size_t count)
194 struct device *connector_dev = container_of(kobj, struct device, kobj);
195 struct drm_connector *connector = to_drm_connector(connector_dev);
199 if (!connector->edid_blob_ptr)
202 edid = connector->edid_blob_ptr->data;
203 size = connector->edid_blob_ptr->length;
210 if (off + count > size)
212 memcpy(buf, edid + off, count);
217 static ssize_t modes_show(struct device *device,
218 struct device_attribute *attr,
221 struct drm_connector *connector = to_drm_connector(device);
222 struct drm_display_mode *mode;
225 list_for_each_entry(mode, &connector->modes, head) {
226 written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
233 static ssize_t subconnector_show(struct device *device,
234 struct device_attribute *attr,
237 struct drm_connector *connector = to_drm_connector(device);
238 struct drm_device *dev = connector->dev;
239 struct drm_property *prop = NULL;
240 uint64_t subconnector;
244 switch (connector->connector_type) {
245 case DRM_MODE_CONNECTOR_DVII:
246 prop = dev->mode_config.dvi_i_subconnector_property;
248 case DRM_MODE_CONNECTOR_Composite:
249 case DRM_MODE_CONNECTOR_SVIDEO:
250 case DRM_MODE_CONNECTOR_Component:
251 case DRM_MODE_CONNECTOR_TV:
252 prop = dev->mode_config.tv_subconnector_property;
256 DRM_ERROR("Wrong connector type for this property\n");
261 DRM_ERROR("Unable to find subconnector property\n");
265 ret = drm_connector_property_get_value(connector, prop, &subconnector);
269 return snprintf(buf, PAGE_SIZE, "%s", is_tv ?
270 drm_get_tv_subconnector_name((int)subconnector) :
271 drm_get_dvi_i_subconnector_name((int)subconnector));
274 static ssize_t select_subconnector_show(struct device *device,
275 struct device_attribute *attr,
278 struct drm_connector *connector = to_drm_connector(device);
279 struct drm_device *dev = connector->dev;
280 struct drm_property *prop = NULL;
281 uint64_t subconnector;
285 switch (connector->connector_type) {
286 case DRM_MODE_CONNECTOR_DVII:
287 prop = dev->mode_config.dvi_i_select_subconnector_property;
289 case DRM_MODE_CONNECTOR_Composite:
290 case DRM_MODE_CONNECTOR_SVIDEO:
291 case DRM_MODE_CONNECTOR_Component:
292 case DRM_MODE_CONNECTOR_TV:
293 prop = dev->mode_config.tv_select_subconnector_property;
297 DRM_ERROR("Wrong connector type for this property\n");
302 DRM_ERROR("Unable to find select subconnector property\n");
306 ret = drm_connector_property_get_value(connector, prop, &subconnector);
310 return snprintf(buf, PAGE_SIZE, "%s", is_tv ?
311 drm_get_tv_select_name((int)subconnector) :
312 drm_get_dvi_i_select_name((int)subconnector));
315 static struct device_attribute connector_attrs[] = {
322 /* These attributes are for both DVI-I connectors and all types of tv-out. */
323 static struct device_attribute connector_attrs_opt1[] = {
324 __ATTR_RO(subconnector),
325 __ATTR_RO(select_subconnector),
328 static struct bin_attribute edid_attr = {
336 * drm_sysfs_connector_add - add an connector to sysfs
337 * @connector: connector to add
339 * Create an connector device in sysfs, along with its associated connector
340 * properties (so far, connection status, dpms, mode list & edid) and
341 * generate a hotplug event so userspace knows there's a new connector
345 * This routine should only be called *once* for each DRM minor registered.
346 * A second call for an already registered device will trigger the BUG_ON
349 int drm_sysfs_connector_add(struct drm_connector *connector)
351 struct drm_device *dev = connector->dev;
354 /* We shouldn't get called more than once for the same connector */
355 BUG_ON(device_is_registered(&connector->kdev));
357 connector->kdev.parent = &dev->primary->kdev;
358 connector->kdev.class = drm_class;
359 connector->kdev.release = drm_sysfs_device_release;
361 DRM_DEBUG("adding \"%s\" to sysfs\n",
362 drm_get_connector_name(connector));
364 dev_set_name(&connector->kdev, "card%d-%s",
365 dev->primary->index, drm_get_connector_name(connector));
366 ret = device_register(&connector->kdev);
369 DRM_ERROR("failed to register connector device: %d\n", ret);
373 /* Standard attributes */
375 for (i = 0; i < ARRAY_SIZE(connector_attrs); i++) {
376 ret = device_create_file(&connector->kdev, &connector_attrs[i]);
381 /* Optional attributes */
383 * In the long run it maybe a good idea to make one set of
384 * optionals per connector type.
386 switch (connector->connector_type) {
387 case DRM_MODE_CONNECTOR_DVII:
388 case DRM_MODE_CONNECTOR_Composite:
389 case DRM_MODE_CONNECTOR_SVIDEO:
390 case DRM_MODE_CONNECTOR_Component:
391 case DRM_MODE_CONNECTOR_TV:
392 for (i = 0; i < ARRAY_SIZE(connector_attrs_opt1); i++) {
393 ret = device_create_file(&connector->kdev, &connector_attrs_opt1[i]);
402 ret = sysfs_create_bin_file(&connector->kdev.kobj, &edid_attr);
406 /* Let userspace know we have a new connector */
407 drm_sysfs_hotplug_event(dev);
413 for (j = 0; j < i; j++)
414 device_remove_file(&connector->kdev,
415 &connector_attrs[i]);
416 device_unregister(&connector->kdev);
421 EXPORT_SYMBOL(drm_sysfs_connector_add);
424 * drm_sysfs_connector_remove - remove an connector device from sysfs
425 * @connector: connector to remove
427 * Remove @connector and its associated attributes from sysfs. Note that
428 * the device model core will take care of sending the "remove" uevent
429 * at this time, so we don't need to do it.
432 * This routine should only be called if the connector was previously
433 * successfully registered. If @connector hasn't been registered yet,
434 * you'll likely see a panic somewhere deep in sysfs code when called.
436 void drm_sysfs_connector_remove(struct drm_connector *connector)
440 DRM_DEBUG("removing \"%s\" from sysfs\n",
441 drm_get_connector_name(connector));
443 for (i = 0; i < ARRAY_SIZE(connector_attrs); i++)
444 device_remove_file(&connector->kdev, &connector_attrs[i]);
445 sysfs_remove_bin_file(&connector->kdev.kobj, &edid_attr);
446 device_unregister(&connector->kdev);
448 EXPORT_SYMBOL(drm_sysfs_connector_remove);
451 * drm_sysfs_hotplug_event - generate a DRM uevent
454 * Send a uevent for the DRM device specified by @dev. Currently we only
455 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
456 * deal with other types of events.
458 void drm_sysfs_hotplug_event(struct drm_device *dev)
460 char *event_string = "HOTPLUG=1";
461 char *envp[] = { event_string, NULL };
463 DRM_DEBUG("generating hotplug event\n");
465 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, envp);
467 EXPORT_SYMBOL(drm_sysfs_hotplug_event);
470 * drm_sysfs_device_add - adds a class device to sysfs for a character driver
471 * @dev: DRM device to be added
472 * @head: DRM head in question
474 * Add a DRM device to the DRM's device model class. We use @dev's PCI device
475 * as the parent for the Linux device, and make sure it has a file containing
476 * the driver we're using (for userspace compatibility).
478 int drm_sysfs_device_add(struct drm_minor *minor)
483 minor->kdev.parent = &minor->dev->pdev->dev;
484 minor->kdev.class = drm_class;
485 minor->kdev.release = drm_sysfs_device_release;
486 minor->kdev.devt = minor->device;
487 if (minor->type == DRM_MINOR_CONTROL)
488 minor_str = "controlD%d";
489 else if (minor->type == DRM_MINOR_RENDER)
490 minor_str = "renderD%d";
492 minor_str = "card%d";
494 dev_set_name(&minor->kdev, minor_str, minor->index);
496 err = device_register(&minor->kdev);
498 DRM_ERROR("device add failed: %d\n", err);
509 * drm_sysfs_device_remove - remove DRM device
510 * @dev: DRM device to remove
512 * This call unregisters and cleans up a class device that was created with a
513 * call to drm_sysfs_device_add()
515 void drm_sysfs_device_remove(struct drm_minor *minor)
517 device_unregister(&minor->kdev);
522 * drm_class_device_register - Register a struct device in the drm class.
524 * @dev: pointer to struct device to register.
526 * @dev should have all relevant members pre-filled with the exception
527 * of the class member. In particular, the device_type member must
531 int drm_class_device_register(struct device *dev)
533 dev->class = drm_class;
534 return device_register(dev);
536 EXPORT_SYMBOL_GPL(drm_class_device_register);
538 void drm_class_device_unregister(struct device *dev)
540 return device_unregister(dev);
542 EXPORT_SYMBOL_GPL(drm_class_device_unregister);