drm_sysfs.c 5.3 KB
Newer Older
1

L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * drm_sysfs.c - Modifications to drm_sysfs_class.c to support
 *               extra sysfs attribute from DRM. Normal drm_sysfs_class
 *               does not allow adding attributes.
 *
 * Copyright (c) 2004 Jon Smirl <jonsmirl@gmail.com>
 * Copyright (c) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
 * Copyright (c) 2003-2004 IBM Corp.
 *
 * This file is released under the GPLv2
 *
 */

#include <linux/device.h>
#include <linux/kdev_t.h>
#include <linux/err.h>

#include "drm_core.h"
D
Dave Airlie 已提交
20
#include "drmP.h"
L
Linus Torvalds 已提交
21

22
#define to_drm_minor(d) container_of(d, struct drm_minor, kdev)
J
Jesse Barnes 已提交
23 24 25 26 27 28 29 30 31 32 33

/**
 * drm_sysfs_suspend - DRM class suspend hook
 * @dev: Linux device to suspend
 * @state: power state to enter
 *
 * Just figures out what the actual struct drm_device associated with
 * @dev is and calls its suspend hook, if present.
 */
static int drm_sysfs_suspend(struct device *dev, pm_message_t state)
{
34 35
	struct drm_minor *drm_minor = to_drm_minor(dev);
	struct drm_device *drm_dev = drm_minor->dev;
J
Jesse Barnes 已提交
36 37

	if (drm_dev->driver->suspend)
38
		return drm_dev->driver->suspend(drm_dev, state);
J
Jesse Barnes 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51

	return 0;
}

/**
 * drm_sysfs_resume - DRM class resume hook
 * @dev: Linux device to resume
 *
 * Just figures out what the actual struct drm_device associated with
 * @dev is and calls its resume hook, if present.
 */
static int drm_sysfs_resume(struct device *dev)
{
52 53
	struct drm_minor *drm_minor = to_drm_minor(dev);
	struct drm_device *drm_dev = drm_minor->dev;
J
Jesse Barnes 已提交
54 55 56 57 58 59 60

	if (drm_dev->driver->resume)
		return drm_dev->driver->resume(drm_dev);

	return 0;
}

L
Linus Torvalds 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74
/* Display the version of drm_core. This doesn't work right in current design */
static ssize_t version_show(struct class *dev, char *buf)
{
	return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR,
		       CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
}

static CLASS_ATTR(version, S_IRUGO, version_show, NULL);

/**
 * drm_sysfs_create - create a struct drm_sysfs_class structure
 * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
 * @name: pointer to a string for the name of this class.
 *
J
Jesse Barnes 已提交
75
 * This is used to create DRM class pointer that can then be used
L
Linus Torvalds 已提交
76 77 78 79 80
 * in calls to drm_sysfs_device_add().
 *
 * Note, the pointer created here is to be destroyed when finished by making a
 * call to drm_sysfs_destroy().
 */
81
struct class *drm_sysfs_create(struct module *owner, char *name)
L
Linus Torvalds 已提交
82
{
83
	struct class *class;
84
	int err;
85 86

	class = class_create(owner, name);
A
Akinobu Mita 已提交
87 88
	if (IS_ERR(class)) {
		err = PTR_ERR(class);
89 90 91
		goto err_out;
	}

J
Jesse Barnes 已提交
92 93 94
	class->suspend = drm_sysfs_suspend;
	class->resume = drm_sysfs_resume;

95 96 97
	err = class_create_file(class, &class_attr_version);
	if (err)
		goto err_out_class;
98 99

	return class;
100 101 102 103 104

err_out_class:
	class_destroy(class);
err_out:
	return ERR_PTR(err);
L
Linus Torvalds 已提交
105 106 107
}

/**
J
Jesse Barnes 已提交
108
 * drm_sysfs_destroy - destroys DRM class
L
Linus Torvalds 已提交
109
 *
J
Jesse Barnes 已提交
110
 * Destroy the DRM device class.
L
Linus Torvalds 已提交
111
 */
J
Jesse Barnes 已提交
112
void drm_sysfs_destroy(void)
L
Linus Torvalds 已提交
113
{
J
Jesse Barnes 已提交
114
	if ((drm_class == NULL) || (IS_ERR(drm_class)))
L
Linus Torvalds 已提交
115
		return;
J
Jesse Barnes 已提交
116 117
	class_remove_file(drm_class, &class_attr_version);
	class_destroy(drm_class);
L
Linus Torvalds 已提交
118 119
}

J
Jesse Barnes 已提交
120 121
static ssize_t show_dri(struct device *device, struct device_attribute *attr,
			char *buf)
D
Dave Airlie 已提交
122
{
123 124 125 126 127
	struct drm_minor *drm_minor = to_drm_minor(device);
	struct drm_device *drm_dev = drm_minor->dev;
	if (drm_dev->driver->dri_library_name)
		return drm_dev->driver->dri_library_name(drm_dev, buf);
	return snprintf(buf, PAGE_SIZE, "%s\n", drm_dev->driver->pci_driver.name);
D
Dave Airlie 已提交
128 129
}

J
Jesse Barnes 已提交
130
static struct device_attribute device_attrs[] = {
D
Dave Airlie 已提交
131 132 133
	__ATTR(dri_library_name, S_IRUGO, show_dri, NULL),
};

J
Jesse Barnes 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146
/**
 * drm_sysfs_device_release - do nothing
 * @dev: Linux device
 *
 * Normally, this would free the DRM device associated with @dev, along
 * with cleaning up any other stuff.  But we do that in the DRM core, so
 * this function can just return and hope that the core does its job.
 */
static void drm_sysfs_device_release(struct device *dev)
{
	return;
}

L
Linus Torvalds 已提交
147 148
/**
 * drm_sysfs_device_add - adds a class device to sysfs for a character driver
J
Jesse Barnes 已提交
149 150
 * @dev: DRM device to be added
 * @head: DRM head in question
L
Linus Torvalds 已提交
151
 *
J
Jesse Barnes 已提交
152 153 154
 * Add a DRM device to the DRM's device model class.  We use @dev's PCI device
 * as the parent for the Linux device, and make sure it has a file containing
 * the driver we're using (for userspace compatibility).
L
Linus Torvalds 已提交
155
 */
156
int drm_sysfs_device_add(struct drm_minor *minor)
L
Linus Torvalds 已提交
157
{
J
Jesse Barnes 已提交
158 159
	int err;
	int i, j;
160
	char *minor_str;
J
Jesse Barnes 已提交
161

162 163 164 165 166
	minor->kdev.parent = &minor->dev->pdev->dev;
	minor->kdev.class = drm_class;
	minor->kdev.release = drm_sysfs_device_release;
	minor->kdev.devt = minor->device;
	minor_str = "card%d";
J
Jesse Barnes 已提交
167

168 169 170
	snprintf(minor->kdev.bus_id, BUS_ID_SIZE, minor_str, minor->index);

	err = device_register(&minor->kdev);
J
Jesse Barnes 已提交
171 172
	if (err) {
		DRM_ERROR("device add failed: %d\n", err);
173 174
		goto err_out;
	}
L
Linus Torvalds 已提交
175

J
Jesse Barnes 已提交
176
	for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
177
		err = device_create_file(&minor->kdev, &device_attrs[i]);
178 179 180 181
		if (err)
			goto err_out_files;
	}

J
Jesse Barnes 已提交
182
	return 0;
183 184 185 186

err_out_files:
	if (i > 0)
		for (j = 0; j < i; j++)
187 188
			device_remove_file(&minor->kdev, &device_attrs[i]);
	device_unregister(&minor->kdev);
189
err_out:
J
Jesse Barnes 已提交
190 191

	return err;
L
Linus Torvalds 已提交
192 193 194
}

/**
J
Jesse Barnes 已提交
195 196
 * drm_sysfs_device_remove - remove DRM device
 * @dev: DRM device to remove
L
Linus Torvalds 已提交
197 198 199 200
 *
 * This call unregisters and cleans up a class device that was created with a
 * call to drm_sysfs_device_add()
 */
201
void drm_sysfs_device_remove(struct drm_minor *minor)
L
Linus Torvalds 已提交
202
{
D
Dave Airlie 已提交
203 204
	int i;

J
Jesse Barnes 已提交
205
	for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
206 207
		device_remove_file(&minor->kdev, &device_attrs[i]);
	device_unregister(&minor->kdev);
L
Linus Torvalds 已提交
208
}