drm: add support for passing state into the suspend hooks.
[platform/upstream/libdrm.git] / linux-core / drm_sysfs.c
1
2 /*
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.
6  *
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.
10  *
11  * This file is released under the GPLv2
12  *
13  */
14
15 #include <linux/device.h>
16 #include <linux/kdev_t.h>
17 #include <linux/err.h>
18
19 #include "drm_core.h"
20 #include "drmP.h"
21
22 #define to_drm_device(d) container_of(d, struct drm_device, dev)
23
24 /**
25  * drm_sysfs_suspend - DRM class suspend hook
26  * @dev: Linux device to suspend
27  * @state: power state to enter
28  *
29  * Just figures out what the actual struct drm_device associated with
30  * @dev is and calls its suspend hook, if present.
31  */
32 static int drm_sysfs_suspend(struct device *dev, pm_message_t state)
33 {
34         struct drm_device *drm_dev = to_drm_device(dev);
35
36         printk(KERN_ERR "%s\n", __FUNCTION__);
37
38         if (drm_dev->driver->suspend)
39                 return drm_dev->driver->suspend(drm_dev, state);
40
41         return 0;
42 }
43
44 /**
45  * drm_sysfs_resume - DRM class resume hook
46  * @dev: Linux device to resume
47  *
48  * Just figures out what the actual struct drm_device associated with
49  * @dev is and calls its resume hook, if present.
50  */
51 static int drm_sysfs_resume(struct device *dev)
52 {
53         struct drm_device *drm_dev = to_drm_device(dev);
54
55         if (drm_dev->driver->resume)
56                 return drm_dev->driver->resume(drm_dev);
57
58         return 0;
59 }
60
61 /* Display the version of drm_core. This doesn't work right in current design */
62 static ssize_t version_show(struct class *dev, char *buf)
63 {
64         return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR,
65                        CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
66 }
67
68 static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
69
70 /**
71  * drm_sysfs_create - create a struct drm_sysfs_class structure
72  * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
73  * @name: pointer to a string for the name of this class.
74  *
75  * This is used to create DRM class pointer that can then be used
76  * in calls to drm_sysfs_device_add().
77  *
78  * Note, the pointer created here is to be destroyed when finished by making a
79  * call to drm_sysfs_destroy().
80  */
81 struct class *drm_sysfs_create(struct module *owner, char *name)
82 {
83         struct class *class;
84         int err;
85
86         class = class_create(owner, name);
87         if (IS_ERR(class)) {
88                 err = PTR_ERR(class);
89                 goto err_out;
90         }
91
92 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22))
93         class->suspend = drm_sysfs_suspend;
94         class->resume = drm_sysfs_resume;
95 #endif
96
97         err = class_create_file(class, &class_attr_version);
98         if (err)
99                 goto err_out_class;
100
101         return class;
102
103 err_out_class:
104         class_destroy(class);
105 err_out:
106         return ERR_PTR(err);
107 }
108
109 /**
110  * drm_sysfs_destroy - destroys DRM class
111  *
112  * Destroy the DRM device class.
113  */
114 void drm_sysfs_destroy(void)
115 {
116         if ((drm_class == NULL) || (IS_ERR(drm_class)))
117                 return;
118         class_remove_file(drm_class, &class_attr_version);
119         class_destroy(drm_class);
120 }
121
122 static ssize_t show_dri(struct device *device, struct device_attribute *attr,
123                         char *buf)
124 {
125         struct drm_device *dev = to_drm_device(device);
126         if (dev->driver->dri_library_name)
127                 return dev->driver->dri_library_name(dev, buf);
128         return snprintf(buf, PAGE_SIZE, "%s\n", dev->driver->pci_driver.name);
129 }
130
131 static struct device_attribute device_attrs[] = {
132         __ATTR(dri_library_name, S_IRUGO, show_dri, NULL),
133 };
134
135 /**
136  * drm_sysfs_device_release - do nothing
137  * @dev: Linux device
138  *
139  * Normally, this would free the DRM device associated with @dev, along
140  * with cleaning up any other stuff.  But we do that in the DRM core, so
141  * this function can just return and hope that the core does its job.
142  */
143 static void drm_sysfs_device_release(struct device *dev)
144 {
145         return;
146 }
147
148 /**
149  * drm_sysfs_device_add - adds a class device to sysfs for a character driver
150  * @dev: DRM device to be added
151  * @head: DRM head in question
152  *
153  * Add a DRM device to the DRM's device model class.  We use @dev's PCI device
154  * as the parent for the Linux device, and make sure it has a file containing
155  * the driver we're using (for userspace compatibility).
156  */
157 int drm_sysfs_device_add(struct drm_device *dev, struct drm_head *head)
158 {
159         int err;
160         int i, j;
161
162         dev->dev.parent = &dev->pdev->dev;
163         dev->dev.class = drm_class;
164         dev->dev.release = drm_sysfs_device_release;
165         dev->dev.devt = head->device;
166         snprintf(dev->dev.bus_id, BUS_ID_SIZE, "card%d", head->minor);
167
168         err = device_register(&dev->dev);
169         if (err) {
170                 DRM_ERROR("device add failed: %d\n", err);
171                 goto err_out;
172         }
173
174         for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
175                 err = device_create_file(&dev->dev, &device_attrs[i]);
176                 if (err)
177                         goto err_out_files;
178         }
179
180         return 0;
181
182 err_out_files:
183         if (i > 0)
184                 for (j = 0; j < i; j++)
185                         device_remove_file(&dev->dev, &device_attrs[i]);
186         device_unregister(&dev->dev);
187 err_out:
188
189         return err;
190 }
191
192 /**
193  * drm_sysfs_device_remove - remove DRM device
194  * @dev: DRM device to remove
195  *
196  * This call unregisters and cleans up a class device that was created with a
197  * call to drm_sysfs_device_add()
198  */
199 void drm_sysfs_device_remove(struct drm_device *dev)
200 {
201         int i;
202
203         for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
204                 device_remove_file(&dev->dev, &device_attrs[i]);
205         device_unregister(&dev->dev);
206 }