blob: 05ed5043254fa4212fc6b36475902516b9c0954a [file] [log] [blame]
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -08001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * drm_sysfs.c - Modifications to drm_sysfs_class.c to support
4 * extra sysfs attribute from DRM. Normal drm_sysfs_class
5 * does not allow adding attributes.
6 *
7 * Copyright (c) 2004 Jon Smirl <jonsmirl@gmail.com>
8 * Copyright (c) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
9 * Copyright (c) 2003-2004 IBM Corp.
10 *
11 * This file is released under the GPLv2
12 *
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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
Jesse Barnese8b962b2007-11-22 14:02:38 +100022#define to_drm_device(d) container_of(d, struct drm_device, dev)
23
24/**
25 * drm_sysfs_suspend - DRM class suspend hook
26 * @dev: Linux device to suspend
27 * @state: power state to enter
28 *
29 * Just figures out what the actual struct drm_device associated with
30 * @dev is and calls its suspend hook, if present.
31 */
32static int drm_sysfs_suspend(struct device *dev, pm_message_t state)
33{
34 struct drm_device *drm_dev = to_drm_device(dev);
35
36 printk(KERN_ERR "%s\n", __FUNCTION__);
37
38 if (drm_dev->driver->suspend)
Dave Airlieb932ccb2008-02-20 10:02:20 +100039 return drm_dev->driver->suspend(drm_dev, state);
Jesse Barnese8b962b2007-11-22 14:02:38 +100040
41 return 0;
42}
43
44/**
45 * drm_sysfs_resume - DRM class resume hook
46 * @dev: Linux device to resume
47 *
48 * Just figures out what the actual struct drm_device associated with
49 * @dev is and calls its resume hook, if present.
50 */
51static int drm_sysfs_resume(struct device *dev)
52{
53 struct drm_device *drm_dev = to_drm_device(dev);
54
55 if (drm_dev->driver->resume)
56 return drm_dev->driver->resume(drm_dev);
57
58 return 0;
59}
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/* Display the version of drm_core. This doesn't work right in current design */
62static ssize_t version_show(struct class *dev, char *buf)
63{
64 return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR,
65 CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
66}
67
68static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
69
70/**
71 * drm_sysfs_create - create a struct drm_sysfs_class structure
72 * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
73 * @name: pointer to a string for the name of this class.
74 *
Jesse Barnese8b962b2007-11-22 14:02:38 +100075 * This is used to create DRM class pointer that can then be used
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * in calls to drm_sysfs_device_add().
77 *
78 * Note, the pointer created here is to be destroyed when finished by making a
79 * call to drm_sysfs_destroy().
80 */
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -080081struct class *drm_sysfs_create(struct module *owner, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -080083 struct class *class;
Jeff Garzik24f73c92006-10-10 14:23:37 -070084 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -080086 class = class_create(owner, name);
Akinobu Mita94f060b2006-12-09 10:49:47 +110087 if (IS_ERR(class)) {
88 err = PTR_ERR(class);
Jeff Garzik24f73c92006-10-10 14:23:37 -070089 goto err_out;
90 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Jesse Barnese8b962b2007-11-22 14:02:38 +100092 class->suspend = drm_sysfs_suspend;
93 class->resume = drm_sysfs_resume;
94
Jeff Garzik24f73c92006-10-10 14:23:37 -070095 err = class_create_file(class, &class_attr_version);
96 if (err)
97 goto err_out_class;
98
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -080099 return class;
Jeff Garzik24f73c92006-10-10 14:23:37 -0700100
101err_out_class:
102 class_destroy(class);
103err_out:
104 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107/**
Jesse Barnese8b962b2007-11-22 14:02:38 +1000108 * drm_sysfs_destroy - destroys DRM class
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *
Jesse Barnese8b962b2007-11-22 14:02:38 +1000110 * Destroy the DRM device class.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
Jesse Barnese8b962b2007-11-22 14:02:38 +1000112void drm_sysfs_destroy(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Jesse Barnese8b962b2007-11-22 14:02:38 +1000114 if ((drm_class == NULL) || (IS_ERR(drm_class)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return;
Jesse Barnese8b962b2007-11-22 14:02:38 +1000116 class_remove_file(drm_class, &class_attr_version);
117 class_destroy(drm_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Jesse Barnese8b962b2007-11-22 14:02:38 +1000120static ssize_t show_dri(struct device *device, struct device_attribute *attr,
121 char *buf)
Dave Airlie732052e2005-11-11 22:07:35 +1100122{
Jesse Barnese8b962b2007-11-22 14:02:38 +1000123 struct drm_device *dev = to_drm_device(device);
Dave Airlie732052e2005-11-11 22:07:35 +1100124 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
Jesse Barnese8b962b2007-11-22 14:02:38 +1000129static struct device_attribute device_attrs[] = {
Dave Airlie732052e2005-11-11 22:07:35 +1100130 __ATTR(dri_library_name, S_IRUGO, show_dri, NULL),
131};
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/**
Jesse Barnese8b962b2007-11-22 14:02:38 +1000134 * drm_sysfs_device_release - do nothing
135 * @dev: Linux device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 *
Jesse Barnese8b962b2007-11-22 14:02:38 +1000137 * Normally, this would free the DRM device associated with @dev, along
138 * with cleaning up any other stuff. But we do that in the DRM core, so
139 * this function can just return and hope that the core does its job.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
Jesse Barnese8b962b2007-11-22 14:02:38 +1000141static void drm_sysfs_device_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Jesse Barnese8b962b2007-11-22 14:02:38 +1000143 return;
144}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Jesse Barnese8b962b2007-11-22 14:02:38 +1000146/**
147 * drm_sysfs_device_add - adds a class device to sysfs for a character driver
148 * @dev: DRM device to be added
149 * @head: DRM head in question
150 *
151 * Add a DRM device to the DRM's device model class. We use @dev's PCI device
152 * as the parent for the Linux device, and make sure it has a file containing
153 * the driver we're using (for userspace compatibility).
154 */
155int drm_sysfs_device_add(struct drm_device *dev, struct drm_head *head)
156{
157 int err;
158 int i, j;
159
160 dev->dev.parent = &dev->pdev->dev;
161 dev->dev.class = drm_class;
162 dev->dev.release = drm_sysfs_device_release;
Dave Airlie77e27e92007-11-29 09:48:20 +1000163 dev->dev.devt = head->device;
Jesse Barnese8b962b2007-11-22 14:02:38 +1000164 snprintf(dev->dev.bus_id, BUS_ID_SIZE, "card%d", head->minor);
165
166 err = device_register(&dev->dev);
167 if (err) {
168 DRM_ERROR("device add failed: %d\n", err);
Jeff Garzik24f73c92006-10-10 14:23:37 -0700169 goto err_out;
170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Jesse Barnese8b962b2007-11-22 14:02:38 +1000172 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
173 err = device_create_file(&dev->dev, &device_attrs[i]);
Jeff Garzik24f73c92006-10-10 14:23:37 -0700174 if (err)
175 goto err_out_files;
176 }
177
Jesse Barnese8b962b2007-11-22 14:02:38 +1000178 return 0;
Jeff Garzik24f73c92006-10-10 14:23:37 -0700179
180err_out_files:
181 if (i > 0)
182 for (j = 0; j < i; j++)
Jesse Barnese8b962b2007-11-22 14:02:38 +1000183 device_remove_file(&dev->dev, &device_attrs[i]);
184 device_unregister(&dev->dev);
Jeff Garzik24f73c92006-10-10 14:23:37 -0700185err_out:
Jesse Barnese8b962b2007-11-22 14:02:38 +1000186
187 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190/**
Jesse Barnese8b962b2007-11-22 14:02:38 +1000191 * drm_sysfs_device_remove - remove DRM device
192 * @dev: DRM device to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 *
194 * This call unregisters and cleans up a class device that was created with a
195 * call to drm_sysfs_device_add()
196 */
Jesse Barnese8b962b2007-11-22 14:02:38 +1000197void drm_sysfs_device_remove(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Dave Airlie732052e2005-11-11 22:07:35 +1100199 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Jesse Barnese8b962b2007-11-22 14:02:38 +1000201 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
202 device_remove_file(&dev->dev, &device_attrs[i]);
203 device_unregister(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}