Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / drm / drm_sysfs.c
blob2fc10c4bbcdfd4c9be3e5a11f822259d5f2aa746
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.
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.
10 * This file is released under the GPLv2
14 #include <linux/config.h>
15 #include <linux/device.h>
16 #include <linux/kdev_t.h>
17 #include <linux/err.h>
19 #include "drm_core.h"
21 struct drm_sysfs_class {
22 struct class_device_attribute attr;
23 struct class class;
25 #define to_drm_sysfs_class(d) container_of(d, struct drm_sysfs_class, class)
27 struct simple_dev {
28 struct list_head node;
29 dev_t dev;
30 struct class_device class_dev;
32 #define to_simple_dev(d) container_of(d, struct simple_dev, class_dev)
34 static LIST_HEAD(simple_dev_list);
35 static DEFINE_SPINLOCK(simple_dev_list_lock);
37 static void release_simple_dev(struct class_device *class_dev)
39 struct simple_dev *s_dev = to_simple_dev(class_dev);
40 kfree(s_dev);
43 static ssize_t show_dev(struct class_device *class_dev, char *buf)
45 struct simple_dev *s_dev = to_simple_dev(class_dev);
46 return print_dev_t(buf, s_dev->dev);
49 static void drm_sysfs_class_release(struct class *class)
51 struct drm_sysfs_class *cs = to_drm_sysfs_class(class);
52 kfree(cs);
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)
58 return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR,
59 CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
62 static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
64 /**
65 * drm_sysfs_create - create a struct drm_sysfs_class structure
66 * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
67 * @name: pointer to a string for the name of this class.
69 * This is used to create a struct drm_sysfs_class pointer that can then be used
70 * in calls to drm_sysfs_device_add().
72 * Note, the pointer created here is to be destroyed when finished by making a
73 * call to drm_sysfs_destroy().
75 struct drm_sysfs_class *drm_sysfs_create(struct module *owner, char *name)
77 struct drm_sysfs_class *cs;
78 int retval;
80 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
81 if (!cs) {
82 retval = -ENOMEM;
83 goto error;
85 memset(cs, 0x00, sizeof(*cs));
87 cs->class.name = name;
88 cs->class.class_release = drm_sysfs_class_release;
89 cs->class.release = release_simple_dev;
91 cs->attr.attr.name = "dev";
92 cs->attr.attr.mode = S_IRUGO;
93 cs->attr.attr.owner = owner;
94 cs->attr.show = show_dev;
95 cs->attr.store = NULL;
97 retval = class_register(&cs->class);
98 if (retval)
99 goto error;
100 class_create_file(&cs->class, &class_attr_version);
102 return cs;
104 error:
105 kfree(cs);
106 return ERR_PTR(retval);
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
113 * Note, the pointer to be destroyed must have been created with a call to
114 * drm_sysfs_create().
116 void drm_sysfs_destroy(struct drm_sysfs_class *cs)
118 if ((cs == NULL) || (IS_ERR(cs)))
119 return;
121 class_unregister(&cs->class);
125 * drm_sysfs_device_add - adds a class device to sysfs for a character driver
126 * @cs: pointer to the struct drm_sysfs_class that this device should be registered to.
127 * @dev: the dev_t for the device to be added.
128 * @device: a pointer to a struct device that is assiociated with this class device.
129 * @fmt: string for the class device's name
131 * A struct class_device will be created in sysfs, registered to the specified
132 * class. A "dev" file will be created, showing the dev_t for the device. The
133 * pointer to the struct class_device will be returned from the call. Any further
134 * sysfs files that might be required can be created using this pointer.
135 * Note: the struct drm_sysfs_class passed to this function must have previously been
136 * created with a call to drm_sysfs_create().
138 struct class_device *drm_sysfs_device_add(struct drm_sysfs_class *cs, dev_t dev,
139 struct device *device,
140 const char *fmt, ...)
142 va_list args;
143 struct simple_dev *s_dev = NULL;
144 int retval;
146 if ((cs == NULL) || (IS_ERR(cs))) {
147 retval = -ENODEV;
148 goto error;
151 s_dev = kmalloc(sizeof(*s_dev), GFP_KERNEL);
152 if (!s_dev) {
153 retval = -ENOMEM;
154 goto error;
156 memset(s_dev, 0x00, sizeof(*s_dev));
158 s_dev->dev = dev;
159 s_dev->class_dev.dev = device;
160 s_dev->class_dev.class = &cs->class;
162 va_start(args, fmt);
163 vsnprintf(s_dev->class_dev.class_id, BUS_ID_SIZE, fmt, args);
164 va_end(args);
165 retval = class_device_register(&s_dev->class_dev);
166 if (retval)
167 goto error;
169 class_device_create_file(&s_dev->class_dev, &cs->attr);
171 spin_lock(&simple_dev_list_lock);
172 list_add(&s_dev->node, &simple_dev_list);
173 spin_unlock(&simple_dev_list_lock);
175 return &s_dev->class_dev;
177 error:
178 kfree(s_dev);
179 return ERR_PTR(retval);
183 * drm_sysfs_device_remove - removes a class device that was created with drm_sysfs_device_add()
184 * @dev: the dev_t of the device that was previously registered.
186 * This call unregisters and cleans up a class device that was created with a
187 * call to drm_sysfs_device_add()
189 void drm_sysfs_device_remove(dev_t dev)
191 struct simple_dev *s_dev = NULL;
192 int found = 0;
194 spin_lock(&simple_dev_list_lock);
195 list_for_each_entry(s_dev, &simple_dev_list, node) {
196 if (s_dev->dev == dev) {
197 found = 1;
198 break;
201 if (found) {
202 list_del(&s_dev->node);
203 spin_unlock(&simple_dev_list_lock);
204 class_device_unregister(&s_dev->class_dev);
205 } else {
206 spin_unlock(&simple_dev_list_lock);