blob: 68e43ddc16aea44fd927cbc9ae6eb532f7d7eb2a [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>
18
19#include "drm_core.h"
Dave Airlief2109732005-09-05 21:33:44 +100020#include "drmP.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22struct 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
28struct simple_dev {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 dev_t dev;
30 struct class_device class_dev;
31};
32#define to_simple_dev(d) container_of(d, struct simple_dev, class_dev)
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static void release_simple_dev(struct class_device *class_dev)
35{
36 struct simple_dev *s_dev = to_simple_dev(class_dev);
37 kfree(s_dev);
38}
39
40static ssize_t show_dev(struct class_device *class_dev, char *buf)
41{
42 struct simple_dev *s_dev = to_simple_dev(class_dev);
43 return print_dev_t(buf, s_dev->dev);
44}
45
46static void drm_sysfs_class_release(struct class *class)
47{
48 struct drm_sysfs_class *cs = to_drm_sysfs_class(class);
49 kfree(cs);
50}
51
52/* Display the version of drm_core. This doesn't work right in current design */
53static ssize_t version_show(struct class *dev, char *buf)
54{
55 return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR,
56 CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
57}
58
59static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
60
61/**
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.
65 *
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().
68 *
69 * Note, the pointer created here is to be destroyed when finished by making a
70 * call to drm_sysfs_destroy().
71 */
72struct drm_sysfs_class *drm_sysfs_create(struct module *owner, char *name)
73{
74 struct drm_sysfs_class *cs;
75 int retval;
76
77 cs = kmalloc(sizeof(*cs), GFP_KERNEL);
78 if (!cs) {
79 retval = -ENOMEM;
80 goto error;
81 }
82 memset(cs, 0x00, sizeof(*cs));
83
84 cs->class.name = name;
85 cs->class.class_release = drm_sysfs_class_release;
86 cs->class.release = release_simple_dev;
87
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;
93
94 retval = class_register(&cs->class);
95 if (retval)
96 goto error;
97 class_create_file(&cs->class, &class_attr_version);
98
99 return cs;
100
101 error:
102 kfree(cs);
103 return ERR_PTR(retval);
104}
105
106/**
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
109 *
110 * Note, the pointer to be destroyed must have been created with a call to
111 * drm_sysfs_create().
112 */
113void drm_sysfs_destroy(struct drm_sysfs_class *cs)
114{
115 if ((cs == NULL) || (IS_ERR(cs)))
116 return;
117
118 class_unregister(&cs->class);
119}
120
Dave Airlie732052e2005-11-11 22:07:35 +1100121static ssize_t show_dri(struct class_device *class_device, char *buf)
122{
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);
127}
128
129static struct class_device_attribute class_device_attrs[] = {
130 __ATTR(dri_library_name, S_IRUGO, show_dri, NULL),
131};
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/**
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
139 *
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().
146 */
Dave Airlie732052e2005-11-11 22:07:35 +1100147struct class_device *drm_sysfs_device_add(struct drm_sysfs_class *cs,
148 drm_head_t *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct simple_dev *s_dev = NULL;
Dave Airlie732052e2005-11-11 22:07:35 +1100151 int i, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 if ((cs == NULL) || (IS_ERR(cs))) {
154 retval = -ENODEV;
155 goto error;
156 }
157
158 s_dev = kmalloc(sizeof(*s_dev), GFP_KERNEL);
159 if (!s_dev) {
160 retval = -ENOMEM;
161 goto error;
162 }
163 memset(s_dev, 0x00, sizeof(*s_dev));
164
Dave Airlie732052e2005-11-11 22:07:35 +1100165 s_dev->dev = MKDEV(DRM_MAJOR, head->minor);
166 s_dev->class_dev.dev = &(head->dev->pdev)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 s_dev->class_dev.class = &cs->class;
168
Dave Airlie732052e2005-11-11 22:07:35 +1100169 snprintf(s_dev->class_dev.class_id, BUS_ID_SIZE, "card%d", head->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 retval = class_device_register(&s_dev->class_dev);
171 if (retval)
172 goto error;
173
174 class_device_create_file(&s_dev->class_dev, &cs->attr);
Dave Airlie732052e2005-11-11 22:07:35 +1100175 class_set_devdata(&s_dev->class_dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Dave Airlie732052e2005-11-11 22:07:35 +1100177 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
178 class_device_create_file(&s_dev->class_dev, &class_device_attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return &s_dev->class_dev;
180
Dave Airlie732052e2005-11-11 22:07:35 +1100181error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 kfree(s_dev);
183 return ERR_PTR(retval);
184}
185
186/**
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.
189 *
190 * This call unregisters and cleans up a class device that was created with a
191 * call to drm_sysfs_device_add()
192 */
Dave Airlie732052e2005-11-11 22:07:35 +1100193void drm_sysfs_device_remove(struct class_device *class_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Dave Airlie732052e2005-11-11 22:07:35 +1100195 struct simple_dev *s_dev = to_simple_dev(class_dev);
196 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Dave Airlie732052e2005-11-11 22:07:35 +1100198 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}