First check in for DRM that splits core from personality modules
[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/config.h>
15 #include <linux/device.h>
16 #include <linux/kdev_t.h>
17 #include <linux/err.h>
18
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         struct list_head node;
29         dev_t dev;
30         struct class_device class_dev;
31 };
32 #define to_simple_dev(d) container_of(d, struct simple_dev, class_dev)
33
34 static LIST_HEAD(simple_dev_list);
35 static spinlock_t simple_dev_list_lock = SPIN_LOCK_UNLOCKED;
36
37 static void release_simple_dev(struct class_device *class_dev)
38 {
39         struct simple_dev *s_dev = to_simple_dev(class_dev);
40         kfree(s_dev);
41 }
42
43 static ssize_t show_dev(struct class_device *class_dev, char *buf)
44 {
45         struct simple_dev *s_dev = to_simple_dev(class_dev);
46         return print_dev_t(buf, s_dev->dev);
47 }
48
49 static void drm_sysfs_class_release(struct class *class)
50 {
51         struct drm_sysfs_class *cs = to_drm_sysfs_class(class);
52         kfree(cs);
53 }
54
55 /* Display the version of drm_core. This doesn't work right in current design */
56 static ssize_t version_show(struct class *dev, char *buf)
57 {
58         return sprintf(buf, "%s %d.%d.%d %s\n", DRIVER_NAME, DRIVER_MAJOR, 
59                                 DRIVER_MINOR, DRIVER_PATCHLEVEL, DRIVER_DATE);
60 }
61 static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
62
63 /**
64  * drm_sysfs_create - create a struct drm_sysfs_class structure
65  * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
66  * @name: pointer to a string for the name of this class.
67  *
68  * This is used to create a struct drm_sysfs_class pointer that can then be used
69  * in calls to drm_sysfs_device_add().
70  *
71  * Note, the pointer created here is to be destroyed when finished by making a
72  * call to drm_sysfs_destroy().
73  */
74 struct drm_sysfs_class *drm_sysfs_create(struct module *owner, char *name)
75 {
76         struct drm_sysfs_class *cs;
77         int retval;
78
79         cs = kmalloc(sizeof(*cs), GFP_KERNEL);
80         if (!cs) {
81                 retval = -ENOMEM;
82                 goto error;
83         }
84         memset(cs, 0x00, sizeof(*cs));
85
86         cs->class.name = name;
87         cs->class.class_release = drm_sysfs_class_release;
88         cs->class.release = release_simple_dev;
89
90         cs->attr.attr.name = "dev";
91         cs->attr.attr.mode = S_IRUGO;
92         cs->attr.attr.owner = owner;
93         cs->attr.show = show_dev;
94         cs->attr.store = NULL;
95
96         retval = class_register(&cs->class);
97         if (retval)
98                 goto error;
99         class_create_file(&cs->class, &class_attr_version);
100
101         return cs;
102
103 error:
104         kfree(cs);
105         return ERR_PTR(retval);
106 }
107
108 /**
109  * drm_sysfs_destroy - destroys a struct drm_sysfs_class structure
110  * @cs: pointer to the struct drm_sysfs_class that is to be destroyed
111  *
112  * Note, the pointer to be destroyed must have been created with a call to
113  * drm_sysfs_create().
114  */
115 void drm_sysfs_destroy(struct drm_sysfs_class *cs)
116 {
117         if ((cs == NULL) || (IS_ERR(cs)))
118                 return;
119
120         class_unregister(&cs->class);
121 }
122
123 /**
124  * drm_sysfs_device_add - adds a class device to sysfs for a character driver
125  * @cs: pointer to the struct drm_sysfs_class that this device should be registered to.
126  * @dev: the dev_t for the device to be added.
127  * @device: a pointer to a struct device that is assiociated with this class device.
128  * @fmt: string for the class device's name
129  *
130  * A struct class_device will be created in sysfs, registered to the specified 
131  * class.  A "dev" file will be created, showing the dev_t for the device.  The 
132  * pointer to the struct class_device will be returned from the call.  Any further
133  * sysfs files that might be required can be created using this pointer.
134  * Note: the struct drm_sysfs_class passed to this function must have previously been
135  * created with a call to drm_sysfs_create().
136  */
137 struct class_device *drm_sysfs_device_add(struct drm_sysfs_class *cs, dev_t dev, struct device *device, const char *fmt, ...)
138 {
139         va_list args;
140         struct simple_dev *s_dev = NULL;
141         int retval;
142
143         if ((cs == NULL) || (IS_ERR(cs))) {
144                 retval = -ENODEV;
145                 goto error;
146         }
147
148         s_dev = kmalloc(sizeof(*s_dev), GFP_KERNEL);
149         if (!s_dev) {
150                 retval = -ENOMEM;
151                 goto error;
152         }
153         memset(s_dev, 0x00, sizeof(*s_dev));
154
155         s_dev->dev = dev;
156         s_dev->class_dev.dev = device;
157         s_dev->class_dev.class = &cs->class;
158
159         va_start(args, fmt);
160         vsnprintf(s_dev->class_dev.class_id, BUS_ID_SIZE, fmt, args);
161         va_end(args);
162         retval = class_device_register(&s_dev->class_dev);
163         if (retval)
164                 goto error;
165
166         class_device_create_file(&s_dev->class_dev, &cs->attr);
167
168         spin_lock(&simple_dev_list_lock);
169         list_add(&s_dev->node, &simple_dev_list);
170         spin_unlock(&simple_dev_list_lock);
171
172         return &s_dev->class_dev;
173
174 error:
175         kfree(s_dev);
176         return ERR_PTR(retval);
177 }
178
179 /**
180  * drm_sysfs_device_remove - removes a class device that was created with drm_sysfs_device_add()
181  * @dev: the dev_t of the device that was previously registered.
182  *
183  * This call unregisters and cleans up a class device that was created with a
184  * call to drm_sysfs_device_add()
185  */
186 void drm_sysfs_device_remove(dev_t dev)
187 {
188         struct simple_dev *s_dev = NULL;
189         int found = 0;
190
191         spin_lock(&simple_dev_list_lock);
192         list_for_each_entry(s_dev, &simple_dev_list, node) {
193                 if (s_dev->dev == dev) {
194                         found = 1;
195                         break;
196                 }
197         }
198         if (found) {
199                 list_del(&s_dev->node);
200                 spin_unlock(&simple_dev_list_lock);
201                 class_device_unregister(&s_dev->class_dev);
202         } else {
203                 spin_unlock(&simple_dev_list_lock);
204         }
205 }
206