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