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