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 | */ |
| 169 | static ssize_t status_show(struct device *device, |
| 170 | struct device_attribute *attr, |
| 171 | char *buf) |
| 172 | { |
| 173 | struct drm_connector *connector = to_drm_connector(device); |
| 174 | enum drm_connector_status status; |
Chris Wilson | 007c80a | 2011-03-15 11:40:00 +0000 | [diff] [blame] | 175 | int ret; |
| 176 | |
| 177 | ret = mutex_lock_interruptible(&connector->dev->mode_config.mutex); |
| 178 | if (ret) |
| 179 | return ret; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 180 | |
Chris Wilson | 7b334fc | 2010-09-09 23:51:02 +0100 | [diff] [blame] | 181 | status = connector->funcs->detect(connector, true); |
Chris Wilson | 007c80a | 2011-03-15 11:40:00 +0000 | [diff] [blame] | 182 | mutex_unlock(&connector->dev->mode_config.mutex); |
| 183 | |
Keith Packard | 75185c9 | 2009-05-30 20:42:25 -0700 | [diff] [blame] | 184 | return snprintf(buf, PAGE_SIZE, "%s\n", |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 185 | drm_get_connector_status_name(status)); |
| 186 | } |
| 187 | |
| 188 | static ssize_t dpms_show(struct device *device, |
| 189 | struct device_attribute *attr, |
| 190 | char *buf) |
| 191 | { |
| 192 | struct drm_connector *connector = to_drm_connector(device); |
| 193 | struct drm_device *dev = connector->dev; |
| 194 | uint64_t dpms_status; |
| 195 | int ret; |
| 196 | |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 197 | ret = drm_object_property_get_value(&connector->base, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 198 | dev->mode_config.dpms_property, |
| 199 | &dpms_status); |
| 200 | if (ret) |
| 201 | return 0; |
| 202 | |
Keith Packard | 75185c9 | 2009-05-30 20:42:25 -0700 | [diff] [blame] | 203 | return snprintf(buf, PAGE_SIZE, "%s\n", |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 204 | drm_get_dpms_name((int)dpms_status)); |
| 205 | } |
| 206 | |
| 207 | static ssize_t enabled_show(struct device *device, |
| 208 | struct device_attribute *attr, |
| 209 | char *buf) |
| 210 | { |
| 211 | struct drm_connector *connector = to_drm_connector(device); |
| 212 | |
Keith Packard | 75185c9 | 2009-05-30 20:42:25 -0700 | [diff] [blame] | 213 | return snprintf(buf, PAGE_SIZE, "%s\n", connector->encoder ? "enabled" : |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 214 | "disabled"); |
| 215 | } |
| 216 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 217 | static ssize_t edid_show(struct file *filp, struct kobject *kobj, |
| 218 | struct bin_attribute *attr, char *buf, loff_t off, |
| 219 | size_t count) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 220 | { |
| 221 | struct device *connector_dev = container_of(kobj, struct device, kobj); |
| 222 | struct drm_connector *connector = to_drm_connector(connector_dev); |
| 223 | unsigned char *edid; |
| 224 | size_t size; |
| 225 | |
| 226 | if (!connector->edid_blob_ptr) |
| 227 | return 0; |
| 228 | |
| 229 | edid = connector->edid_blob_ptr->data; |
| 230 | size = connector->edid_blob_ptr->length; |
| 231 | if (!edid) |
| 232 | return 0; |
| 233 | |
| 234 | if (off >= size) |
| 235 | return 0; |
| 236 | |
| 237 | if (off + count > size) |
| 238 | count = size - off; |
| 239 | memcpy(buf, edid + off, count); |
| 240 | |
| 241 | return count; |
| 242 | } |
| 243 | |
| 244 | static ssize_t modes_show(struct device *device, |
| 245 | struct device_attribute *attr, |
| 246 | char *buf) |
| 247 | { |
| 248 | struct drm_connector *connector = to_drm_connector(device); |
| 249 | struct drm_display_mode *mode; |
| 250 | int written = 0; |
| 251 | |
| 252 | list_for_each_entry(mode, &connector->modes, head) { |
| 253 | written += snprintf(buf + written, PAGE_SIZE - written, "%s\n", |
| 254 | mode->name); |
| 255 | } |
| 256 | |
| 257 | return written; |
| 258 | } |
| 259 | |
| 260 | static ssize_t subconnector_show(struct device *device, |
| 261 | struct device_attribute *attr, |
| 262 | char *buf) |
| 263 | { |
| 264 | struct drm_connector *connector = to_drm_connector(device); |
| 265 | struct drm_device *dev = connector->dev; |
| 266 | struct drm_property *prop = NULL; |
| 267 | uint64_t subconnector; |
| 268 | int is_tv = 0; |
| 269 | int ret; |
| 270 | |
| 271 | switch (connector->connector_type) { |
| 272 | case DRM_MODE_CONNECTOR_DVII: |
| 273 | prop = dev->mode_config.dvi_i_subconnector_property; |
| 274 | break; |
| 275 | case DRM_MODE_CONNECTOR_Composite: |
| 276 | case DRM_MODE_CONNECTOR_SVIDEO: |
| 277 | case DRM_MODE_CONNECTOR_Component: |
Francisco Jerez | 74bd3c2 | 2009-08-02 04:19:18 +0200 | [diff] [blame] | 278 | case DRM_MODE_CONNECTOR_TV: |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 279 | prop = dev->mode_config.tv_subconnector_property; |
| 280 | is_tv = 1; |
| 281 | break; |
| 282 | default: |
| 283 | DRM_ERROR("Wrong connector type for this property\n"); |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | if (!prop) { |
| 288 | DRM_ERROR("Unable to find subconnector property\n"); |
| 289 | return 0; |
| 290 | } |
| 291 | |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 292 | ret = drm_object_property_get_value(&connector->base, prop, &subconnector); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 293 | if (ret) |
| 294 | return 0; |
| 295 | |
| 296 | return snprintf(buf, PAGE_SIZE, "%s", is_tv ? |
| 297 | drm_get_tv_subconnector_name((int)subconnector) : |
| 298 | drm_get_dvi_i_subconnector_name((int)subconnector)); |
| 299 | } |
| 300 | |
| 301 | static ssize_t select_subconnector_show(struct device *device, |
| 302 | struct device_attribute *attr, |
| 303 | char *buf) |
| 304 | { |
| 305 | struct drm_connector *connector = to_drm_connector(device); |
| 306 | struct drm_device *dev = connector->dev; |
| 307 | struct drm_property *prop = NULL; |
| 308 | uint64_t subconnector; |
| 309 | int is_tv = 0; |
| 310 | int ret; |
| 311 | |
| 312 | switch (connector->connector_type) { |
| 313 | case DRM_MODE_CONNECTOR_DVII: |
| 314 | prop = dev->mode_config.dvi_i_select_subconnector_property; |
| 315 | break; |
| 316 | case DRM_MODE_CONNECTOR_Composite: |
| 317 | case DRM_MODE_CONNECTOR_SVIDEO: |
| 318 | case DRM_MODE_CONNECTOR_Component: |
Francisco Jerez | 74bd3c2 | 2009-08-02 04:19:18 +0200 | [diff] [blame] | 319 | case DRM_MODE_CONNECTOR_TV: |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 320 | prop = dev->mode_config.tv_select_subconnector_property; |
| 321 | is_tv = 1; |
| 322 | break; |
| 323 | default: |
| 324 | DRM_ERROR("Wrong connector type for this property\n"); |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | if (!prop) { |
| 329 | DRM_ERROR("Unable to find select subconnector property\n"); |
| 330 | return 0; |
| 331 | } |
| 332 | |
Rob Clark | 5849556 | 2012-10-11 20:50:56 -0500 | [diff] [blame] | 333 | ret = drm_object_property_get_value(&connector->base, prop, &subconnector); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 334 | if (ret) |
| 335 | return 0; |
| 336 | |
| 337 | return snprintf(buf, PAGE_SIZE, "%s", is_tv ? |
| 338 | drm_get_tv_select_name((int)subconnector) : |
| 339 | drm_get_dvi_i_select_name((int)subconnector)); |
| 340 | } |
| 341 | |
Takashi Iwai | 335f1a6 | 2015-02-04 11:58:53 +0100 | [diff] [blame^] | 342 | static DEVICE_ATTR_RO(status); |
| 343 | static DEVICE_ATTR_RO(enabled); |
| 344 | static DEVICE_ATTR_RO(dpms); |
| 345 | static DEVICE_ATTR_RO(modes); |
| 346 | |
| 347 | static struct attribute *connector_dev_attrs[] = { |
| 348 | &dev_attr_status.attr, |
| 349 | &dev_attr_enabled.attr, |
| 350 | &dev_attr_dpms.attr, |
| 351 | &dev_attr_modes.attr, |
| 352 | NULL |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | /* These attributes are for both DVI-I connectors and all types of tv-out. */ |
Takashi Iwai | 335f1a6 | 2015-02-04 11:58:53 +0100 | [diff] [blame^] | 356 | static DEVICE_ATTR_RO(subconnector); |
| 357 | static DEVICE_ATTR_RO(select_subconnector); |
| 358 | |
| 359 | static struct attribute *connector_opt_dev_attrs[] = { |
| 360 | &dev_attr_subconnector.attr, |
| 361 | &dev_attr_select_subconnector.attr, |
| 362 | NULL |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 363 | }; |
| 364 | |
Takashi Iwai | 335f1a6 | 2015-02-04 11:58:53 +0100 | [diff] [blame^] | 365 | static umode_t connector_opt_dev_is_visible(struct kobject *kobj, |
| 366 | struct attribute *attr, int idx) |
| 367 | { |
| 368 | struct device *dev = kobj_to_dev(kobj); |
| 369 | struct drm_connector *connector = to_drm_connector(dev); |
| 370 | |
| 371 | /* |
| 372 | * In the long run it maybe a good idea to make one set of |
| 373 | * optionals per connector type. |
| 374 | */ |
| 375 | switch (connector->connector_type) { |
| 376 | case DRM_MODE_CONNECTOR_DVII: |
| 377 | case DRM_MODE_CONNECTOR_Composite: |
| 378 | case DRM_MODE_CONNECTOR_SVIDEO: |
| 379 | case DRM_MODE_CONNECTOR_Component: |
| 380 | case DRM_MODE_CONNECTOR_TV: |
| 381 | return attr->mode; |
| 382 | } |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 387 | static struct bin_attribute edid_attr = { |
| 388 | .attr.name = "edid", |
Keith Packard | e36ebaf | 2009-05-30 20:42:26 -0700 | [diff] [blame] | 389 | .attr.mode = 0444, |
Adam Jackson | 7466f4c | 2010-03-29 21:43:23 +0000 | [diff] [blame] | 390 | .size = 0, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 391 | .read = edid_show, |
| 392 | }; |
| 393 | |
Takashi Iwai | 335f1a6 | 2015-02-04 11:58:53 +0100 | [diff] [blame^] | 394 | static struct bin_attribute *connector_bin_attrs[] = { |
| 395 | &edid_attr, |
| 396 | NULL |
| 397 | }; |
| 398 | |
| 399 | static const struct attribute_group connector_dev_group = { |
| 400 | .attrs = connector_dev_attrs, |
| 401 | .bin_attrs = connector_bin_attrs, |
| 402 | }; |
| 403 | |
| 404 | static const struct attribute_group connector_opt_dev_group = { |
| 405 | .attrs = connector_opt_dev_attrs, |
| 406 | .is_visible = connector_opt_dev_is_visible, |
| 407 | }; |
| 408 | |
| 409 | static const struct attribute_group *connector_dev_groups[] = { |
| 410 | &connector_dev_group, |
| 411 | &connector_opt_dev_group, |
| 412 | NULL |
| 413 | }; |
| 414 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 415 | /** |
Laurent Pinchart | 3b02ab8 | 2012-05-17 13:27:20 +0200 | [diff] [blame] | 416 | * drm_sysfs_connector_add - add a connector to sysfs |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 417 | * @connector: connector to add |
| 418 | * |
Laurent Pinchart | 3b02ab8 | 2012-05-17 13:27:20 +0200 | [diff] [blame] | 419 | * Create a connector device in sysfs, along with its associated connector |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 420 | * properties (so far, connection status, dpms, mode list & edid) and |
| 421 | * generate a hotplug event so userspace knows there's a new connector |
| 422 | * available. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 423 | */ |
| 424 | int drm_sysfs_connector_add(struct drm_connector *connector) |
| 425 | { |
| 426 | struct drm_device *dev = connector->dev; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 427 | |
Dave Airlie | 5bdebb1 | 2013-10-11 14:07:25 +1000 | [diff] [blame] | 428 | if (connector->kdev) |
| 429 | return 0; |
| 430 | |
Takashi Iwai | 335f1a6 | 2015-02-04 11:58:53 +0100 | [diff] [blame^] | 431 | connector->kdev = |
| 432 | device_create_with_groups(drm_class, dev->primary->kdev, 0, |
| 433 | connector, connector_dev_groups, |
| 434 | "card%d-%s", dev->primary->index, |
| 435 | connector->name); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 436 | DRM_DEBUG("adding \"%s\" to sysfs\n", |
Jani Nikula | 2593382 | 2014-06-03 14:56:20 +0300 | [diff] [blame] | 437 | connector->name); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 438 | |
Dave Airlie | 5bdebb1 | 2013-10-11 14:07:25 +1000 | [diff] [blame] | 439 | if (IS_ERR(connector->kdev)) { |
| 440 | 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^] | 441 | return PTR_ERR(connector->kdev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 444 | /* Let userspace know we have a new connector */ |
| 445 | drm_sysfs_hotplug_event(dev); |
| 446 | |
| 447 | return 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 448 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 449 | |
| 450 | /** |
| 451 | * drm_sysfs_connector_remove - remove an connector device from sysfs |
| 452 | * @connector: connector to remove |
| 453 | * |
| 454 | * Remove @connector and its associated attributes from sysfs. Note that |
| 455 | * the device model core will take care of sending the "remove" uevent |
| 456 | * at this time, so we don't need to do it. |
| 457 | * |
| 458 | * Note: |
| 459 | * This routine should only be called if the connector was previously |
| 460 | * successfully registered. If @connector hasn't been registered yet, |
| 461 | * you'll likely see a panic somewhere deep in sysfs code when called. |
| 462 | */ |
| 463 | void drm_sysfs_connector_remove(struct drm_connector *connector) |
| 464 | { |
Dave Airlie | 5bdebb1 | 2013-10-11 14:07:25 +1000 | [diff] [blame] | 465 | if (!connector->kdev) |
Dave Airlie | 1828fe6 | 2012-02-20 14:15:02 +0000 | [diff] [blame] | 466 | return; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 467 | DRM_DEBUG("removing \"%s\" from sysfs\n", |
Jani Nikula | 2593382 | 2014-06-03 14:56:20 +0300 | [diff] [blame] | 468 | connector->name); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 469 | |
Dave Airlie | 5bdebb1 | 2013-10-11 14:07:25 +1000 | [diff] [blame] | 470 | device_unregister(connector->kdev); |
| 471 | connector->kdev = NULL; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 472 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 473 | |
| 474 | /** |
| 475 | * drm_sysfs_hotplug_event - generate a DRM uevent |
| 476 | * @dev: DRM device |
| 477 | * |
| 478 | * Send a uevent for the DRM device specified by @dev. Currently we only |
| 479 | * set HOTPLUG=1 in the uevent environment, but this could be expanded to |
| 480 | * deal with other types of events. |
| 481 | */ |
| 482 | void drm_sysfs_hotplug_event(struct drm_device *dev) |
| 483 | { |
| 484 | char *event_string = "HOTPLUG=1"; |
| 485 | char *envp[] = { event_string, NULL }; |
| 486 | |
| 487 | DRM_DEBUG("generating hotplug event\n"); |
| 488 | |
Dave Airlie | 5bdebb1 | 2013-10-11 14:07:25 +1000 | [diff] [blame] | 489 | kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 490 | } |
Jesse Barnes | 5ca5828 | 2009-03-31 14:11:15 -0700 | [diff] [blame] | 491 | EXPORT_SYMBOL(drm_sysfs_hotplug_event); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 492 | |
David Herrmann | 760c960 | 2013-11-21 20:50:50 +1000 | [diff] [blame] | 493 | static void drm_sysfs_release(struct device *dev) |
| 494 | { |
| 495 | kfree(dev); |
| 496 | } |
| 497 | |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 498 | /** |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 499 | * drm_sysfs_minor_alloc() - Allocate sysfs device for given minor |
| 500 | * @minor: minor to allocate sysfs device for |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 501 | * |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 502 | * This allocates a new sysfs device for @minor and returns it. The device is |
| 503 | * not registered nor linked. The caller has to use device_add() and |
| 504 | * device_del() to register and unregister it. |
| 505 | * |
| 506 | * Note that dev_get_drvdata() on the new device will return the minor. |
| 507 | * However, the device does not hold a ref-count to the minor nor to the |
| 508 | * underlying drm_device. This is unproblematic as long as you access the |
| 509 | * private data only in sysfs callbacks. device_del() disables those |
| 510 | * synchronously, so they cannot be called after you cleanup a minor. |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 511 | */ |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 512 | struct device *drm_sysfs_minor_alloc(struct drm_minor *minor) |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 513 | { |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 514 | const char *minor_str; |
| 515 | struct device *kdev; |
David Herrmann | 760c960 | 2013-11-21 20:50:50 +1000 | [diff] [blame] | 516 | int r; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 517 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 518 | if (minor->type == DRM_MINOR_CONTROL) |
| 519 | minor_str = "controlD%d"; |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 520 | else if (minor->type == DRM_MINOR_RENDER) |
| 521 | minor_str = "renderD%d"; |
| 522 | else |
| 523 | minor_str = "card%d"; |
Jesse Barnes | e8b962b | 2007-11-22 14:02:38 +1000 | [diff] [blame] | 524 | |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 525 | kdev = kzalloc(sizeof(*kdev), GFP_KERNEL); |
| 526 | if (!kdev) |
| 527 | return ERR_PTR(-ENOMEM); |
David Herrmann | 760c960 | 2013-11-21 20:50:50 +1000 | [diff] [blame] | 528 | |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 529 | device_initialize(kdev); |
| 530 | kdev->devt = MKDEV(DRM_MAJOR, minor->index); |
| 531 | kdev->class = drm_class; |
| 532 | kdev->type = &drm_sysfs_device_minor; |
| 533 | kdev->parent = minor->dev->dev; |
| 534 | kdev->release = drm_sysfs_release; |
| 535 | dev_set_drvdata(kdev, minor); |
David Herrmann | 760c960 | 2013-11-21 20:50:50 +1000 | [diff] [blame] | 536 | |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 537 | r = dev_set_name(kdev, minor_str, minor->index); |
David Herrmann | 760c960 | 2013-11-21 20:50:50 +1000 | [diff] [blame] | 538 | if (r < 0) |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 539 | goto err_free; |
David Herrmann | 760c960 | 2013-11-21 20:50:50 +1000 | [diff] [blame] | 540 | |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 541 | return kdev; |
David Herrmann | 760c960 | 2013-11-21 20:50:50 +1000 | [diff] [blame] | 542 | |
David Herrmann | e172807 | 2014-07-23 11:38:38 +0200 | [diff] [blame] | 543 | err_free: |
| 544 | put_device(kdev); |
| 545 | return ERR_PTR(r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /** |
Thomas Hellstrom | 327c225 | 2009-08-17 16:28:37 +0200 | [diff] [blame] | 549 | * drm_class_device_register - Register a struct device in the drm class. |
| 550 | * |
| 551 | * @dev: pointer to struct device to register. |
| 552 | * |
| 553 | * @dev should have all relevant members pre-filled with the exception |
| 554 | * of the class member. In particular, the device_type member must |
| 555 | * be set. |
| 556 | */ |
| 557 | |
| 558 | int drm_class_device_register(struct device *dev) |
| 559 | { |
Dave Airlie | 49099c4 | 2012-07-06 18:06:42 +0100 | [diff] [blame] | 560 | if (!drm_class || IS_ERR(drm_class)) |
| 561 | return -ENOENT; |
| 562 | |
Thomas Hellstrom | 327c225 | 2009-08-17 16:28:37 +0200 | [diff] [blame] | 563 | dev->class = drm_class; |
| 564 | return device_register(dev); |
| 565 | } |
| 566 | EXPORT_SYMBOL_GPL(drm_class_device_register); |
| 567 | |
| 568 | void drm_class_device_unregister(struct device *dev) |
| 569 | { |
| 570 | return device_unregister(dev); |
| 571 | } |
| 572 | EXPORT_SYMBOL_GPL(drm_class_device_unregister); |