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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
Paul Gortmaker | 2d1a8a4 | 2011-08-30 18:16:33 -0400 | [diff] [blame] | 19 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 21 | #include <drm/drm_sysfs.h> |
| 22 | #include <drm/drm_core.h> |
| 23 | #include <drm/drmP.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 25 | #define to_drm_minor(d) container_of(d, struct drm_minor, kdev) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 26 | #define to_drm_connector(d) container_of(d, struct drm_connector, kdev) |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 27 | |
Thomas Hellstrom | 08e4d53 | 2009-08-20 19:02:31 +1000 | [diff] [blame] | 28 | static struct device_type drm_sysfs_device_minor = { |
| 29 | .name = "drm_minor" |
| 30 | }; |
| 31 | |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 32 | /** |
Shuah Khan | cf4b91f | 2013-07-01 16:06:02 -0600 | [diff] [blame] | 33 | * __drm_class_suspend - internal DRM class suspend routine |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 34 | * @dev: Linux device to suspend |
| 35 | * @state: power state to enter |
| 36 | * |
| 37 | * Just figures out what the actual struct drm_device associated with |
| 38 | * @dev is and calls its suspend hook, if present. |
| 39 | */ |
Shuah Khan | cf4b91f | 2013-07-01 16:06:02 -0600 | [diff] [blame] | 40 | static int __drm_class_suspend(struct device *dev, pm_message_t state) |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 41 | { |
Thomas Hellstrom | 08e4d53 | 2009-08-20 19:02:31 +1000 | [diff] [blame] | 42 | if (dev->type == &drm_sysfs_device_minor) { |
| 43 | struct drm_minor *drm_minor = to_drm_minor(dev); |
| 44 | struct drm_device *drm_dev = drm_minor->dev; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 45 | |
Thomas Hellstrom | 08e4d53 | 2009-08-20 19:02:31 +1000 | [diff] [blame] | 46 | if (drm_minor->type == DRM_MINOR_LEGACY && |
| 47 | !drm_core_check_feature(drm_dev, DRIVER_MODESET) && |
| 48 | drm_dev->driver->suspend) |
| 49 | return drm_dev->driver->suspend(drm_dev, state); |
| 50 | } |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | /** |
Shuah Khan | cf4b91f | 2013-07-01 16:06:02 -0600 | [diff] [blame] | 55 | * drm_class_suspend - internal DRM class suspend hook. Simply calls |
| 56 | * __drm_class_suspend() with the correct pm state. |
| 57 | * @dev: Linux device to suspend |
| 58 | */ |
| 59 | static int drm_class_suspend(struct device *dev) |
| 60 | { |
| 61 | return __drm_class_suspend(dev, PMSG_SUSPEND); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * drm_class_freeze - internal DRM class freeze hook. Simply calls |
| 66 | * __drm_class_suspend() with the correct pm state. |
| 67 | * @dev: Linux device to freeze |
| 68 | */ |
| 69 | static int drm_class_freeze(struct device *dev) |
| 70 | { |
| 71 | return __drm_class_suspend(dev, PMSG_FREEZE); |
| 72 | } |
| 73 | |
| 74 | /** |
Thomas Hellstrom | 08e4d53 | 2009-08-20 19:02:31 +1000 | [diff] [blame] | 75 | * drm_class_resume - DRM class resume hook |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 76 | * @dev: Linux device to resume |
| 77 | * |
| 78 | * Just figures out what the actual struct drm_device associated with |
| 79 | * @dev is and calls its resume hook, if present. |
| 80 | */ |
Thomas Hellstrom | 08e4d53 | 2009-08-20 19:02:31 +1000 | [diff] [blame] | 81 | static int drm_class_resume(struct device *dev) |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 82 | { |
Thomas Hellstrom | 08e4d53 | 2009-08-20 19:02:31 +1000 | [diff] [blame] | 83 | if (dev->type == &drm_sysfs_device_minor) { |
| 84 | struct drm_minor *drm_minor = to_drm_minor(dev); |
| 85 | struct drm_device *drm_dev = drm_minor->dev; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 86 | |
Thomas Hellstrom | 08e4d53 | 2009-08-20 19:02:31 +1000 | [diff] [blame] | 87 | if (drm_minor->type == DRM_MINOR_LEGACY && |
| 88 | !drm_core_check_feature(drm_dev, DRIVER_MODESET) && |
| 89 | drm_dev->driver->resume) |
| 90 | return drm_dev->driver->resume(drm_dev); |
| 91 | } |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | |
Shuah Khan | cf4b91f | 2013-07-01 16:06:02 -0600 | [diff] [blame] | 95 | static const struct dev_pm_ops drm_class_dev_pm_ops = { |
| 96 | .suspend = drm_class_suspend, |
| 97 | .resume = drm_class_resume, |
| 98 | .freeze = drm_class_freeze, |
| 99 | }; |
| 100 | |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 101 | static char *drm_devnode(struct device *dev, umode_t *mode) |
Kay Sievers | 02200d0 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 102 | { |
| 103 | return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev)); |
| 104 | } |
| 105 | |
Andi Kleen | 0933e2d | 2010-01-05 12:48:09 +0100 | [diff] [blame] | 106 | static CLASS_ATTR_STRING(version, S_IRUGO, |
| 107 | CORE_NAME " " |
| 108 | __stringify(CORE_MAJOR) "." |
| 109 | __stringify(CORE_MINOR) "." |
| 110 | __stringify(CORE_PATCHLEVEL) " " |
| 111 | CORE_DATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * drm_sysfs_create - create a struct drm_sysfs_class structure |
| 115 | * @owner: pointer to the module that is to "own" this struct drm_sysfs_class |
| 116 | * @name: pointer to a string for the name of this class. |
| 117 | * |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 118 | * 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] | 119 | * in calls to drm_sysfs_device_add(). |
| 120 | * |
| 121 | * Note, the pointer created here is to be destroyed when finished by making a |
| 122 | * call to drm_sysfs_destroy(). |
| 123 | */ |
Greg Kroah-Hartman | 0650fd5 | 2006-01-20 14:08:59 -0800 | [diff] [blame] | 124 | struct class *drm_sysfs_create(struct module *owner, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Greg Kroah-Hartman | 0650fd5 | 2006-01-20 14:08:59 -0800 | [diff] [blame] | 126 | struct class *class; |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 127 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Greg Kroah-Hartman | 0650fd5 | 2006-01-20 14:08:59 -0800 | [diff] [blame] | 129 | class = class_create(owner, name); |
Akinobu Mita | 94f060b | 2006-12-09 10:49:47 +1100 | [diff] [blame] | 130 | if (IS_ERR(class)) { |
| 131 | err = PTR_ERR(class); |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 132 | goto err_out; |
| 133 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Shuah Khan | cf4b91f | 2013-07-01 16:06:02 -0600 | [diff] [blame] | 135 | class->pm = &drm_class_dev_pm_ops; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 136 | |
Andi Kleen | 0933e2d | 2010-01-05 12:48:09 +0100 | [diff] [blame] | 137 | err = class_create_file(class, &class_attr_version.attr); |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 138 | if (err) |
| 139 | goto err_out_class; |
| 140 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 141 | class->devnode = drm_devnode; |
Kay Sievers | 02200d0 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 142 | |
Greg Kroah-Hartman | 0650fd5 | 2006-01-20 14:08:59 -0800 | [diff] [blame] | 143 | return class; |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 144 | |
| 145 | err_out_class: |
| 146 | class_destroy(class); |
| 147 | err_out: |
| 148 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 152 | * drm_sysfs_destroy - destroys DRM class |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | * |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 154 | * Destroy the DRM device class. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | */ |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 156 | void drm_sysfs_destroy(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 158 | if ((drm_class == NULL) || (IS_ERR(drm_class))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | return; |
Andi Kleen | 0933e2d | 2010-01-05 12:48:09 +0100 | [diff] [blame] | 160 | class_remove_file(drm_class, &class_attr_version.attr); |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 161 | class_destroy(drm_class); |
Dave Airlie | 49099c4 | 2012-07-06 18:06:42 +0100 | [diff] [blame] | 162 | drm_class = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 166 | * drm_sysfs_device_release - do nothing |
| 167 | * @dev: Linux device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | * |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 169 | * Normally, this would free the DRM device associated with @dev, along |
| 170 | * with cleaning up any other stuff. But we do that in the DRM core, so |
| 171 | * 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] | 172 | */ |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 173 | static void drm_sysfs_device_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Ma Ling | 77d26dc | 2009-04-16 17:51:25 +0800 | [diff] [blame] | 175 | memset(dev, 0, sizeof(struct device)); |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 176 | return; |
| 177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 179 | /* |
| 180 | * Connector properties |
| 181 | */ |
| 182 | static ssize_t status_show(struct device *device, |
| 183 | struct device_attribute *attr, |
| 184 | char *buf) |
| 185 | { |
| 186 | struct drm_connector *connector = to_drm_connector(device); |
| 187 | enum drm_connector_status status; |
Chris Wilson | 007c80a | 2011-03-15 11:40:00 +0000 | [diff] [blame] | 188 | int ret; |
| 189 | |
| 190 | ret = mutex_lock_interruptible(&connector->dev->mode_config.mutex); |
| 191 | if (ret) |
| 192 | return ret; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 193 | |
Chris Wilson | 7b334fc | 2010-09-09 23:51:02 +0100 | [diff] [blame] | 194 | status = connector->funcs->detect(connector, true); |
Chris Wilson | 007c80a | 2011-03-15 11:40:00 +0000 | [diff] [blame] | 195 | mutex_unlock(&connector->dev->mode_config.mutex); |
| 196 | |
Keith Packard | 75185c9 | 2009-05-30 20:42:25 -0700 | [diff] [blame] | 197 | return snprintf(buf, PAGE_SIZE, "%s\n", |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 198 | drm_get_connector_status_name(status)); |
| 199 | } |
| 200 | |
| 201 | static ssize_t dpms_show(struct device *device, |
| 202 | struct device_attribute *attr, |
| 203 | char *buf) |
| 204 | { |
| 205 | struct drm_connector *connector = to_drm_connector(device); |
| 206 | struct drm_device *dev = connector->dev; |
| 207 | uint64_t dpms_status; |
| 208 | int ret; |
| 209 | |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 210 | ret = drm_object_property_get_value(&connector->base, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 211 | dev->mode_config.dpms_property, |
| 212 | &dpms_status); |
| 213 | if (ret) |
| 214 | return 0; |
| 215 | |
Keith Packard | 75185c9 | 2009-05-30 20:42:25 -0700 | [diff] [blame] | 216 | return snprintf(buf, PAGE_SIZE, "%s\n", |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 217 | drm_get_dpms_name((int)dpms_status)); |
| 218 | } |
| 219 | |
| 220 | static ssize_t enabled_show(struct device *device, |
| 221 | struct device_attribute *attr, |
| 222 | char *buf) |
| 223 | { |
| 224 | struct drm_connector *connector = to_drm_connector(device); |
| 225 | |
Keith Packard | 75185c9 | 2009-05-30 20:42:25 -0700 | [diff] [blame] | 226 | return snprintf(buf, PAGE_SIZE, "%s\n", connector->encoder ? "enabled" : |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 227 | "disabled"); |
| 228 | } |
| 229 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 230 | static ssize_t edid_show(struct file *filp, struct kobject *kobj, |
| 231 | struct bin_attribute *attr, char *buf, loff_t off, |
| 232 | size_t count) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 233 | { |
| 234 | struct device *connector_dev = container_of(kobj, struct device, kobj); |
| 235 | struct drm_connector *connector = to_drm_connector(connector_dev); |
| 236 | unsigned char *edid; |
| 237 | size_t size; |
| 238 | |
| 239 | if (!connector->edid_blob_ptr) |
| 240 | return 0; |
| 241 | |
| 242 | edid = connector->edid_blob_ptr->data; |
| 243 | size = connector->edid_blob_ptr->length; |
| 244 | if (!edid) |
| 245 | return 0; |
| 246 | |
| 247 | if (off >= size) |
| 248 | return 0; |
| 249 | |
| 250 | if (off + count > size) |
| 251 | count = size - off; |
| 252 | memcpy(buf, edid + off, count); |
| 253 | |
| 254 | return count; |
| 255 | } |
| 256 | |
| 257 | static ssize_t modes_show(struct device *device, |
| 258 | struct device_attribute *attr, |
| 259 | char *buf) |
| 260 | { |
| 261 | struct drm_connector *connector = to_drm_connector(device); |
| 262 | struct drm_display_mode *mode; |
| 263 | int written = 0; |
| 264 | |
| 265 | list_for_each_entry(mode, &connector->modes, head) { |
| 266 | written += snprintf(buf + written, PAGE_SIZE - written, "%s\n", |
| 267 | mode->name); |
| 268 | } |
| 269 | |
| 270 | return written; |
| 271 | } |
| 272 | |
| 273 | static ssize_t subconnector_show(struct device *device, |
| 274 | struct device_attribute *attr, |
| 275 | char *buf) |
| 276 | { |
| 277 | struct drm_connector *connector = to_drm_connector(device); |
| 278 | struct drm_device *dev = connector->dev; |
| 279 | struct drm_property *prop = NULL; |
| 280 | uint64_t subconnector; |
| 281 | int is_tv = 0; |
| 282 | int ret; |
| 283 | |
| 284 | switch (connector->connector_type) { |
| 285 | case DRM_MODE_CONNECTOR_DVII: |
| 286 | prop = dev->mode_config.dvi_i_subconnector_property; |
| 287 | break; |
| 288 | case DRM_MODE_CONNECTOR_Composite: |
| 289 | case DRM_MODE_CONNECTOR_SVIDEO: |
| 290 | case DRM_MODE_CONNECTOR_Component: |
Francisco Jerez | 74bd3c2 | 2009-08-02 04:19:18 +0200 | [diff] [blame] | 291 | case DRM_MODE_CONNECTOR_TV: |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 292 | prop = dev->mode_config.tv_subconnector_property; |
| 293 | is_tv = 1; |
| 294 | break; |
| 295 | default: |
| 296 | DRM_ERROR("Wrong connector type for this property\n"); |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | if (!prop) { |
| 301 | DRM_ERROR("Unable to find subconnector property\n"); |
| 302 | return 0; |
| 303 | } |
| 304 | |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 305 | ret = drm_object_property_get_value(&connector->base, prop, &subconnector); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 306 | if (ret) |
| 307 | return 0; |
| 308 | |
| 309 | return snprintf(buf, PAGE_SIZE, "%s", is_tv ? |
| 310 | drm_get_tv_subconnector_name((int)subconnector) : |
| 311 | drm_get_dvi_i_subconnector_name((int)subconnector)); |
| 312 | } |
| 313 | |
| 314 | static ssize_t select_subconnector_show(struct device *device, |
| 315 | struct device_attribute *attr, |
| 316 | char *buf) |
| 317 | { |
| 318 | struct drm_connector *connector = to_drm_connector(device); |
| 319 | struct drm_device *dev = connector->dev; |
| 320 | struct drm_property *prop = NULL; |
| 321 | uint64_t subconnector; |
| 322 | int is_tv = 0; |
| 323 | int ret; |
| 324 | |
| 325 | switch (connector->connector_type) { |
| 326 | case DRM_MODE_CONNECTOR_DVII: |
| 327 | prop = dev->mode_config.dvi_i_select_subconnector_property; |
| 328 | break; |
| 329 | case DRM_MODE_CONNECTOR_Composite: |
| 330 | case DRM_MODE_CONNECTOR_SVIDEO: |
| 331 | case DRM_MODE_CONNECTOR_Component: |
Francisco Jerez | 74bd3c2 | 2009-08-02 04:19:18 +0200 | [diff] [blame] | 332 | case DRM_MODE_CONNECTOR_TV: |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 333 | prop = dev->mode_config.tv_select_subconnector_property; |
| 334 | is_tv = 1; |
| 335 | break; |
| 336 | default: |
| 337 | DRM_ERROR("Wrong connector type for this property\n"); |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | if (!prop) { |
| 342 | DRM_ERROR("Unable to find select subconnector property\n"); |
| 343 | return 0; |
| 344 | } |
| 345 | |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 346 | ret = drm_object_property_get_value(&connector->base, prop, &subconnector); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 347 | if (ret) |
| 348 | return 0; |
| 349 | |
| 350 | return snprintf(buf, PAGE_SIZE, "%s", is_tv ? |
| 351 | drm_get_tv_select_name((int)subconnector) : |
| 352 | drm_get_dvi_i_select_name((int)subconnector)); |
| 353 | } |
| 354 | |
| 355 | static struct device_attribute connector_attrs[] = { |
| 356 | __ATTR_RO(status), |
| 357 | __ATTR_RO(enabled), |
| 358 | __ATTR_RO(dpms), |
| 359 | __ATTR_RO(modes), |
| 360 | }; |
| 361 | |
| 362 | /* These attributes are for both DVI-I connectors and all types of tv-out. */ |
| 363 | static struct device_attribute connector_attrs_opt1[] = { |
| 364 | __ATTR_RO(subconnector), |
| 365 | __ATTR_RO(select_subconnector), |
| 366 | }; |
| 367 | |
| 368 | static struct bin_attribute edid_attr = { |
| 369 | .attr.name = "edid", |
Keith Packard | e36ebaf | 2009-05-30 20:42:26 -0700 | [diff] [blame] | 370 | .attr.mode = 0444, |
Adam Jackson | 7466f4c | 2010-03-29 21:43:23 +0000 | [diff] [blame] | 371 | .size = 0, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 372 | .read = edid_show, |
| 373 | }; |
| 374 | |
| 375 | /** |
Laurent Pinchart | 3b02ab8 | 2012-05-17 13:27:20 +0200 | [diff] [blame] | 376 | * drm_sysfs_connector_add - add a connector to sysfs |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 377 | * @connector: connector to add |
| 378 | * |
Laurent Pinchart | 3b02ab8 | 2012-05-17 13:27:20 +0200 | [diff] [blame] | 379 | * Create a connector device in sysfs, along with its associated connector |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 380 | * properties (so far, connection status, dpms, mode list & edid) and |
| 381 | * generate a hotplug event so userspace knows there's a new connector |
| 382 | * available. |
| 383 | * |
| 384 | * Note: |
Laurent Pinchart | 3b02ab8 | 2012-05-17 13:27:20 +0200 | [diff] [blame] | 385 | * This routine should only be called *once* for each registered connector. |
| 386 | * A second call for an already registered connector will trigger the BUG_ON |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 387 | * below. |
| 388 | */ |
| 389 | int drm_sysfs_connector_add(struct drm_connector *connector) |
| 390 | { |
| 391 | struct drm_device *dev = connector->dev; |
Dan Carpenter | a1c4560 | 2010-04-27 14:11:05 -0700 | [diff] [blame] | 392 | int attr_cnt = 0; |
| 393 | int opt_cnt = 0; |
| 394 | int i; |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 395 | int ret; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 396 | |
| 397 | /* We shouldn't get called more than once for the same connector */ |
| 398 | BUG_ON(device_is_registered(&connector->kdev)); |
| 399 | |
| 400 | connector->kdev.parent = &dev->primary->kdev; |
| 401 | connector->kdev.class = drm_class; |
| 402 | connector->kdev.release = drm_sysfs_device_release; |
| 403 | |
| 404 | DRM_DEBUG("adding \"%s\" to sysfs\n", |
| 405 | drm_get_connector_name(connector)); |
| 406 | |
Kay Sievers | 2ead054 | 2009-03-24 16:38:22 -0700 | [diff] [blame] | 407 | dev_set_name(&connector->kdev, "card%d-%s", |
| 408 | dev->primary->index, drm_get_connector_name(connector)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 409 | ret = device_register(&connector->kdev); |
| 410 | |
| 411 | if (ret) { |
| 412 | DRM_ERROR("failed to register connector device: %d\n", ret); |
| 413 | goto out; |
| 414 | } |
| 415 | |
| 416 | /* Standard attributes */ |
| 417 | |
Dan Carpenter | a1c4560 | 2010-04-27 14:11:05 -0700 | [diff] [blame] | 418 | for (attr_cnt = 0; attr_cnt < ARRAY_SIZE(connector_attrs); attr_cnt++) { |
| 419 | ret = device_create_file(&connector->kdev, &connector_attrs[attr_cnt]); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 420 | if (ret) |
| 421 | goto err_out_files; |
| 422 | } |
| 423 | |
| 424 | /* Optional attributes */ |
| 425 | /* |
| 426 | * In the long run it maybe a good idea to make one set of |
| 427 | * optionals per connector type. |
| 428 | */ |
| 429 | switch (connector->connector_type) { |
| 430 | case DRM_MODE_CONNECTOR_DVII: |
| 431 | case DRM_MODE_CONNECTOR_Composite: |
| 432 | case DRM_MODE_CONNECTOR_SVIDEO: |
| 433 | case DRM_MODE_CONNECTOR_Component: |
Francisco Jerez | 74bd3c2 | 2009-08-02 04:19:18 +0200 | [diff] [blame] | 434 | case DRM_MODE_CONNECTOR_TV: |
Dan Carpenter | a1c4560 | 2010-04-27 14:11:05 -0700 | [diff] [blame] | 435 | for (opt_cnt = 0; opt_cnt < ARRAY_SIZE(connector_attrs_opt1); opt_cnt++) { |
| 436 | ret = device_create_file(&connector->kdev, &connector_attrs_opt1[opt_cnt]); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 437 | if (ret) |
| 438 | goto err_out_files; |
| 439 | } |
| 440 | break; |
| 441 | default: |
| 442 | break; |
| 443 | } |
| 444 | |
| 445 | ret = sysfs_create_bin_file(&connector->kdev.kobj, &edid_attr); |
| 446 | if (ret) |
| 447 | goto err_out_files; |
| 448 | |
| 449 | /* Let userspace know we have a new connector */ |
| 450 | drm_sysfs_hotplug_event(dev); |
| 451 | |
| 452 | return 0; |
| 453 | |
| 454 | err_out_files: |
Dan Carpenter | a1c4560 | 2010-04-27 14:11:05 -0700 | [diff] [blame] | 455 | for (i = 0; i < opt_cnt; i++) |
| 456 | device_remove_file(&connector->kdev, &connector_attrs_opt1[i]); |
| 457 | for (i = 0; i < attr_cnt; i++) |
| 458 | device_remove_file(&connector->kdev, &connector_attrs[i]); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 459 | device_unregister(&connector->kdev); |
| 460 | |
| 461 | out: |
| 462 | return ret; |
| 463 | } |
| 464 | EXPORT_SYMBOL(drm_sysfs_connector_add); |
| 465 | |
| 466 | /** |
| 467 | * drm_sysfs_connector_remove - remove an connector device from sysfs |
| 468 | * @connector: connector to remove |
| 469 | * |
| 470 | * Remove @connector and its associated attributes from sysfs. Note that |
| 471 | * the device model core will take care of sending the "remove" uevent |
| 472 | * at this time, so we don't need to do it. |
| 473 | * |
| 474 | * Note: |
| 475 | * This routine should only be called if the connector was previously |
| 476 | * successfully registered. If @connector hasn't been registered yet, |
| 477 | * you'll likely see a panic somewhere deep in sysfs code when called. |
| 478 | */ |
| 479 | void drm_sysfs_connector_remove(struct drm_connector *connector) |
| 480 | { |
| 481 | int i; |
| 482 | |
Dave Airlie | 1828fe6 | 2012-02-20 14:15:02 +0000 | [diff] [blame] | 483 | if (!connector->kdev.parent) |
| 484 | return; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 485 | DRM_DEBUG("removing \"%s\" from sysfs\n", |
| 486 | drm_get_connector_name(connector)); |
| 487 | |
| 488 | for (i = 0; i < ARRAY_SIZE(connector_attrs); i++) |
| 489 | device_remove_file(&connector->kdev, &connector_attrs[i]); |
| 490 | sysfs_remove_bin_file(&connector->kdev.kobj, &edid_attr); |
| 491 | device_unregister(&connector->kdev); |
Dave Airlie | 1828fe6 | 2012-02-20 14:15:02 +0000 | [diff] [blame] | 492 | connector->kdev.parent = NULL; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 493 | } |
| 494 | EXPORT_SYMBOL(drm_sysfs_connector_remove); |
| 495 | |
| 496 | /** |
| 497 | * drm_sysfs_hotplug_event - generate a DRM uevent |
| 498 | * @dev: DRM device |
| 499 | * |
| 500 | * Send a uevent for the DRM device specified by @dev. Currently we only |
| 501 | * set HOTPLUG=1 in the uevent environment, but this could be expanded to |
| 502 | * deal with other types of events. |
| 503 | */ |
| 504 | void drm_sysfs_hotplug_event(struct drm_device *dev) |
| 505 | { |
| 506 | char *event_string = "HOTPLUG=1"; |
| 507 | char *envp[] = { event_string, NULL }; |
| 508 | |
| 509 | DRM_DEBUG("generating hotplug event\n"); |
| 510 | |
| 511 | kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, envp); |
| 512 | } |
Jesse Barnes | 5ca5828 | 2009-03-31 14:11:15 -0700 | [diff] [blame] | 513 | EXPORT_SYMBOL(drm_sysfs_hotplug_event); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 514 | |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 515 | /** |
| 516 | * drm_sysfs_device_add - adds a class device to sysfs for a character driver |
| 517 | * @dev: DRM device to be added |
| 518 | * @head: DRM head in question |
| 519 | * |
| 520 | * Add a DRM device to the DRM's device model class. We use @dev's PCI device |
| 521 | * as the parent for the Linux device, and make sure it has a file containing |
| 522 | * the driver we're using (for userspace compatibility). |
| 523 | */ |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 524 | int drm_sysfs_device_add(struct drm_minor *minor) |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 525 | { |
| 526 | int err; |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 527 | char *minor_str; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 528 | |
Jordan Crouse | dcdb167 | 2010-05-27 13:40:25 -0600 | [diff] [blame] | 529 | minor->kdev.parent = minor->dev->dev; |
| 530 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 531 | minor->kdev.class = drm_class; |
| 532 | minor->kdev.release = drm_sysfs_device_release; |
| 533 | minor->kdev.devt = minor->device; |
Thomas Hellstrom | 08e4d53 | 2009-08-20 19:02:31 +1000 | [diff] [blame] | 534 | minor->kdev.type = &drm_sysfs_device_minor; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 535 | if (minor->type == DRM_MINOR_CONTROL) |
| 536 | minor_str = "controlD%d"; |
| 537 | else if (minor->type == DRM_MINOR_RENDER) |
| 538 | minor_str = "renderD%d"; |
| 539 | else |
| 540 | minor_str = "card%d"; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 541 | |
Kay Sievers | 8f4bbd9 | 2009-01-06 10:44:41 -0800 | [diff] [blame] | 542 | dev_set_name(&minor->kdev, minor_str, minor->index); |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 543 | |
| 544 | err = device_register(&minor->kdev); |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 545 | if (err) { |
| 546 | DRM_ERROR("device add failed: %d\n", err); |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 547 | goto err_out; |
| 548 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 550 | return 0; |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 551 | |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 552 | err_out: |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 553 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /** |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 557 | * drm_sysfs_device_remove - remove DRM device |
| 558 | * @dev: DRM device to remove |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | * |
| 560 | * This call unregisters and cleans up a class device that was created with a |
| 561 | * call to drm_sysfs_device_add() |
| 562 | */ |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 563 | void drm_sysfs_device_remove(struct drm_minor *minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
Dave Airlie | 1828fe6 | 2012-02-20 14:15:02 +0000 | [diff] [blame] | 565 | if (minor->kdev.parent) |
| 566 | device_unregister(&minor->kdev); |
| 567 | minor->kdev.parent = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | } |
Thomas Hellstrom | 327c225 | 2009-08-17 16:28:37 +0200 | [diff] [blame] | 569 | |
| 570 | |
| 571 | /** |
| 572 | * drm_class_device_register - Register a struct device in the drm class. |
| 573 | * |
| 574 | * @dev: pointer to struct device to register. |
| 575 | * |
| 576 | * @dev should have all relevant members pre-filled with the exception |
| 577 | * of the class member. In particular, the device_type member must |
| 578 | * be set. |
| 579 | */ |
| 580 | |
| 581 | int drm_class_device_register(struct device *dev) |
| 582 | { |
Dave Airlie | 49099c4 | 2012-07-06 18:06:42 +0100 | [diff] [blame] | 583 | if (!drm_class || IS_ERR(drm_class)) |
| 584 | return -ENOENT; |
| 585 | |
Thomas Hellstrom | 327c225 | 2009-08-17 16:28:37 +0200 | [diff] [blame] | 586 | dev->class = drm_class; |
| 587 | return device_register(dev); |
| 588 | } |
| 589 | EXPORT_SYMBOL_GPL(drm_class_device_register); |
| 590 | |
| 591 | void drm_class_device_unregister(struct device *dev) |
| 592 | { |
| 593 | return device_unregister(dev); |
| 594 | } |
| 595 | EXPORT_SYMBOL_GPL(drm_class_device_unregister); |