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