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>
22 struct drm_sysfs_class {
23 struct class_device_attribute attr;
26 #define to_drm_sysfs_class(d) container_of(d, struct drm_sysfs_class, class)
29 struct list_head node;
31 struct class_device class_dev;
33 #define to_simple_dev(d) container_of(d, struct simple_dev, class_dev)
35 static LIST_HEAD(simple_dev_list);
36 static DEFINE_SPINLOCK(simple_dev_list_lock);
38 static void release_simple_dev(struct class_device *class_dev)
40 struct simple_dev *s_dev = to_simple_dev(class_dev);
44 static ssize_t show_dev(struct class_device *class_dev, char *buf)
46 struct simple_dev *s_dev = to_simple_dev(class_dev);
47 return print_dev_t(buf, s_dev->dev);
50 static void drm_sysfs_class_release(struct class *class)
52 struct drm_sysfs_class *cs = to_drm_sysfs_class(class);
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)
59 return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR,
60 CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
63 static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
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.
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().
73 * Note, the pointer created here is to be destroyed when finished by making a
74 * call to drm_sysfs_destroy().
76 struct drm_sysfs_class *drm_sysfs_create(struct module *owner, char *name)
78 struct drm_sysfs_class *cs;
81 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
86 memset(cs, 0x00, sizeof(*cs));
88 cs->class.name = name;
89 cs->class.class_release = drm_sysfs_class_release;
90 cs->class.release = release_simple_dev;
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;
98 retval = class_register(&cs->class);
101 class_create_file(&cs->class, &class_attr_version);
107 return ERR_PTR(retval);
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
114 * Note, the pointer to be destroyed must have been created with a call to
115 * drm_sysfs_create().
117 void drm_sysfs_destroy(struct drm_sysfs_class *cs)
119 if ((cs == NULL) || (IS_ERR(cs)))
122 class_unregister(&cs->class);
126 * drm_sysfs_device_add - adds a class device to sysfs for a character driver
127 * @cs: pointer to the struct drm_sysfs_class that this device should be registered to.
128 * @dev: the dev_t for the device to be added.
129 * @device: a pointer to a struct device that is assiociated with this class device.
130 * @fmt: string for the class device's name
132 * A struct class_device will be created in sysfs, registered to the specified
133 * class. A "dev" file will be created, showing the dev_t for the device. The
134 * pointer to the struct class_device will be returned from the call. Any further
135 * sysfs files that might be required can be created using this pointer.
136 * Note: the struct drm_sysfs_class passed to this function must have previously been
137 * created with a call to drm_sysfs_create().
139 struct class_device *drm_sysfs_device_add(struct drm_sysfs_class *cs, dev_t dev,
140 struct device *device,
141 const char *fmt, ...)
144 struct simple_dev *s_dev = NULL;
147 if ((cs == NULL) || (IS_ERR(cs))) {
152 s_dev = kmalloc(sizeof(*s_dev), GFP_KERNEL);
157 memset(s_dev, 0x00, sizeof(*s_dev));
160 s_dev->class_dev.dev = device;
161 s_dev->class_dev.class = &cs->class;
164 vsnprintf(s_dev->class_dev.class_id, BUS_ID_SIZE, fmt, args);
166 retval = class_device_register(&s_dev->class_dev);
170 class_device_create_file(&s_dev->class_dev, &cs->attr);
172 spin_lock(&simple_dev_list_lock);
173 list_add(&s_dev->node, &simple_dev_list);
174 spin_unlock(&simple_dev_list_lock);
176 return &s_dev->class_dev;
180 return ERR_PTR(retval);
184 * drm_sysfs_device_remove - removes a class device that was created with drm_sysfs_device_add()
185 * @dev: the dev_t of the device that was previously registered.
187 * This call unregisters and cleans up a class device that was created with a
188 * call to drm_sysfs_device_add()
190 void drm_sysfs_device_remove(dev_t dev)
192 struct simple_dev *s_dev = NULL;
195 spin_lock(&simple_dev_list_lock);
196 list_for_each_entry(s_dev, &simple_dev_list, node) {
197 if (s_dev->dev == dev) {
203 list_del(&s_dev->node);
204 spin_unlock(&simple_dev_list_lock);
205 class_device_unregister(&s_dev->class_dev);
207 spin_unlock(&simple_dev_list_lock);