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> |
Daniel Vetter | 67d0ec4 | 2014-09-10 12:43:53 +0200 | [diff] [blame] | 24 | #include "drm_internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Dave Airlie | 5bdebb1 | 2013-10-11 14:07:25 +1000 | [diff] [blame] | 26 | #define to_drm_minor(d) dev_get_drvdata(d) |
| 27 | #define to_drm_connector(d) dev_get_drvdata(d) |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 28 | |
Thomas Hellstrom | 08e4d53 | 2009-08-20 19:02:31 +1000 | [diff] [blame] | 29 | static struct device_type drm_sysfs_device_minor = { |
| 30 | .name = "drm_minor" |
| 31 | }; |
| 32 | |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 33 | /** |
Shuah Khan | cf4b91f | 2013-07-01 16:06:02 -0600 | [diff] [blame] | 34 | * __drm_class_suspend - internal DRM class suspend routine |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 35 | * @dev: Linux device to suspend |
| 36 | * @state: power state to enter |
| 37 | * |
| 38 | * Just figures out what the actual struct drm_device associated with |
| 39 | * @dev is and calls its suspend hook, if present. |
| 40 | */ |
Shuah Khan | cf4b91f | 2013-07-01 16:06:02 -0600 | [diff] [blame] | 41 | static int __drm_class_suspend(struct device *dev, pm_message_t state) |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 42 | { |
Thomas Hellstrom | 08e4d53 | 2009-08-20 19:02:31 +1000 | [diff] [blame] | 43 | if (dev->type == &drm_sysfs_device_minor) { |
| 44 | struct drm_minor *drm_minor = to_drm_minor(dev); |
| 45 | struct drm_device *drm_dev = drm_minor->dev; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 46 | |
Thomas Hellstrom | 08e4d53 | 2009-08-20 19:02:31 +1000 | [diff] [blame] | 47 | if (drm_minor->type == DRM_MINOR_LEGACY && |
| 48 | !drm_core_check_feature(drm_dev, DRIVER_MODESET) && |
| 49 | drm_dev->driver->suspend) |
| 50 | return drm_dev->driver->suspend(drm_dev, state); |
| 51 | } |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | /** |
Shuah Khan | cf4b91f | 2013-07-01 16:06:02 -0600 | [diff] [blame] | 56 | * drm_class_suspend - internal DRM class suspend hook. Simply calls |
| 57 | * __drm_class_suspend() with the correct pm state. |
| 58 | * @dev: Linux device to suspend |
| 59 | */ |
| 60 | static int drm_class_suspend(struct device *dev) |
| 61 | { |
| 62 | return __drm_class_suspend(dev, PMSG_SUSPEND); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * drm_class_freeze - internal DRM class freeze hook. Simply calls |
| 67 | * __drm_class_suspend() with the correct pm state. |
| 68 | * @dev: Linux device to freeze |
| 69 | */ |
| 70 | static int drm_class_freeze(struct device *dev) |
| 71 | { |
| 72 | return __drm_class_suspend(dev, PMSG_FREEZE); |
| 73 | } |
| 74 | |
| 75 | /** |
Thomas Hellstrom | 08e4d53 | 2009-08-20 19:02:31 +1000 | [diff] [blame] | 76 | * drm_class_resume - DRM class resume hook |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 77 | * @dev: Linux device to resume |
| 78 | * |
| 79 | * Just figures out what the actual struct drm_device associated with |
| 80 | * @dev is and calls its resume hook, if present. |
| 81 | */ |
Thomas Hellstrom | 08e4d53 | 2009-08-20 19:02:31 +1000 | [diff] [blame] | 82 | static int drm_class_resume(struct device *dev) |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 83 | { |
Thomas Hellstrom | 08e4d53 | 2009-08-20 19:02:31 +1000 | [diff] [blame] | 84 | if (dev->type == &drm_sysfs_device_minor) { |
| 85 | struct drm_minor *drm_minor = to_drm_minor(dev); |
| 86 | struct drm_device *drm_dev = drm_minor->dev; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 87 | |
Thomas Hellstrom | 08e4d53 | 2009-08-20 19:02:31 +1000 | [diff] [blame] | 88 | if (drm_minor->type == DRM_MINOR_LEGACY && |
| 89 | !drm_core_check_feature(drm_dev, DRIVER_MODESET) && |
| 90 | drm_dev->driver->resume) |
| 91 | return drm_dev->driver->resume(drm_dev); |
| 92 | } |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
Shuah Khan | cf4b91f | 2013-07-01 16:06:02 -0600 | [diff] [blame] | 96 | static const struct dev_pm_ops drm_class_dev_pm_ops = { |
| 97 | .suspend = drm_class_suspend, |
| 98 | .resume = drm_class_resume, |
| 99 | .freeze = drm_class_freeze, |
| 100 | }; |
| 101 | |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 102 | static char *drm_devnode(struct device *dev, umode_t *mode) |
Kay Sievers | 02200d0 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 103 | { |
| 104 | return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev)); |
| 105 | } |
| 106 | |
Andi Kleen | 0933e2d | 2010-01-05 12:48:09 +0100 | [diff] [blame] | 107 | static CLASS_ATTR_STRING(version, S_IRUGO, |
| 108 | CORE_NAME " " |
| 109 | __stringify(CORE_MAJOR) "." |
| 110 | __stringify(CORE_MINOR) "." |
| 111 | __stringify(CORE_PATCHLEVEL) " " |
| 112 | CORE_DATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * drm_sysfs_create - create a struct drm_sysfs_class structure |
| 116 | * @owner: pointer to the module that is to "own" this struct drm_sysfs_class |
| 117 | * @name: pointer to a string for the name of this class. |
| 118 | * |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 119 | * 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] | 120 | * in calls to drm_sysfs_device_add(). |
| 121 | * |
| 122 | * Note, the pointer created here is to be destroyed when finished by making a |
| 123 | * call to drm_sysfs_destroy(). |
| 124 | */ |
Greg Kroah-Hartman | 0650fd5 | 2006-01-20 14:08:59 -0800 | [diff] [blame] | 125 | struct class *drm_sysfs_create(struct module *owner, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Greg Kroah-Hartman | 0650fd5 | 2006-01-20 14:08:59 -0800 | [diff] [blame] | 127 | struct class *class; |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 128 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Greg Kroah-Hartman | 0650fd5 | 2006-01-20 14:08:59 -0800 | [diff] [blame] | 130 | class = class_create(owner, name); |
Akinobu Mita | 94f060b | 2006-12-09 10:49:47 +1100 | [diff] [blame] | 131 | if (IS_ERR(class)) { |
| 132 | err = PTR_ERR(class); |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 133 | goto err_out; |
| 134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Shuah Khan | cf4b91f | 2013-07-01 16:06:02 -0600 | [diff] [blame] | 136 | class->pm = &drm_class_dev_pm_ops; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 137 | |
Andi Kleen | 0933e2d | 2010-01-05 12:48:09 +0100 | [diff] [blame] | 138 | err = class_create_file(class, &class_attr_version.attr); |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 139 | if (err) |
| 140 | goto err_out_class; |
| 141 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 142 | class->devnode = drm_devnode; |
Kay Sievers | 02200d0 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 143 | |
Greg Kroah-Hartman | 0650fd5 | 2006-01-20 14:08:59 -0800 | [diff] [blame] | 144 | return class; |
Jeff Garzik | 24f73c9 | 2006-10-10 14:23:37 -0700 | [diff] [blame] | 145 | |
| 146 | err_out_class: |
| 147 | class_destroy(class); |
| 148 | err_out: |
| 149 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 153 | * drm_sysfs_destroy - destroys DRM class |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | * |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 155 | * Destroy the DRM device class. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | */ |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 157 | void drm_sysfs_destroy(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 159 | if ((drm_class == NULL) || (IS_ERR(drm_class))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | return; |
Andi Kleen | 0933e2d | 2010-01-05 12:48:09 +0100 | [diff] [blame] | 161 | class_remove_file(drm_class, &class_attr_version.attr); |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 162 | class_destroy(drm_class); |
Dave Airlie | 49099c4 | 2012-07-06 18:06:42 +0100 | [diff] [blame] | 163 | drm_class = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 166 | /* |
| 167 | * Connector properties |
| 168 | */ |
Chris Wilson | c484f02 | 2015-03-06 12:36:42 +0000 | [diff] [blame] | 169 | static ssize_t status_store(struct device *device, |
| 170 | struct device_attribute *attr, |
| 171 | const char *buf, size_t count) |
| 172 | { |
| 173 | struct drm_connector *connector = to_drm_connector(device); |
| 174 | struct drm_device *dev = connector->dev; |
| 175 | enum drm_connector_status old_status; |
| 176 | int ret; |
| 177 | |
| 178 | ret = mutex_lock_interruptible(&dev->mode_config.mutex); |
| 179 | if (ret) |
| 180 | return ret; |
| 181 | |
| 182 | old_status = connector->status; |
| 183 | |
| 184 | if (sysfs_streq(buf, "detect")) { |
| 185 | connector->force = 0; |
| 186 | connector->status = connector->funcs->detect(connector, true); |
| 187 | } else if (sysfs_streq(buf, "on")) { |
| 188 | connector->force = DRM_FORCE_ON; |
| 189 | } else if (sysfs_streq(buf, "on-digital")) { |
| 190 | connector->force = DRM_FORCE_ON_DIGITAL; |
| 191 | } else if (sysfs_streq(buf, "off")) { |
| 192 | connector->force = DRM_FORCE_OFF; |
| 193 | } else |
| 194 | ret = -EINVAL; |
| 195 | |
| 196 | if (ret == 0 && connector->force) { |
| 197 | if (connector->force == DRM_FORCE_ON || |
| 198 | connector->force == DRM_FORCE_ON_DIGITAL) |
| 199 | connector->status = connector_status_connected; |
| 200 | else |
| 201 | connector->status = connector_status_disconnected; |
| 202 | if (connector->funcs->force) |
| 203 | connector->funcs->force(connector); |
| 204 | } |
| 205 | |
| 206 | if (old_status != connector->status) { |
| 207 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n", |
| 208 | connector->base.id, |
| 209 | connector->name, |
| 210 | old_status, connector->status); |
| 211 | |
| 212 | dev->mode_config.delayed_event = true; |
| 213 | if (dev->mode_config.poll_enabled) |
| 214 | schedule_delayed_work(&dev->mode_config.output_poll_work, |
| 215 | 0); |
| 216 | } |
| 217 | |
| 218 | mutex_unlock(&dev->mode_config.mutex); |
| 219 | |
Russell King | 38d8571 | 2015-06-06 08:27:30 +1000 | [diff] [blame] | 220 | return ret ? ret : count; |
Chris Wilson | c484f02 | 2015-03-06 12:36:42 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 223 | static ssize_t status_show(struct device *device, |
| 224 | struct device_attribute *attr, |
| 225 | char *buf) |
| 226 | { |
| 227 | struct drm_connector *connector = to_drm_connector(device); |
Chris Wilson | 007c80a | 2011-03-15 11:40:00 +0000 | [diff] [blame] | 228 | |
Keith Packard | 75185c9 | 2009-05-30 20:42:25 -0700 | [diff] [blame] | 229 | return snprintf(buf, PAGE_SIZE, "%s\n", |
Chris Wilson | c484f02 | 2015-03-06 12:36:42 +0000 | [diff] [blame] | 230 | drm_get_connector_status_name(connector->status)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static ssize_t dpms_show(struct device *device, |
| 234 | struct device_attribute *attr, |
| 235 | char *buf) |
| 236 | { |
| 237 | struct drm_connector *connector = to_drm_connector(device); |
| 238 | struct drm_device *dev = connector->dev; |
| 239 | uint64_t dpms_status; |
| 240 | int ret; |
| 241 | |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 242 | ret = drm_object_property_get_value(&connector->base, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 243 | dev->mode_config.dpms_property, |
| 244 | &dpms_status); |
| 245 | if (ret) |
| 246 | return 0; |
| 247 | |
Keith Packard | 75185c9 | 2009-05-30 20:42:25 -0700 | [diff] [blame] | 248 | return snprintf(buf, PAGE_SIZE, "%s\n", |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 249 | drm_get_dpms_name((int)dpms_status)); |
| 250 | } |
| 251 | |
| 252 | static ssize_t enabled_show(struct device *device, |
| 253 | struct device_attribute *attr, |
| 254 | char *buf) |
| 255 | { |
| 256 | struct drm_connector *connector = to_drm_connector(device); |
| 257 | |
Keith Packard | 75185c9 | 2009-05-30 20:42:25 -0700 | [diff] [blame] | 258 | return snprintf(buf, PAGE_SIZE, "%s\n", connector->encoder ? "enabled" : |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 259 | "disabled"); |
| 260 | } |
| 261 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 262 | static ssize_t edid_show(struct file *filp, struct kobject *kobj, |
| 263 | struct bin_attribute *attr, char *buf, loff_t off, |
| 264 | size_t count) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 265 | { |
| 266 | struct device *connector_dev = container_of(kobj, struct device, kobj); |
| 267 | struct drm_connector *connector = to_drm_connector(connector_dev); |
| 268 | unsigned char *edid; |
| 269 | size_t size; |
| 270 | |
| 271 | if (!connector->edid_blob_ptr) |
| 272 | return 0; |
| 273 | |
| 274 | edid = connector->edid_blob_ptr->data; |
| 275 | size = connector->edid_blob_ptr->length; |
| 276 | if (!edid) |
| 277 | return 0; |
| 278 | |
| 279 | if (off >= size) |
| 280 | return 0; |
| 281 | |
| 282 | if (off + count > size) |
| 283 | count = size - off; |
| 284 | memcpy(buf, edid + off, count); |
| 285 | |
| 286 | return count; |
| 287 | } |
| 288 | |
| 289 | static ssize_t modes_show(struct device *device, |
| 290 | struct device_attribute *attr, |
| 291 | char *buf) |
| 292 | { |
| 293 | struct drm_connector *connector = to_drm_connector(device); |
| 294 | struct drm_display_mode *mode; |
| 295 | int written = 0; |
| 296 | |
| 297 | list_for_each_entry(mode, &connector->modes, head) { |
| 298 | written += snprintf(buf + written, PAGE_SIZE - written, "%s\n", |
| 299 | mode->name); |
| 300 | } |
| 301 | |
| 302 | return written; |
| 303 | } |
| 304 | |
Jani Nikula | 4bb4637 | 2015-05-12 12:14:54 +0300 | [diff] [blame] | 305 | static ssize_t tv_subconnector_show(struct device *device, |
| 306 | struct device_attribute *attr, |
| 307 | char *buf) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 308 | { |
| 309 | struct drm_connector *connector = to_drm_connector(device); |
| 310 | struct drm_device *dev = connector->dev; |
Jani Nikula | 4bb4637 | 2015-05-12 12:14:54 +0300 | [diff] [blame] | 311 | struct drm_property *prop; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 312 | uint64_t subconnector; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 313 | int ret; |
| 314 | |
Jani Nikula | 4bb4637 | 2015-05-12 12:14:54 +0300 | [diff] [blame] | 315 | prop = dev->mode_config.tv_subconnector_property; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 316 | if (!prop) { |
| 317 | DRM_ERROR("Unable to find subconnector property\n"); |
| 318 | return 0; |
| 319 | } |
| 320 | |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 321 | ret = drm_object_property_get_value(&connector->base, prop, &subconnector); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 322 | if (ret) |
| 323 | return 0; |
| 324 | |
Jani Nikula | 4bb4637 | 2015-05-12 12:14:54 +0300 | [diff] [blame] | 325 | return snprintf(buf, PAGE_SIZE, "%s", |
| 326 | drm_get_tv_subconnector_name((int)subconnector)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Jani Nikula | 4bb4637 | 2015-05-12 12:14:54 +0300 | [diff] [blame] | 329 | static ssize_t tv_select_subconnector_show(struct device *device, |
| 330 | struct device_attribute *attr, |
| 331 | char *buf) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 332 | { |
| 333 | struct drm_connector *connector = to_drm_connector(device); |
| 334 | struct drm_device *dev = connector->dev; |
Jani Nikula | 4bb4637 | 2015-05-12 12:14:54 +0300 | [diff] [blame] | 335 | struct drm_property *prop; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 336 | uint64_t subconnector; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 337 | int ret; |
| 338 | |
Jani Nikula | 4bb4637 | 2015-05-12 12:14:54 +0300 | [diff] [blame] | 339 | prop = dev->mode_config.tv_select_subconnector_property; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 340 | if (!prop) { |
| 341 | DRM_ERROR("Unable to find select subconnector property\n"); |
| 342 | return 0; |
| 343 | } |
| 344 | |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 345 | ret = drm_object_property_get_value(&connector->base, prop, &subconnector); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 346 | if (ret) |
| 347 | return 0; |
| 348 | |
Jani Nikula | 4bb4637 | 2015-05-12 12:14:54 +0300 | [diff] [blame] | 349 | return snprintf(buf, PAGE_SIZE, "%s", |
| 350 | drm_get_tv_select_name((int)subconnector)); |
| 351 | } |
| 352 | |
| 353 | static ssize_t dvii_subconnector_show(struct device *device, |
| 354 | struct device_attribute *attr, |
| 355 | char *buf) |
| 356 | { |
| 357 | struct drm_connector *connector = to_drm_connector(device); |
| 358 | struct drm_device *dev = connector->dev; |
| 359 | struct drm_property *prop; |
| 360 | uint64_t subconnector; |
| 361 | int ret; |
| 362 | |
Jani Nikula | 4bb4637 | 2015-05-12 12:14:54 +0300 | [diff] [blame] | 363 | prop = dev->mode_config.dvi_i_subconnector_property; |
| 364 | if (!prop) { |
| 365 | DRM_ERROR("Unable to find subconnector property\n"); |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | ret = drm_object_property_get_value(&connector->base, prop, &subconnector); |
| 370 | if (ret) |
| 371 | return 0; |
| 372 | |
| 373 | return snprintf(buf, PAGE_SIZE, "%s", |
| 374 | drm_get_dvi_i_subconnector_name((int)subconnector)); |
| 375 | } |
| 376 | |
| 377 | static ssize_t dvii_select_subconnector_show(struct device *device, |
| 378 | struct device_attribute *attr, |
| 379 | char *buf) |
| 380 | { |
| 381 | struct drm_connector *connector = to_drm_connector(device); |
| 382 | struct drm_device *dev = connector->dev; |
| 383 | struct drm_property *prop; |
| 384 | uint64_t subconnector; |
| 385 | int ret; |
| 386 | |
Jani Nikula | 4bb4637 | 2015-05-12 12:14:54 +0300 | [diff] [blame] | 387 | prop = dev->mode_config.dvi_i_select_subconnector_property; |
| 388 | if (!prop) { |
| 389 | DRM_ERROR("Unable to find select subconnector property\n"); |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | ret = drm_object_property_get_value(&connector->base, prop, &subconnector); |
| 394 | if (ret) |
| 395 | return 0; |
| 396 | |
| 397 | return snprintf(buf, PAGE_SIZE, "%s", |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 398 | drm_get_dvi_i_select_name((int)subconnector)); |
| 399 | } |
| 400 | |
Chris Wilson | c484f02 | 2015-03-06 12:36:42 +0000 | [diff] [blame] | 401 | static DEVICE_ATTR_RW(status); |
Takashi Iwai | 335f1a6 | 2015-02-04 11:58:53 +0100 | [diff] [blame] | 402 | static DEVICE_ATTR_RO(enabled); |
| 403 | static DEVICE_ATTR_RO(dpms); |
| 404 | static DEVICE_ATTR_RO(modes); |
| 405 | |
| 406 | static struct attribute *connector_dev_attrs[] = { |
| 407 | &dev_attr_status.attr, |
| 408 | &dev_attr_enabled.attr, |
| 409 | &dev_attr_dpms.attr, |
| 410 | &dev_attr_modes.attr, |
| 411 | NULL |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 412 | }; |
| 413 | |
Jani Nikula | 4bb4637 | 2015-05-12 12:14:54 +0300 | [diff] [blame] | 414 | static DEVICE_ATTR_RO(tv_subconnector); |
| 415 | static DEVICE_ATTR_RO(tv_select_subconnector); |
Takashi Iwai | 335f1a6 | 2015-02-04 11:58:53 +0100 | [diff] [blame] | 416 | |
Jani Nikula | fe6fcdd | 2015-05-12 12:14:53 +0300 | [diff] [blame] | 417 | static struct attribute *connector_tv_dev_attrs[] = { |
Jani Nikula | 4bb4637 | 2015-05-12 12:14:54 +0300 | [diff] [blame] | 418 | &dev_attr_tv_subconnector.attr, |
| 419 | &dev_attr_tv_select_subconnector.attr, |
Takashi Iwai | 335f1a6 | 2015-02-04 11:58:53 +0100 | [diff] [blame] | 420 | NULL |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 421 | }; |
| 422 | |
Jani Nikula | 4bb4637 | 2015-05-12 12:14:54 +0300 | [diff] [blame] | 423 | static DEVICE_ATTR_RO(dvii_subconnector); |
| 424 | static DEVICE_ATTR_RO(dvii_select_subconnector); |
| 425 | |
| 426 | static struct attribute *connector_dvii_dev_attrs[] = { |
| 427 | &dev_attr_dvii_subconnector.attr, |
| 428 | &dev_attr_dvii_select_subconnector.attr, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 429 | NULL |
| 430 | }; |
| 431 | |
Jani Nikula | 7d4d3a5 | 2015-05-12 12:14:52 +0300 | [diff] [blame] | 432 | /* Connector type related helpers */ |
| 433 | static int kobj_connector_type(struct kobject *kobj) |
Takashi Iwai | 335f1a6 | 2015-02-04 11:58:53 +0100 | [diff] [blame] | 434 | { |
| 435 | struct device *dev = kobj_to_dev(kobj); |
| 436 | struct drm_connector *connector = to_drm_connector(dev); |
| 437 | |
Jani Nikula | 7d4d3a5 | 2015-05-12 12:14:52 +0300 | [diff] [blame] | 438 | return connector->connector_type; |
| 439 | } |
| 440 | |
Jani Nikula | fe6fcdd | 2015-05-12 12:14:53 +0300 | [diff] [blame] | 441 | static umode_t connector_is_dvii(struct kobject *kobj, |
| 442 | struct attribute *attr, int idx) |
Jani Nikula | 7d4d3a5 | 2015-05-12 12:14:52 +0300 | [diff] [blame] | 443 | { |
Jani Nikula | fe6fcdd | 2015-05-12 12:14:53 +0300 | [diff] [blame] | 444 | return kobj_connector_type(kobj) == DRM_MODE_CONNECTOR_DVII ? |
| 445 | attr->mode : 0; |
| 446 | } |
| 447 | |
| 448 | static umode_t connector_is_tv(struct kobject *kobj, |
| 449 | struct attribute *attr, int idx) |
| 450 | { |
Jani Nikula | 7d4d3a5 | 2015-05-12 12:14:52 +0300 | [diff] [blame] | 451 | switch (kobj_connector_type(kobj)) { |
Takashi Iwai | 335f1a6 | 2015-02-04 11:58:53 +0100 | [diff] [blame] | 452 | case DRM_MODE_CONNECTOR_Composite: |
| 453 | case DRM_MODE_CONNECTOR_SVIDEO: |
| 454 | case DRM_MODE_CONNECTOR_Component: |
| 455 | case DRM_MODE_CONNECTOR_TV: |
| 456 | return attr->mode; |
| 457 | } |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 462 | static struct bin_attribute edid_attr = { |
| 463 | .attr.name = "edid", |
Keith Packard | e36ebaf | 2009-05-30 20:42:26 -0700 | [diff] [blame] | 464 | .attr.mode = 0444, |
Adam Jackson | 7466f4c | 2010-03-29 21:43:23 +0000 | [diff] [blame] | 465 | .size = 0, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 466 | .read = edid_show, |
| 467 | }; |
| 468 | |
Takashi Iwai | 335f1a6 | 2015-02-04 11:58:53 +0100 | [diff] [blame] | 469 | static struct bin_attribute *connector_bin_attrs[] = { |
| 470 | &edid_attr, |
| 471 | NULL |
| 472 | }; |
| 473 | |
| 474 | static const struct attribute_group connector_dev_group = { |
| 475 | .attrs = connector_dev_attrs, |
| 476 | .bin_attrs = connector_bin_attrs, |
| 477 | }; |
| 478 | |
Jani Nikula | fe6fcdd | 2015-05-12 12:14:53 +0300 | [diff] [blame] | 479 | static const struct attribute_group connector_tv_dev_group = { |
| 480 | .attrs = connector_tv_dev_attrs, |
| 481 | .is_visible = connector_is_tv, |
| 482 | }; |
| 483 | |
| 484 | static const struct attribute_group connector_dvii_dev_group = { |
Jani Nikula | 4bb4637 | 2015-05-12 12:14:54 +0300 | [diff] [blame] | 485 | .attrs = connector_dvii_dev_attrs, |
Jani Nikula | fe6fcdd | 2015-05-12 12:14:53 +0300 | [diff] [blame] | 486 | .is_visible = connector_is_dvii, |
Takashi Iwai | 335f1a6 | 2015-02-04 11:58:53 +0100 | [diff] [blame] | 487 | }; |
| 488 | |
| 489 | static const struct attribute_group *connector_dev_groups[] = { |
| 490 | &connector_dev_group, |
Jani Nikula | fe6fcdd | 2015-05-12 12:14:53 +0300 | [diff] [blame] | 491 | &connector_tv_dev_group, |
| 492 | &connector_dvii_dev_group, |
Takashi Iwai | 335f1a6 | 2015-02-04 11:58:53 +0100 | [diff] [blame] | 493 | NULL |
| 494 | }; |
| 495 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 496 | /** |
Laurent Pinchart | 3b02ab8 | 2012-05-17 13:27:20 +0200 | [diff] [blame] | 497 | * drm_sysfs_connector_add - add a connector to sysfs |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 498 | * @connector: connector to add |
| 499 | * |
Laurent Pinchart | 3b02ab8 | 2012-05-17 13:27:20 +0200 | [diff] [blame] | 500 | * Create a connector device in sysfs, along with its associated connector |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 501 | * properties (so far, connection status, dpms, mode list & edid) and |
| 502 | * generate a hotplug event so userspace knows there's a new connector |
| 503 | * available. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 504 | */ |
| 505 | int drm_sysfs_connector_add(struct drm_connector *connector) |
| 506 | { |
| 507 | struct drm_device *dev = connector->dev; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 508 | |
Dave Airlie | 5bdebb1 | 2013-10-11 14:07:25 +1000 | [diff] [blame] | 509 | if (connector->kdev) |
| 510 | return 0; |
| 511 | |
Takashi Iwai | 335f1a6 | 2015-02-04 11:58:53 +0100 | [diff] [blame] | 512 | connector->kdev = |
| 513 | device_create_with_groups(drm_class, dev->primary->kdev, 0, |
| 514 | connector, connector_dev_groups, |
| 515 | "card%d-%s", dev->primary->index, |
| 516 | connector->name); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 517 | DRM_DEBUG("adding \"%s\" to sysfs\n", |
Jani Nikula | 2593382 | 2014-06-03 14:56:20 +0300 | [diff] [blame] | 518 | connector->name); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 519 | |
Dave Airlie | 5bdebb1 | 2013-10-11 14:07:25 +1000 | [diff] [blame] | 520 | if (IS_ERR(connector->kdev)) { |
| 521 | DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector->kdev)); |
Takashi Iwai | 335f1a6 | 2015-02-04 11:58:53 +0100 | [diff] [blame] | 522 | return PTR_ERR(connector->kdev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 525 | /* Let userspace know we have a new connector */ |
| 526 | drm_sysfs_hotplug_event(dev); |
| 527 | |
| 528 | return 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 529 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 530 | |
| 531 | /** |
| 532 | * drm_sysfs_connector_remove - remove an connector device from sysfs |
| 533 | * @connector: connector to remove |
| 534 | * |
| 535 | * Remove @connector and its associated attributes from sysfs. Note that |
| 536 | * the device model core will take care of sending the "remove" uevent |
| 537 | * at this time, so we don't need to do it. |
| 538 | * |
| 539 | * Note: |
| 540 | * This routine should only be called if the connector was previously |
| 541 | * successfully registered. If @connector hasn't been registered yet, |
| 542 | * you'll likely see a panic somewhere deep in sysfs code when called. |
| 543 | */ |
| 544 | void drm_sysfs_connector_remove(struct drm_connector *connector) |
| 545 | { |
Dave Airlie | 5bdebb1 | 2013-10-11 14:07:25 +1000 | [diff] [blame] | 546 | if (!connector->kdev) |
Dave Airlie | 1828fe6c | 2012-02-20 14:15:02 +0000 | [diff] [blame] | 547 | return; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 548 | DRM_DEBUG("removing \"%s\" from sysfs\n", |
Jani Nikula | 2593382 | 2014-06-03 14:56:20 +0300 | [diff] [blame] | 549 | connector->name); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 550 | |
Dave Airlie | 5bdebb1 | 2013-10-11 14:07:25 +1000 | [diff] [blame] | 551 | device_unregister(connector->kdev); |
| 552 | connector->kdev = NULL; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 553 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 554 | |
| 555 | /** |
| 556 | * drm_sysfs_hotplug_event - generate a DRM uevent |
| 557 | * @dev: DRM device |
| 558 | * |
| 559 | * Send a uevent for the DRM device specified by @dev. Currently we only |
| 560 | * set HOTPLUG=1 in the uevent environment, but this could be expanded to |
| 561 | * deal with other types of events. |
| 562 | */ |
| 563 | void drm_sysfs_hotplug_event(struct drm_device *dev) |
| 564 | { |
| 565 | char *event_string = "HOTPLUG=1"; |
| 566 | char *envp[] = { event_string, NULL }; |
| 567 | |
| 568 | DRM_DEBUG("generating hotplug event\n"); |
| 569 | |
Dave Airlie | 5bdebb1 | 2013-10-11 14:07:25 +1000 | [diff] [blame] | 570 | kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 571 | } |
Jesse Barnes | 5ca5828 | 2009-03-31 14:11:15 -0700 | [diff] [blame] | 572 | EXPORT_SYMBOL(drm_sysfs_hotplug_event); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 573 | |
David Herrmann | 760c960 | 2013-11-21 20:50:50 +1000 | [diff] [blame] | 574 | static void drm_sysfs_release(struct device *dev) |
| 575 | { |
| 576 | kfree(dev); |
| 577 | } |
| 578 | |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 579 | /** |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 580 | * drm_sysfs_minor_alloc() - Allocate sysfs device for given minor |
| 581 | * @minor: minor to allocate sysfs device for |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 582 | * |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 583 | * This allocates a new sysfs device for @minor and returns it. The device is |
| 584 | * not registered nor linked. The caller has to use device_add() and |
| 585 | * device_del() to register and unregister it. |
| 586 | * |
| 587 | * Note that dev_get_drvdata() on the new device will return the minor. |
| 588 | * However, the device does not hold a ref-count to the minor nor to the |
| 589 | * underlying drm_device. This is unproblematic as long as you access the |
| 590 | * private data only in sysfs callbacks. device_del() disables those |
| 591 | * synchronously, so they cannot be called after you cleanup a minor. |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 592 | */ |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 593 | struct device *drm_sysfs_minor_alloc(struct drm_minor *minor) |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 594 | { |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 595 | const char *minor_str; |
| 596 | struct device *kdev; |
David Herrmann | 760c960 | 2013-11-21 20:50:50 +1000 | [diff] [blame] | 597 | int r; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 598 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 599 | if (minor->type == DRM_MINOR_CONTROL) |
| 600 | minor_str = "controlD%d"; |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 601 | else if (minor->type == DRM_MINOR_RENDER) |
| 602 | minor_str = "renderD%d"; |
| 603 | else |
| 604 | minor_str = "card%d"; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 605 | |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 606 | kdev = kzalloc(sizeof(*kdev), GFP_KERNEL); |
| 607 | if (!kdev) |
| 608 | return ERR_PTR(-ENOMEM); |
David Herrmann | 760c960 | 2013-11-21 20:50:50 +1000 | [diff] [blame] | 609 | |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 610 | device_initialize(kdev); |
| 611 | kdev->devt = MKDEV(DRM_MAJOR, minor->index); |
| 612 | kdev->class = drm_class; |
| 613 | kdev->type = &drm_sysfs_device_minor; |
| 614 | kdev->parent = minor->dev->dev; |
| 615 | kdev->release = drm_sysfs_release; |
| 616 | dev_set_drvdata(kdev, minor); |
David Herrmann | 760c960 | 2013-11-21 20:50:50 +1000 | [diff] [blame] | 617 | |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 618 | r = dev_set_name(kdev, minor_str, minor->index); |
David Herrmann | 760c960 | 2013-11-21 20:50:50 +1000 | [diff] [blame] | 619 | if (r < 0) |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 620 | goto err_free; |
David Herrmann | 760c960 | 2013-11-21 20:50:50 +1000 | [diff] [blame] | 621 | |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 622 | return kdev; |
David Herrmann | 760c960 | 2013-11-21 20:50:50 +1000 | [diff] [blame] | 623 | |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 624 | err_free: |
| 625 | put_device(kdev); |
| 626 | return ERR_PTR(r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /** |
Thomas Hellstrom | 327c225 | 2009-08-17 16:28:37 +0200 | [diff] [blame] | 630 | * drm_class_device_register - Register a struct device in the drm class. |
| 631 | * |
| 632 | * @dev: pointer to struct device to register. |
| 633 | * |
| 634 | * @dev should have all relevant members pre-filled with the exception |
| 635 | * of the class member. In particular, the device_type member must |
| 636 | * be set. |
| 637 | */ |
| 638 | |
| 639 | int drm_class_device_register(struct device *dev) |
| 640 | { |
Dave Airlie | 49099c4 | 2012-07-06 18:06:42 +0100 | [diff] [blame] | 641 | if (!drm_class || IS_ERR(drm_class)) |
| 642 | return -ENOENT; |
| 643 | |
Thomas Hellstrom | 327c225 | 2009-08-17 16:28:37 +0200 | [diff] [blame] | 644 | dev->class = drm_class; |
| 645 | return device_register(dev); |
| 646 | } |
| 647 | EXPORT_SYMBOL_GPL(drm_class_device_register); |
| 648 | |
| 649 | void drm_class_device_unregister(struct device *dev) |
| 650 | { |
| 651 | return device_unregister(dev); |
| 652 | } |
| 653 | EXPORT_SYMBOL_GPL(drm_class_device_unregister); |