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)
25 * drm_sysfs_suspend - DRM class suspend hook
26 * @dev: Linux device to suspend
27 * @state: power state to enter
29 * Just figures out what the actual struct drm_device associated with
30 * @dev is and calls its suspend hook, if present.
32 static int drm_sysfs_suspend(struct device *dev, pm_message_t state)
34 struct drm_minor *drm_minor = to_drm_minor(dev);
35 struct drm_device *drm_dev = drm_minor->dev;
37 printk(KERN_ERR "%s\n", __FUNCTION__);
39 if (drm_minor->type == DRM_MINOR_CONTROL)
40 if (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_CONTROL)
59 if (drm_dev->driver->resume)
60 return drm_dev->driver->resume(drm_dev);
65 /* Display the version of drm_core. This doesn't work right in current design */
66 static ssize_t version_show(struct class *dev, char *buf)
68 return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR,
69 CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
72 static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
75 * drm_sysfs_create - create a struct drm_sysfs_class structure
76 * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
77 * @name: pointer to a string for the name of this class.
79 * This is used to create DRM class pointer that can then be used
80 * in calls to drm_sysfs_device_add().
82 * Note, the pointer created here is to be destroyed when finished by making a
83 * call to drm_sysfs_destroy().
85 struct class *drm_sysfs_create(struct module *owner, char *name)
90 class = class_create(owner, name);
96 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22))
97 class->suspend = drm_sysfs_suspend;
98 class->resume = drm_sysfs_resume;
101 err = class_create_file(class, &class_attr_version);
108 class_destroy(class);
114 * drm_sysfs_destroy - destroys DRM class
116 * Destroy the DRM device class.
118 void drm_sysfs_destroy(void)
120 if ((drm_class == NULL) || (IS_ERR(drm_class)))
122 class_remove_file(drm_class, &class_attr_version);
123 class_destroy(drm_class);
126 static ssize_t show_dri(struct device *device, struct device_attribute *attr,
129 struct drm_minor *drm_minor = to_drm_minor(device);
130 struct drm_device *drm_dev = drm_minor->dev;
131 if (drm_dev->driver->dri_library_name)
132 return drm_dev->driver->dri_library_name(drm_dev, buf);
133 return snprintf(buf, PAGE_SIZE, "%s\n", drm_dev->driver->pci_driver.name);
137 * drm_sysfs_device_release - do nothing
140 * Normally, this would free the DRM device associated with @dev, along
141 * with cleaning up any other stuff. But we do that in the DRM core, so
142 * this function can just return and hope that the core does its job.
144 static void drm_sysfs_device_release(struct device *dev)
150 * Connector properties
152 static ssize_t status_show(struct device *device,
153 struct device_attribute *attr,
156 struct drm_connector *connector = container_of(device, struct drm_connector, kdev);
157 return snprintf(buf, PAGE_SIZE, "%s",
158 drm_get_connector_status_name(connector->funcs->detect(connector)));
161 static ssize_t dpms_show(struct device *device,
162 struct device_attribute *attr,
165 struct drm_connector *connector = container_of(device, struct drm_connector, kdev);
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", drm_get_dpms_name((int)dpms_status));
179 static ssize_t enabled_show(struct device *device,
180 struct device_attribute *attr,
183 struct drm_connector *connector = container_of(device, struct drm_connector, kdev);
185 if (connector->encoder)
186 return snprintf(buf, PAGE_SIZE, "enabled");
188 return snprintf(buf, PAGE_SIZE, "disabled");
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 = container_of(connector_dev, struct drm_connector,
200 if (!connector->edid_blob_ptr)
203 edid = connector->edid_blob_ptr->data;
204 size = connector->edid_blob_ptr->length;
211 if (off + count > size)
213 memcpy(buf, edid + off, count);
218 static ssize_t modes_show(struct device *device,
219 struct device_attribute *attr,
222 struct drm_connector *connector = container_of(device, struct drm_connector, kdev);
223 struct drm_display_mode *mode;
226 list_for_each_entry(mode, &connector->modes, head) {
227 written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
234 static ssize_t subconnector_show(struct device *device,
235 struct device_attribute *attr,
238 struct drm_connector *connector = container_of(device, struct drm_connector, kdev);
239 struct drm_device *dev = connector->dev;
240 struct drm_property *prop = NULL;
241 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 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", drm_get_subconnector_name((int)subconnector));
270 static ssize_t select_subconnector_show(struct device *device,
271 struct device_attribute *attr,
274 struct drm_connector *connector = container_of(device, struct drm_connector, kdev);
275 struct drm_device *dev = connector->dev;
276 struct drm_property *prop = NULL;
277 uint64_t subconnector;
280 switch (connector->connector_type) {
281 case DRM_MODE_CONNECTOR_DVII:
282 prop = dev->mode_config.dvi_i_select_subconnector_property;
284 case DRM_MODE_CONNECTOR_Composite:
285 case DRM_MODE_CONNECTOR_SVIDEO:
286 case DRM_MODE_CONNECTOR_Component:
287 prop = dev->mode_config.tv_select_subconnector_property;
290 DRM_ERROR("Wrong connector type for this property\n");
295 DRM_ERROR("Unable to find select subconnector property\n");
299 ret = drm_connector_property_get_value(connector, prop, &subconnector);
303 return snprintf(buf, PAGE_SIZE, "%s", drm_get_select_subconnector_name((int)subconnector));
306 static struct device_attribute connector_attrs[] = {
313 /* These attributes are for both DVI-I connectors and all types of tv-out. */
314 static struct device_attribute connector_attrs_opt1[] = {
315 __ATTR_RO(subconnector),
316 __ATTR_RO(select_subconnector),
319 static struct bin_attribute edid_attr = {
326 * drm_sysfs_connector_add - add an connector to sysfs
327 * @connector: connector to add
329 * Create an connector device in sysfs, along with its associated connector
330 * properties (so far, connection status, dpms, mode list & edid) and
331 * generate a hotplug event so userspace knows there's a new connector
335 * This routine should only be called *once* for each DRM minor registered.
336 * A second call for an already registered device will trigger the BUG_ON
339 int drm_sysfs_connector_add(struct drm_connector *connector)
341 struct drm_device *dev = connector->dev;
344 /* We shouldn't get called more than once for the same connector */
345 BUG_ON(device_is_registered(&connector->kdev));
347 connector->kdev.parent = &dev->primary->kdev;
348 connector->kdev.class = drm_class;
349 connector->kdev.release = drm_sysfs_device_release;
351 DRM_DEBUG("adding \"%s\" to sysfs\n",
352 drm_get_connector_name(connector));
354 snprintf(connector->kdev.bus_id, BUS_ID_SIZE, "card%d-%s",
355 dev->primary->index, drm_get_connector_name(connector));
356 ret = device_register(&connector->kdev);
359 DRM_ERROR("failed to register connector device: %d\n", ret);
363 /* Standard attributes */
365 for (i = 0; i < ARRAY_SIZE(connector_attrs); i++) {
366 ret = device_create_file(&connector->kdev, &connector_attrs[i]);
371 /* Optional attributes */
372 /* On the long run it maybe a good idea to make one set of optionals per connector type. */
374 switch (connector->connector_type) {
375 case DRM_MODE_CONNECTOR_DVII:
376 case DRM_MODE_CONNECTOR_Composite:
377 case DRM_MODE_CONNECTOR_SVIDEO:
378 case DRM_MODE_CONNECTOR_Component:
379 for (i = 0; i < ARRAY_SIZE(connector_attrs_opt1); i++) {
380 ret = device_create_file(&connector->kdev, &connector_attrs_opt1[i]);
389 ret = sysfs_create_bin_file(&connector->kdev.kobj, &edid_attr);
393 /* Let userspace know we have a new connector */
394 drm_sysfs_hotplug_event(dev);
400 for (j = 0; j < i; j++)
401 device_remove_file(&connector->kdev, &connector_attrs[i]);
402 device_unregister(&connector->kdev);
407 EXPORT_SYMBOL(drm_sysfs_connector_add);
410 * drm_sysfs_connector_remove - remove an connector device from sysfs
411 * @connector: connector to remove
413 * Remove @connector and its associated attributes from sysfs. Note that
414 * the device model core will take care of sending the "remove" uevent
415 * at this time, so we don't need to do it.
418 * This routine should only be called if the connector was previously
419 * successfully registered. If @connector hasn't been registered yet,
420 * you'll likely see a panic somewhere deep in sysfs code when called.
422 void drm_sysfs_connector_remove(struct drm_connector *connector)
426 DRM_DEBUG("removing \"%s\" from sysfs\n",
427 drm_get_connector_name(connector));
429 for (i = 0; i < ARRAY_SIZE(connector_attrs); i++)
430 device_remove_file(&connector->kdev, &connector_attrs[i]);
431 sysfs_remove_bin_file(&connector->kdev.kobj, &edid_attr);
432 device_unregister(&connector->kdev);
434 EXPORT_SYMBOL(drm_sysfs_connector_remove);
437 * drm_sysfs_hotplug_event - generate a DRM uevent
440 * Send a uevent for the DRM device specified by @dev. Currently we only
441 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
442 * deal with other types of events.
444 void drm_sysfs_hotplug_event(struct drm_device *dev)
446 char *event_string = "HOTPLUG=1";
447 char *envp[] = { event_string, NULL };
449 DRM_DEBUG("generating hotplug event\n");
451 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, envp);
454 static struct device_attribute dri_attrs[] = {
455 __ATTR(dri_library_name, S_IRUGO, show_dri, NULL),
459 * drm_sysfs_device_add - adds a class device to sysfs for a character driver
460 * @dev: DRM device to be added
461 * @head: DRM head in question
463 * Add a DRM device to the DRM's device model class. We use @dev's PCI device
464 * as the parent for the Linux device, and make sure it has a file containing
465 * the driver we're using (for userspace compatibility).
468 * This routine should only be called *once* for each DRM minor registered.
469 * A second call for an already registered device will trigger the BUG_ON
472 int drm_sysfs_device_add(struct drm_minor *minor)
478 minor->kdev.parent = &minor->dev->pdev->dev;
479 minor->kdev.class = drm_class;
480 minor->kdev.release = drm_sysfs_device_release;
481 minor->kdev.devt = minor->device;
482 if (minor->type == DRM_MINOR_CONTROL)
483 minor_str = "controlD%d";
484 else if (minor->type == DRM_MINOR_RENDER)
485 minor_str = "renderD%d";
487 minor_str = "card%d";
489 snprintf(minor->kdev.bus_id, BUS_ID_SIZE, minor_str, minor->index);
491 /* Shouldn't register more than once */
492 BUG_ON(device_is_registered(&minor->kdev));
494 DRM_DEBUG("registering DRM device \"%s\"\n", minor->kdev.bus_id);
496 err = device_register(&minor->kdev);
498 DRM_ERROR("device add failed: %d\n", err);
502 for (i = 0; i < ARRAY_SIZE(dri_attrs); i++) {
503 err = device_create_file(&minor->kdev, &dri_attrs[i]);
512 for (j = 0; j < i; j++)
513 device_remove_file(&minor->kdev, &dri_attrs[i]);
514 device_unregister(&minor->kdev);
521 * drm_sysfs_device_remove - remove DRM device
522 * @dev: DRM device to remove
524 * This call unregisters and cleans up a class device that was created with a
525 * call to drm_sysfs_device_add()
527 void drm_sysfs_device_remove(struct drm_minor *minor)
531 for (i = 0; i < ARRAY_SIZE(dri_attrs); i++)
532 device_remove_file(&minor->kdev, &dri_attrs[i]);
533 device_unregister(&minor->kdev);