drm: remove drmP.h internal typedefs
[platform/upstream/libdrm.git] / linux-core / drm_sysfs.c
1 /*
2  * drm_sysfs.c - Modifications to drm_sysfs_class.c to support
3  *               extra sysfs attribute from DRM. Normal drm_sysfs_class
4  *               does not allow adding attributes.
5  *
6  * Copyright (c) 2004 Jon Smirl <jonsmirl@gmail.com>
7  * Copyright (c) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
8  * Copyright (c) 2003-2004 IBM Corp.
9  *
10  * This file is released under the GPLv2
11  *
12  */
13
14 #include <linux/device.h>
15 #include <linux/kdev_t.h>
16 #include <linux/err.h>
17
18 #include "drmP.h"
19 #include "drm_core.h"
20
21 struct drm_sysfs_class {
22         struct class_device_attribute attr;
23         struct class class;
24 };
25 #define to_drm_sysfs_class(d) container_of(d, struct drm_sysfs_class, class)
26
27 struct simple_dev {
28         dev_t dev;
29         struct class_device class_dev;
30 };
31 #define to_simple_dev(d) container_of(d, struct simple_dev, class_dev)
32
33 static void release_simple_dev(struct class_device *class_dev)
34 {
35         struct simple_dev *s_dev = to_simple_dev(class_dev);
36         kfree(s_dev);
37 }
38
39 static ssize_t show_dev(struct class_device *class_dev, char *buf)
40 {
41         struct simple_dev *s_dev = to_simple_dev(class_dev);
42         return print_dev_t(buf, s_dev->dev);
43 }
44
45 static void drm_sysfs_class_release(struct class *class)
46 {
47         struct drm_sysfs_class *cs = to_drm_sysfs_class(class);
48         kfree(cs);
49 }
50
51 /* Display the version of drm_core. This doesn't work right in current design */
52 static ssize_t version_show(struct class *dev, char *buf)
53 {
54         return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR,
55                        CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
56 }
57
58 static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
59
60 /**
61  * drm_sysfs_create - create a struct drm_sysfs_class structure
62  * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
63  * @name: pointer to a string for the name of this class.
64  *
65  * This is used to create a struct drm_sysfs_class pointer that can then be used
66  * in calls to drm_sysfs_device_add().
67  *
68  * Note, the pointer created here is to be destroyed when finished by making a
69  * call to drm_sysfs_destroy().
70  */
71 struct drm_sysfs_class *drm_sysfs_create(struct module *owner, char *name)
72 {
73         struct drm_sysfs_class *cs;
74         int retval;
75
76         cs = kmalloc(sizeof(*cs), GFP_KERNEL);
77         if (!cs) {
78                 retval = -ENOMEM;
79                 goto error;
80         }
81         memset(cs, 0x00, sizeof(*cs));
82
83         cs->class.name = name;
84         cs->class.class_release = drm_sysfs_class_release;
85         cs->class.release = release_simple_dev;
86
87         cs->attr.attr.name = "dev";
88         cs->attr.attr.mode = S_IRUGO;
89         cs->attr.attr.owner = owner;
90         cs->attr.show = show_dev;
91         cs->attr.store = NULL;
92
93         retval = class_register(&cs->class);
94         if (retval)
95                 goto error;
96         retval = class_create_file(&cs->class, &class_attr_version);
97         if (retval)
98                 goto error_with_class;
99
100         return cs;
101
102  error_with_class:
103         class_unregister(&cs->class);
104  error:
105         kfree(cs);
106         return ERR_PTR(retval);
107 }
108
109 /**
110  * drm_sysfs_destroy - destroys a struct drm_sysfs_class structure
111  * @cs: pointer to the struct drm_sysfs_class that is to be destroyed
112  *
113  * Note, the pointer to be destroyed must have been created with a call to
114  * drm_sysfs_create().
115  */
116 void drm_sysfs_destroy(struct drm_sysfs_class *cs)
117 {
118         if ((cs == NULL) || (IS_ERR(cs)))
119                 return;
120
121         class_unregister(&cs->class);
122 }
123
124 static ssize_t show_dri(struct class_device *class_device, char *buf)
125 {
126         struct drm_device * dev = ((struct drm_head *)class_get_devdata(class_device))->dev;
127         if (dev->driver->dri_library_name)
128                 return dev->driver->dri_library_name(dev, buf);
129         return snprintf(buf, PAGE_SIZE, "%s\n", dev->driver->pci_driver.name);
130 }
131
132 static struct class_device_attribute class_device_attrs[] = {
133         __ATTR(dri_library_name, S_IRUGO, show_dri, NULL),
134 };
135
136 /**
137  * drm_sysfs_device_add - adds a class device to sysfs for a character driver
138  * @cs: pointer to the struct drm_sysfs_class that this device should be registered to.
139  * @dev: the dev_t for the device to be added.
140  * @device: a pointer to a struct device that is assiociated with this class device.
141  * @fmt: string for the class device's name
142  *
143  * A struct class_device will be created in sysfs, registered to the specified
144  * class.  A "dev" file will be created, showing the dev_t for the device.  The
145  * pointer to the struct class_device will be returned from the call.  Any further
146  * sysfs files that might be required can be created using this pointer.
147  * Note: the struct drm_sysfs_class passed to this function must have previously been
148  * created with a call to drm_sysfs_create().
149  */
150 struct class_device *drm_sysfs_device_add(struct drm_sysfs_class *cs,
151                                           struct drm_head * head)
152 {
153         struct simple_dev *s_dev = NULL;
154         int i, retval;
155
156         if ((cs == NULL) || (IS_ERR(cs))) {
157                 retval = -ENODEV;
158                 goto error;
159         }
160
161         s_dev = kmalloc(sizeof(*s_dev), GFP_KERNEL);
162         if (!s_dev) {
163                 retval = -ENOMEM;
164                 goto error;
165         }
166         memset(s_dev, 0x00, sizeof(*s_dev));
167
168         s_dev->dev = MKDEV(DRM_MAJOR, head->minor);
169         s_dev->class_dev.dev = &head->dev->pdev->dev;
170         s_dev->class_dev.class = &cs->class;
171
172         snprintf(s_dev->class_dev.class_id, BUS_ID_SIZE, "card%d", head->minor);
173         retval = class_device_register(&s_dev->class_dev);
174         if (retval)
175                 goto error;
176
177         retval = class_device_create_file(&s_dev->class_dev, &cs->attr);
178         if (retval)
179                 goto error_with_device;
180
181         class_set_devdata(&s_dev->class_dev, head);
182
183         for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++) {
184                 retval = class_device_create_file(&s_dev->class_dev,
185                                                   &class_device_attrs[i]);
186                 if (retval)
187                         goto error_with_files;
188         }
189
190         return &s_dev->class_dev;
191
192  error_with_files:
193         while (i > 0)
194                 class_device_remove_file(&s_dev->class_dev,
195                                          &class_device_attrs[--i]);
196         class_device_remove_file(&s_dev->class_dev, &cs->attr);
197  error_with_device:
198         class_device_unregister(&s_dev->class_dev);
199  error:
200         kfree(s_dev);
201
202         return ERR_PTR(retval);
203 }
204
205 /**
206  * drm_sysfs_device_remove - removes a class device that was created with drm_sysfs_device_add()
207  * @dev: the dev_t of the device that was previously registered.
208  *
209  * This call unregisters and cleans up a class device that was created with a
210  * call to drm_sysfs_device_add()
211  */
212 void drm_sysfs_device_remove(struct class_device *class_dev)
213 {
214         struct simple_dev *s_dev = to_simple_dev(class_dev);
215         int i;
216
217         for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
218                 class_device_remove_file(&s_dev->class_dev, &class_device_attrs[i]);
219
220         class_device_unregister(&s_dev->class_dev);
221 }