Greg Kroah-Hartman | 0650fd5 | 2006-01-20 14:08:59 -0800 | [diff] [blame] | 1 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/device.h> |
| 16 | #include <linux/kdev_t.h> |
| 17 | #include <linux/err.h> |
| 18 | |
| 19 | #include "drm_core.h" |
Dave Airlie | f210973 | 2005-09-05 21:33:44 +1000 | [diff] [blame] | 20 | #include "drmP.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 22 | #define to_drm_minor(d) container_of(d, struct drm_minor, kdev) |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 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 | */ |
| 32 | static int drm_sysfs_suspend(struct device *dev, pm_message_t state) |
| 33 | { |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 34 | struct drm_minor *drm_minor = to_drm_minor(dev); |
| 35 | struct drm_device *drm_dev = drm_minor->dev; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 36 | |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 37 | printk(KERN_ERR "%s\n", __func__); |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 38 | |
| 39 | if (drm_dev->driver->suspend) |
Dave Airlie | b932ccb | 2008-02-20 10:02:20 +1000 | [diff] [blame] | 40 | return drm_dev->driver->suspend(drm_dev, state); |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * drm_sysfs_resume - DRM class resume hook |
| 47 | * @dev: Linux device to resume |
| 48 | * |
| 49 | * Just figures out what the actual struct drm_device associated with |
| 50 | * @dev is and calls its resume hook, if present. |
| 51 | */ |
| 52 | static int drm_sysfs_resume(struct device *dev) |
| 53 | { |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 54 | struct drm_minor *drm_minor = to_drm_minor(dev); |
| 55 | struct drm_device *drm_dev = drm_minor->dev; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 56 | |
| 57 | if (drm_dev->driver->resume) |
| 58 | return drm_dev->driver->resume(drm_dev); |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | /* Display the version of drm_core. This doesn't work right in current design */ |
| 64 | static ssize_t version_show(struct class *dev, char *buf) |
| 65 | { |
| 66 | return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR, |
| 67 | CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE); |
| 68 | } |
| 69 | |
| 70 | static CLASS_ATTR(version, S_IRUGO, version_show, NULL); |
| 71 | |
| 72 | /** |
| 73 | * drm_sysfs_create - create a struct drm_sysfs_class structure |
| 74 | * @owner: pointer to the module that is to "own" this struct drm_sysfs_class |
| 75 | * @name: pointer to a string for the name of this class. |
| 76 | * |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 77 | * This is used to create DRM class pointer that can then be used |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | * in calls to drm_sysfs_device_add(). |
| 79 | * |
| 80 | * Note, the pointer created here is to be destroyed when finished by making a |
| 81 | * call to drm_sysfs_destroy(). |
| 82 | */ |
Greg Kroah-Hartman | 0650fd5 | 2006-01-20 14:08:59 -0800 | [diff] [blame] | 83 | struct class *drm_sysfs_create(struct module *owner, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
Greg Kroah-Hartman | 0650fd5 | 2006-01-20 14:08:59 -0800 | [diff] [blame] | 85 | struct class *class; |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 86 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Greg Kroah-Hartman | 0650fd5 | 2006-01-20 14:08:59 -0800 | [diff] [blame] | 88 | class = class_create(owner, name); |
Akinobu Mita | 94f060b | 2006-12-09 10:49:47 +1100 | [diff] [blame] | 89 | if (IS_ERR(class)) { |
| 90 | err = PTR_ERR(class); |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 91 | goto err_out; |
| 92 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 94 | class->suspend = drm_sysfs_suspend; |
| 95 | class->resume = drm_sysfs_resume; |
| 96 | |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 97 | err = class_create_file(class, &class_attr_version); |
| 98 | if (err) |
| 99 | goto err_out_class; |
| 100 | |
Greg Kroah-Hartman | 0650fd5 | 2006-01-20 14:08:59 -0800 | [diff] [blame] | 101 | return class; |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 102 | |
| 103 | err_out_class: |
| 104 | class_destroy(class); |
| 105 | err_out: |
| 106 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 110 | * drm_sysfs_destroy - destroys DRM class |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | * |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 112 | * Destroy the DRM device class. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | */ |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 114 | void drm_sysfs_destroy(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 116 | if ((drm_class == NULL) || (IS_ERR(drm_class))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 118 | class_remove_file(drm_class, &class_attr_version); |
| 119 | class_destroy(drm_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 122 | static ssize_t show_dri(struct device *device, struct device_attribute *attr, |
| 123 | char *buf) |
Dave Airlie | 732052e | 2005-11-11 22:07:35 +1100 | [diff] [blame] | 124 | { |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 125 | struct drm_minor *drm_minor = to_drm_minor(device); |
| 126 | struct drm_device *drm_dev = drm_minor->dev; |
| 127 | if (drm_dev->driver->dri_library_name) |
| 128 | return drm_dev->driver->dri_library_name(drm_dev, buf); |
| 129 | return snprintf(buf, PAGE_SIZE, "%s\n", drm_dev->driver->pci_driver.name); |
Dave Airlie | 732052e | 2005-11-11 22:07:35 +1100 | [diff] [blame] | 130 | } |
| 131 | |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 132 | static struct device_attribute device_attrs[] = { |
Dave Airlie | 732052e | 2005-11-11 22:07:35 +1100 | [diff] [blame] | 133 | __ATTR(dri_library_name, S_IRUGO, show_dri, NULL), |
| 134 | }; |
| 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | /** |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 137 | * drm_sysfs_device_release - do nothing |
| 138 | * @dev: Linux device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | * |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 140 | * Normally, this would free the DRM device associated with @dev, along |
| 141 | * with cleaning up any other stuff. But we do that in the DRM core, so |
| 142 | * this function can just return and hope that the core does its job. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | */ |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 144 | static void drm_sysfs_device_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 146 | return; |
| 147 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 149 | /** |
| 150 | * drm_sysfs_device_add - adds a class device to sysfs for a character driver |
| 151 | * @dev: DRM device to be added |
| 152 | * @head: DRM head in question |
| 153 | * |
| 154 | * Add a DRM device to the DRM's device model class. We use @dev's PCI device |
| 155 | * as the parent for the Linux device, and make sure it has a file containing |
| 156 | * the driver we're using (for userspace compatibility). |
| 157 | */ |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 158 | int drm_sysfs_device_add(struct drm_minor *minor) |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 159 | { |
| 160 | int err; |
| 161 | int i, j; |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 162 | char *minor_str; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 163 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 164 | minor->kdev.parent = &minor->dev->pdev->dev; |
| 165 | minor->kdev.class = drm_class; |
| 166 | minor->kdev.release = drm_sysfs_device_release; |
| 167 | minor->kdev.devt = minor->device; |
| 168 | minor_str = "card%d"; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 169 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 170 | snprintf(minor->kdev.bus_id, BUS_ID_SIZE, minor_str, minor->index); |
| 171 | |
| 172 | err = device_register(&minor->kdev); |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 173 | if (err) { |
| 174 | DRM_ERROR("device add failed: %d\n", err); |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 175 | goto err_out; |
| 176 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 178 | for (i = 0; i < ARRAY_SIZE(device_attrs); i++) { |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 179 | err = device_create_file(&minor->kdev, &device_attrs[i]); |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 180 | if (err) |
| 181 | goto err_out_files; |
| 182 | } |
| 183 | |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 184 | return 0; |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 185 | |
| 186 | err_out_files: |
| 187 | if (i > 0) |
| 188 | for (j = 0; j < i; j++) |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 189 | device_remove_file(&minor->kdev, &device_attrs[i]); |
| 190 | device_unregister(&minor->kdev); |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 191 | err_out: |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 192 | |
| 193 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 197 | * drm_sysfs_device_remove - remove DRM device |
| 198 | * @dev: DRM device to remove |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | * |
| 200 | * This call unregisters and cleans up a class device that was created with a |
| 201 | * call to drm_sysfs_device_add() |
| 202 | */ |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 203 | void drm_sysfs_device_remove(struct drm_minor *minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
Dave Airlie | 732052e | 2005-11-11 22:07:35 +1100 | [diff] [blame] | 205 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 207 | for (i = 0; i < ARRAY_SIZE(device_attrs); i++) |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 208 | device_remove_file(&minor->kdev, &device_attrs[i]); |
| 209 | device_unregister(&minor->kdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |