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 edid_show(struct kobject *kobj, struct bin_attribute *attr,
180 char *buf, loff_t off, size_t count)
182 struct device *connector_dev = container_of(kobj, struct device, kobj);
183 struct drm_connector *connector = container_of(connector_dev, struct drm_connector,
188 if (!connector->edid_blob_ptr)
191 edid = connector->edid_blob_ptr->data;
192 size = connector->edid_blob_ptr->length;
199 if (off + count > size)
201 memcpy(buf, edid + off, count);
206 static ssize_t modes_show(struct device *device,
207 struct device_attribute *attr,
210 struct drm_connector *connector = container_of(device, struct drm_connector, kdev);
211 struct drm_display_mode *mode;
214 list_for_each_entry(mode, &connector->modes, head) {
215 written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
222 static struct device_attribute connector_attrs[] = {
228 static struct bin_attribute edid_attr = {
235 * drm_sysfs_connector_add - add an connector to sysfs
236 * @connector: connector to add
238 * Create an connector device in sysfs, along with its associated connector
239 * properties (so far, connection status, dpms, mode list & edid) and
240 * generate a hotplug event so userspace knows there's a new connector
244 * This routine should only be called *once* for each DRM minor registered.
245 * A second call for an already registered device will trigger the BUG_ON
248 int drm_sysfs_connector_add(struct drm_connector *connector)
250 struct drm_device *dev = connector->dev;
253 /* We shouldn't get called more than once for the same connector */
254 BUG_ON(device_is_registered(&connector->kdev));
256 connector->kdev.parent = &dev->primary->kdev;
257 connector->kdev.class = drm_class;
258 connector->kdev.release = drm_sysfs_device_release;
260 DRM_DEBUG("adding \"%s\" to sysfs\n",
261 drm_get_connector_name(connector));
263 snprintf(connector->kdev.bus_id, BUS_ID_SIZE, "card%d-%s",
264 dev->primary->index, drm_get_connector_name(connector));
265 ret = device_register(&connector->kdev);
268 DRM_ERROR("failed to register connector device: %d\n", ret);
272 for (i = 0; i < ARRAY_SIZE(connector_attrs); i++) {
273 ret = device_create_file(&connector->kdev, &connector_attrs[i]);
278 ret = sysfs_create_bin_file(&connector->kdev.kobj, &edid_attr);
282 /* Let userspace know we have a new connector */
283 drm_sysfs_hotplug_event(dev);
289 for (j = 0; j < i; j++)
290 device_remove_file(&connector->kdev, &connector_attrs[i]);
291 device_unregister(&connector->kdev);
296 EXPORT_SYMBOL(drm_sysfs_connector_add);
299 * drm_sysfs_connector_remove - remove an connector device from sysfs
300 * @connector: connector to remove
302 * Remove @connector and its associated attributes from sysfs. Note that
303 * the device model core will take care of sending the "remove" uevent
304 * at this time, so we don't need to do it.
307 * This routine should only be called if the connector was previously
308 * successfully registered. If @connector hasn't been registered yet,
309 * you'll likely see a panic somewhere deep in sysfs code when called.
311 void drm_sysfs_connector_remove(struct drm_connector *connector)
315 DRM_DEBUG("removing \"%s\" from sysfs\n",
316 drm_get_connector_name(connector));
318 for (i = 0; i < ARRAY_SIZE(connector_attrs); i++)
319 device_remove_file(&connector->kdev, &connector_attrs[i]);
320 sysfs_remove_bin_file(&connector->kdev.kobj, &edid_attr);
321 device_unregister(&connector->kdev);
323 EXPORT_SYMBOL(drm_sysfs_connector_remove);
326 * drm_sysfs_hotplug_event - generate a DRM uevent
329 * Send a uevent for the DRM device specified by @dev. Currently we only
330 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
331 * deal with other types of events.
333 void drm_sysfs_hotplug_event(struct drm_device *dev)
335 char *event_string = "HOTPLUG=1";
336 char *envp[] = { event_string, NULL };
338 DRM_DEBUG("generating hotplug event\n");
340 kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, envp);
343 static struct device_attribute dri_attrs[] = {
344 __ATTR(dri_library_name, S_IRUGO, show_dri, NULL),
348 * drm_sysfs_device_add - adds a class device to sysfs for a character driver
349 * @dev: DRM device to be added
350 * @head: DRM head in question
352 * Add a DRM device to the DRM's device model class. We use @dev's PCI device
353 * as the parent for the Linux device, and make sure it has a file containing
354 * the driver we're using (for userspace compatibility).
357 * This routine should only be called *once* for each DRM minor registered.
358 * A second call for an already registered device will trigger the BUG_ON
361 int drm_sysfs_device_add(struct drm_minor *minor)
367 minor->kdev.parent = &minor->dev->pdev->dev;
368 minor->kdev.class = drm_class;
369 minor->kdev.release = drm_sysfs_device_release;
370 minor->kdev.devt = minor->device;
371 if (minor->type == DRM_MINOR_CONTROL)
372 minor_str = "controlD%d";
373 else if (minor->type == DRM_MINOR_RENDER)
374 minor_str = "renderD%d";
376 minor_str = "card%d";
378 snprintf(minor->kdev.bus_id, BUS_ID_SIZE, minor_str, minor->index);
380 /* Shouldn't register more than once */
381 BUG_ON(device_is_registered(&minor->kdev));
383 DRM_DEBUG("registering DRM device \"%s\"\n", minor->kdev.bus_id);
385 err = device_register(&minor->kdev);
387 DRM_ERROR("device add failed: %d\n", err);
391 for (i = 0; i < ARRAY_SIZE(dri_attrs); i++) {
392 err = device_create_file(&minor->kdev, &dri_attrs[i]);
401 for (j = 0; j < i; j++)
402 device_remove_file(&minor->kdev, &dri_attrs[i]);
403 device_unregister(&minor->kdev);
410 * drm_sysfs_device_remove - remove DRM device
411 * @dev: DRM device to remove
413 * This call unregisters and cleans up a class device that was created with a
414 * call to drm_sysfs_device_add()
416 void drm_sysfs_device_remove(struct drm_minor *minor)
420 for (i = 0; i < ARRAY_SIZE(dri_attrs); i++)
421 device_remove_file(&minor->kdev, &dri_attrs[i]);
422 device_unregister(&minor->kdev);