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)
30 struct class_device class_dev;
32 #define to_simple_dev(d) container_of(d, struct simple_dev, class_dev)
34 static void release_simple_dev(struct class_device *class_dev)
36 struct simple_dev *s_dev = to_simple_dev(class_dev);
40 static ssize_t show_dev(struct class_device *class_dev, char *buf)
42 struct simple_dev *s_dev = to_simple_dev(class_dev);
43 return print_dev_t(buf, s_dev->dev);
46 static void drm_sysfs_class_release(struct class *class)
48 struct drm_sysfs_class *cs = to_drm_sysfs_class(class);
52 /* Display the version of drm_core. This doesn't work right in current design */
53 static ssize_t version_show(struct class *dev, char *buf)
55 return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR,
56 CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
59 static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
62 * drm_sysfs_create - create a struct drm_sysfs_class structure
63 * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
64 * @name: pointer to a string for the name of this class.
66 * This is used to create a struct drm_sysfs_class pointer that can then be used
67 * in calls to drm_sysfs_device_add().
69 * Note, the pointer created here is to be destroyed when finished by making a
70 * call to drm_sysfs_destroy().
72 struct drm_sysfs_class *drm_sysfs_create(struct module *owner, char *name)
74 struct drm_sysfs_class *cs;
77 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
82 memset(cs, 0x00, sizeof(*cs));
84 cs->class.name = name;
85 cs->class.class_release = drm_sysfs_class_release;
86 cs->class.release = release_simple_dev;
88 cs->attr.attr.name = "dev";
89 cs->attr.attr.mode = S_IRUGO;
90 cs->attr.attr.owner = owner;
91 cs->attr.show = show_dev;
92 cs->attr.store = NULL;
94 retval = class_register(&cs->class);
97 class_create_file(&cs->class, &class_attr_version);
103 return ERR_PTR(retval);
107 * drm_sysfs_destroy - destroys a struct drm_sysfs_class structure
108 * @cs: pointer to the struct drm_sysfs_class that is to be destroyed
110 * Note, the pointer to be destroyed must have been created with a call to
111 * drm_sysfs_create().
113 void drm_sysfs_destroy(struct drm_sysfs_class *cs)
115 if ((cs == NULL) || (IS_ERR(cs)))
118 class_unregister(&cs->class);
121 static ssize_t show_dri(struct class_device *class_device, char *buf)
123 drm_device_t * dev = ((drm_head_t *)class_get_devdata(class_device))->dev;
124 if (dev->driver->dri_library_name)
125 return dev->driver->dri_library_name(dev, buf);
126 return snprintf(buf, PAGE_SIZE, "%s\n", dev->driver->pci_driver.name);
129 static struct class_device_attribute class_device_attrs[] = {
130 __ATTR(dri_library_name, S_IRUGO, show_dri, NULL),
134 * drm_sysfs_device_add - adds a class device to sysfs for a character driver
135 * @cs: pointer to the struct drm_sysfs_class that this device should be registered to.
136 * @dev: the dev_t for the device to be added.
137 * @device: a pointer to a struct device that is assiociated with this class device.
138 * @fmt: string for the class device's name
140 * A struct class_device will be created in sysfs, registered to the specified
141 * class. A "dev" file will be created, showing the dev_t for the device. The
142 * pointer to the struct class_device will be returned from the call. Any further
143 * sysfs files that might be required can be created using this pointer.
144 * Note: the struct drm_sysfs_class passed to this function must have previously been
145 * created with a call to drm_sysfs_create().
147 struct class_device *drm_sysfs_device_add(struct drm_sysfs_class *cs,
150 struct simple_dev *s_dev = NULL;
153 if ((cs == NULL) || (IS_ERR(cs))) {
158 s_dev = kmalloc(sizeof(*s_dev), GFP_KERNEL);
163 memset(s_dev, 0x00, sizeof(*s_dev));
165 s_dev->dev = MKDEV(DRM_MAJOR, head->minor);
166 s_dev->class_dev.dev = &(head->dev->pdev)->dev;
167 s_dev->class_dev.class = &cs->class;
169 snprintf(s_dev->class_dev.class_id, BUS_ID_SIZE, "card%d", head->minor);
170 retval = class_device_register(&s_dev->class_dev);
174 class_device_create_file(&s_dev->class_dev, &cs->attr);
175 class_set_devdata(&s_dev->class_dev, head);
177 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
178 class_device_create_file(&s_dev->class_dev, &class_device_attrs[i]);
179 return &s_dev->class_dev;
183 return ERR_PTR(retval);
187 * drm_sysfs_device_remove - removes a class device that was created with drm_sysfs_device_add()
188 * @dev: the dev_t of the device that was previously registered.
190 * This call unregisters and cleans up a class device that was created with a
191 * call to drm_sysfs_device_add()
193 void drm_sysfs_device_remove(struct class_device *class_dev)
195 struct simple_dev *s_dev = to_simple_dev(class_dev);
198 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
199 class_device_remove_file(&s_dev->class_dev, &class_device_attrs[i]);
200 class_device_unregister(&s_dev->class_dev);