blob: c75f02297d01c4799aa360f758851ba0ca7adfec [file] [log] [blame]
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -08001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * drm_sysfs.c - Modifications to drm_sysfs_class.c to support
4 * extra sysfs attribute from DRM. Normal drm_sysfs_class
5 * does not allow adding attributes.
6 *
7 * Copyright (c) 2004 Jon Smirl <jonsmirl@gmail.com>
8 * Copyright (c) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
9 * Copyright (c) 2003-2004 IBM Corp.
10 *
11 * This file is released under the GPLv2
12 *
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/device.h>
16#include <linux/kdev_t.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/err.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040019#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
David Howells760285e2012-10-02 18:01:07 +010021#include <drm/drm_sysfs.h>
22#include <drm/drm_core.h>
23#include <drm/drmP.h>
Daniel Vetter67d0ec42014-09-10 12:43:53 +020024#include "drm_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Dave Airlie5bdebb12013-10-11 14:07:25 +100026#define to_drm_minor(d) dev_get_drvdata(d)
27#define to_drm_connector(d) dev_get_drvdata(d)
Jesse Barnese8b962b2007-11-22 14:02:38 +100028
Thomas Hellstrom08e4d532009-08-20 19:02:31 +100029static struct device_type drm_sysfs_device_minor = {
30 .name = "drm_minor"
31};
32
David Herrmannfcc90212015-09-09 14:21:30 +020033struct class *drm_class;
34
Jesse Barnese8b962b2007-11-22 14:02:38 +100035/**
Shuah Khancf4b91f2013-07-01 16:06:02 -060036 * __drm_class_suspend - internal DRM class suspend routine
Jesse Barnese8b962b2007-11-22 14:02:38 +100037 * @dev: Linux device to suspend
38 * @state: power state to enter
39 *
40 * Just figures out what the actual struct drm_device associated with
41 * @dev is and calls its suspend hook, if present.
42 */
Shuah Khancf4b91f2013-07-01 16:06:02 -060043static int __drm_class_suspend(struct device *dev, pm_message_t state)
Jesse Barnese8b962b2007-11-22 14:02:38 +100044{
Thomas Hellstrom08e4d532009-08-20 19:02:31 +100045 if (dev->type == &drm_sysfs_device_minor) {
46 struct drm_minor *drm_minor = to_drm_minor(dev);
47 struct drm_device *drm_dev = drm_minor->dev;
Jesse Barnese8b962b2007-11-22 14:02:38 +100048
Thomas Hellstrom08e4d532009-08-20 19:02:31 +100049 if (drm_minor->type == DRM_MINOR_LEGACY &&
50 !drm_core_check_feature(drm_dev, DRIVER_MODESET) &&
51 drm_dev->driver->suspend)
52 return drm_dev->driver->suspend(drm_dev, state);
53 }
Jesse Barnese8b962b2007-11-22 14:02:38 +100054 return 0;
55}
56
57/**
Shuah Khancf4b91f2013-07-01 16:06:02 -060058 * drm_class_suspend - internal DRM class suspend hook. Simply calls
59 * __drm_class_suspend() with the correct pm state.
60 * @dev: Linux device to suspend
61 */
62static int drm_class_suspend(struct device *dev)
63{
64 return __drm_class_suspend(dev, PMSG_SUSPEND);
65}
66
67/**
68 * drm_class_freeze - internal DRM class freeze hook. Simply calls
69 * __drm_class_suspend() with the correct pm state.
70 * @dev: Linux device to freeze
71 */
72static int drm_class_freeze(struct device *dev)
73{
74 return __drm_class_suspend(dev, PMSG_FREEZE);
75}
76
77/**
Thomas Hellstrom08e4d532009-08-20 19:02:31 +100078 * drm_class_resume - DRM class resume hook
Jesse Barnese8b962b2007-11-22 14:02:38 +100079 * @dev: Linux device to resume
80 *
81 * Just figures out what the actual struct drm_device associated with
82 * @dev is and calls its resume hook, if present.
83 */
Thomas Hellstrom08e4d532009-08-20 19:02:31 +100084static int drm_class_resume(struct device *dev)
Jesse Barnese8b962b2007-11-22 14:02:38 +100085{
Thomas Hellstrom08e4d532009-08-20 19:02:31 +100086 if (dev->type == &drm_sysfs_device_minor) {
87 struct drm_minor *drm_minor = to_drm_minor(dev);
88 struct drm_device *drm_dev = drm_minor->dev;
Jesse Barnese8b962b2007-11-22 14:02:38 +100089
Thomas Hellstrom08e4d532009-08-20 19:02:31 +100090 if (drm_minor->type == DRM_MINOR_LEGACY &&
91 !drm_core_check_feature(drm_dev, DRIVER_MODESET) &&
92 drm_dev->driver->resume)
93 return drm_dev->driver->resume(drm_dev);
94 }
Jesse Barnese8b962b2007-11-22 14:02:38 +100095 return 0;
96}
97
Shuah Khancf4b91f2013-07-01 16:06:02 -060098static const struct dev_pm_ops drm_class_dev_pm_ops = {
99 .suspend = drm_class_suspend,
100 .resume = drm_class_resume,
101 .freeze = drm_class_freeze,
102};
103
Al Viro2c9ede52011-07-23 20:24:48 -0400104static char *drm_devnode(struct device *dev, umode_t *mode)
Kay Sievers02200d02009-04-30 15:23:42 +0200105{
106 return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
107}
108
Andi Kleen0933e2d2010-01-05 12:48:09 +0100109static CLASS_ATTR_STRING(version, S_IRUGO,
110 CORE_NAME " "
111 __stringify(CORE_MAJOR) "."
112 __stringify(CORE_MINOR) "."
113 __stringify(CORE_PATCHLEVEL) " "
114 CORE_DATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/**
David Herrmannfcc90212015-09-09 14:21:30 +0200117 * drm_sysfs_init - initialize sysfs helpers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 *
David Herrmannfcc90212015-09-09 14:21:30 +0200119 * This is used to create the DRM class, which is the implicit parent of any
120 * other top-level DRM sysfs objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 *
David Herrmannfcc90212015-09-09 14:21:30 +0200122 * You must call drm_sysfs_destroy() to release the allocated resources.
123 *
124 * Return: 0 on success, negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 */
David Herrmannfcc90212015-09-09 14:21:30 +0200126int drm_sysfs_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Jeff Garzik24f73c92006-10-10 14:23:37 -0700128 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
David Herrmannfcc90212015-09-09 14:21:30 +0200130 drm_class = class_create(THIS_MODULE, "drm");
131 if (IS_ERR(drm_class))
132 return PTR_ERR(drm_class);
133
134 drm_class->pm = &drm_class_dev_pm_ops;
135
136 err = class_create_file(drm_class, &class_attr_version.attr);
137 if (err) {
138 class_destroy(drm_class);
139 drm_class = NULL;
140 return err;
Jeff Garzik24f73c92006-10-10 14:23:37 -0700141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
David Herrmannfcc90212015-09-09 14:21:30 +0200143 drm_class->devnode = drm_devnode;
144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147/**
Jesse Barnese8b962b2007-11-22 14:02:38 +1000148 * drm_sysfs_destroy - destroys DRM class
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 *
Jesse Barnese8b962b2007-11-22 14:02:38 +1000150 * Destroy the DRM device class.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 */
Jesse Barnese8b962b2007-11-22 14:02:38 +1000152void drm_sysfs_destroy(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
David Herrmann26b91ae2015-09-09 14:21:29 +0200154 if (IS_ERR_OR_NULL(drm_class))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return;
Andi Kleen0933e2d2010-01-05 12:48:09 +0100156 class_remove_file(drm_class, &class_attr_version.attr);
Jesse Barnese8b962b2007-11-22 14:02:38 +1000157 class_destroy(drm_class);
Dave Airlie49099c42012-07-06 18:06:42 +0100158 drm_class = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Dave Airlief453ba02008-11-07 14:05:41 -0800161/*
162 * Connector properties
163 */
Chris Wilsonc484f022015-03-06 12:36:42 +0000164static ssize_t status_store(struct device *device,
165 struct device_attribute *attr,
166 const char *buf, size_t count)
167{
168 struct drm_connector *connector = to_drm_connector(device);
169 struct drm_device *dev = connector->dev;
170 enum drm_connector_status old_status;
171 int ret;
172
173 ret = mutex_lock_interruptible(&dev->mode_config.mutex);
174 if (ret)
175 return ret;
176
177 old_status = connector->status;
178
179 if (sysfs_streq(buf, "detect")) {
180 connector->force = 0;
181 connector->status = connector->funcs->detect(connector, true);
182 } else if (sysfs_streq(buf, "on")) {
183 connector->force = DRM_FORCE_ON;
184 } else if (sysfs_streq(buf, "on-digital")) {
185 connector->force = DRM_FORCE_ON_DIGITAL;
186 } else if (sysfs_streq(buf, "off")) {
187 connector->force = DRM_FORCE_OFF;
188 } else
189 ret = -EINVAL;
190
191 if (ret == 0 && connector->force) {
192 if (connector->force == DRM_FORCE_ON ||
193 connector->force == DRM_FORCE_ON_DIGITAL)
194 connector->status = connector_status_connected;
195 else
196 connector->status = connector_status_disconnected;
197 if (connector->funcs->force)
198 connector->funcs->force(connector);
199 }
200
201 if (old_status != connector->status) {
202 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
203 connector->base.id,
204 connector->name,
205 old_status, connector->status);
206
207 dev->mode_config.delayed_event = true;
208 if (dev->mode_config.poll_enabled)
209 schedule_delayed_work(&dev->mode_config.output_poll_work,
210 0);
211 }
212
213 mutex_unlock(&dev->mode_config.mutex);
214
Russell King38d85712015-06-06 08:27:30 +1000215 return ret ? ret : count;
Chris Wilsonc484f022015-03-06 12:36:42 +0000216}
217
Dave Airlief453ba02008-11-07 14:05:41 -0800218static ssize_t status_show(struct device *device,
219 struct device_attribute *attr,
220 char *buf)
221{
222 struct drm_connector *connector = to_drm_connector(device);
Chris Wilson007c80a2011-03-15 11:40:00 +0000223
Keith Packard75185c92009-05-30 20:42:25 -0700224 return snprintf(buf, PAGE_SIZE, "%s\n",
Chris Wilsonc484f022015-03-06 12:36:42 +0000225 drm_get_connector_status_name(connector->status));
Dave Airlief453ba02008-11-07 14:05:41 -0800226}
227
228static ssize_t dpms_show(struct device *device,
229 struct device_attribute *attr,
230 char *buf)
231{
232 struct drm_connector *connector = to_drm_connector(device);
Daniel Vetter621bd0f2015-09-29 09:56:53 +0200233 int dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800234
Daniel Vetter621bd0f2015-09-29 09:56:53 +0200235 dpms = READ_ONCE(connector->dpms);
Dave Airlief453ba02008-11-07 14:05:41 -0800236
Keith Packard75185c92009-05-30 20:42:25 -0700237 return snprintf(buf, PAGE_SIZE, "%s\n",
Daniel Vetter621bd0f2015-09-29 09:56:53 +0200238 drm_get_dpms_name(dpms));
Dave Airlief453ba02008-11-07 14:05:41 -0800239}
240
241static ssize_t enabled_show(struct device *device,
242 struct device_attribute *attr,
243 char *buf)
244{
245 struct drm_connector *connector = to_drm_connector(device);
246
Keith Packard75185c92009-05-30 20:42:25 -0700247 return snprintf(buf, PAGE_SIZE, "%s\n", connector->encoder ? "enabled" :
Dave Airlief453ba02008-11-07 14:05:41 -0800248 "disabled");
249}
250
Chris Wright2c3c8be2010-05-12 18:28:57 -0700251static ssize_t edid_show(struct file *filp, struct kobject *kobj,
252 struct bin_attribute *attr, char *buf, loff_t off,
253 size_t count)
Dave Airlief453ba02008-11-07 14:05:41 -0800254{
255 struct device *connector_dev = container_of(kobj, struct device, kobj);
256 struct drm_connector *connector = to_drm_connector(connector_dev);
257 unsigned char *edid;
258 size_t size;
Daniel Vettera48a62b2015-10-02 13:01:02 +0200259 ssize_t ret = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800260
Daniel Vettera48a62b2015-10-02 13:01:02 +0200261 mutex_lock(&connector->dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800262 if (!connector->edid_blob_ptr)
Daniel Vettera48a62b2015-10-02 13:01:02 +0200263 goto unlock;
Dave Airlief453ba02008-11-07 14:05:41 -0800264
265 edid = connector->edid_blob_ptr->data;
266 size = connector->edid_blob_ptr->length;
267 if (!edid)
Daniel Vettera48a62b2015-10-02 13:01:02 +0200268 goto unlock;
Dave Airlief453ba02008-11-07 14:05:41 -0800269
270 if (off >= size)
Daniel Vettera48a62b2015-10-02 13:01:02 +0200271 goto unlock;
Dave Airlief453ba02008-11-07 14:05:41 -0800272
273 if (off + count > size)
274 count = size - off;
275 memcpy(buf, edid + off, count);
276
Daniel Vettera48a62b2015-10-02 13:01:02 +0200277 ret = count;
278unlock:
279 mutex_unlock(&connector->dev->mode_config.mutex);
280
281 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800282}
283
284static ssize_t modes_show(struct device *device,
285 struct device_attribute *attr,
286 char *buf)
287{
288 struct drm_connector *connector = to_drm_connector(device);
289 struct drm_display_mode *mode;
290 int written = 0;
291
Daniel Vettera48a62b2015-10-02 13:01:02 +0200292 mutex_lock(&connector->dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800293 list_for_each_entry(mode, &connector->modes, head) {
294 written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
295 mode->name);
296 }
Daniel Vettera48a62b2015-10-02 13:01:02 +0200297 mutex_unlock(&connector->dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800298
299 return written;
300}
301
Jani Nikula4bb46372015-05-12 12:14:54 +0300302static ssize_t tv_subconnector_show(struct device *device,
303 struct device_attribute *attr,
304 char *buf)
Dave Airlief453ba02008-11-07 14:05:41 -0800305{
306 struct drm_connector *connector = to_drm_connector(device);
307 struct drm_device *dev = connector->dev;
Jani Nikula4bb46372015-05-12 12:14:54 +0300308 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -0800309 uint64_t subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800310 int ret;
311
Jani Nikula4bb46372015-05-12 12:14:54 +0300312 prop = dev->mode_config.tv_subconnector_property;
Dave Airlief453ba02008-11-07 14:05:41 -0800313 if (!prop) {
314 DRM_ERROR("Unable to find subconnector property\n");
315 return 0;
316 }
317
Rob Clark58495562012-10-11 20:50:56 -0500318 ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
Dave Airlief453ba02008-11-07 14:05:41 -0800319 if (ret)
320 return 0;
321
Jani Nikula4bb46372015-05-12 12:14:54 +0300322 return snprintf(buf, PAGE_SIZE, "%s",
323 drm_get_tv_subconnector_name((int)subconnector));
Dave Airlief453ba02008-11-07 14:05:41 -0800324}
325
Jani Nikula4bb46372015-05-12 12:14:54 +0300326static ssize_t tv_select_subconnector_show(struct device *device,
327 struct device_attribute *attr,
328 char *buf)
Dave Airlief453ba02008-11-07 14:05:41 -0800329{
330 struct drm_connector *connector = to_drm_connector(device);
331 struct drm_device *dev = connector->dev;
Jani Nikula4bb46372015-05-12 12:14:54 +0300332 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -0800333 uint64_t subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800334 int ret;
335
Jani Nikula4bb46372015-05-12 12:14:54 +0300336 prop = dev->mode_config.tv_select_subconnector_property;
Dave Airlief453ba02008-11-07 14:05:41 -0800337 if (!prop) {
338 DRM_ERROR("Unable to find select subconnector property\n");
339 return 0;
340 }
341
Rob Clark58495562012-10-11 20:50:56 -0500342 ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
Dave Airlief453ba02008-11-07 14:05:41 -0800343 if (ret)
344 return 0;
345
Jani Nikula4bb46372015-05-12 12:14:54 +0300346 return snprintf(buf, PAGE_SIZE, "%s",
347 drm_get_tv_select_name((int)subconnector));
348}
349
350static ssize_t dvii_subconnector_show(struct device *device,
351 struct device_attribute *attr,
352 char *buf)
353{
354 struct drm_connector *connector = to_drm_connector(device);
355 struct drm_device *dev = connector->dev;
356 struct drm_property *prop;
357 uint64_t subconnector;
358 int ret;
359
Jani Nikula4bb46372015-05-12 12:14:54 +0300360 prop = dev->mode_config.dvi_i_subconnector_property;
361 if (!prop) {
362 DRM_ERROR("Unable to find subconnector property\n");
363 return 0;
364 }
365
366 ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
367 if (ret)
368 return 0;
369
370 return snprintf(buf, PAGE_SIZE, "%s",
371 drm_get_dvi_i_subconnector_name((int)subconnector));
372}
373
374static ssize_t dvii_select_subconnector_show(struct device *device,
375 struct device_attribute *attr,
376 char *buf)
377{
378 struct drm_connector *connector = to_drm_connector(device);
379 struct drm_device *dev = connector->dev;
380 struct drm_property *prop;
381 uint64_t subconnector;
382 int ret;
383
Jani Nikula4bb46372015-05-12 12:14:54 +0300384 prop = dev->mode_config.dvi_i_select_subconnector_property;
385 if (!prop) {
386 DRM_ERROR("Unable to find select subconnector property\n");
387 return 0;
388 }
389
390 ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
391 if (ret)
392 return 0;
393
394 return snprintf(buf, PAGE_SIZE, "%s",
Dave Airlief453ba02008-11-07 14:05:41 -0800395 drm_get_dvi_i_select_name((int)subconnector));
396}
397
Chris Wilsonc484f022015-03-06 12:36:42 +0000398static DEVICE_ATTR_RW(status);
Takashi Iwai335f1a62015-02-04 11:58:53 +0100399static DEVICE_ATTR_RO(enabled);
400static DEVICE_ATTR_RO(dpms);
401static DEVICE_ATTR_RO(modes);
402
403static struct attribute *connector_dev_attrs[] = {
404 &dev_attr_status.attr,
405 &dev_attr_enabled.attr,
406 &dev_attr_dpms.attr,
407 &dev_attr_modes.attr,
408 NULL
Dave Airlief453ba02008-11-07 14:05:41 -0800409};
410
Jani Nikula4bb46372015-05-12 12:14:54 +0300411static DEVICE_ATTR_RO(tv_subconnector);
412static DEVICE_ATTR_RO(tv_select_subconnector);
Takashi Iwai335f1a62015-02-04 11:58:53 +0100413
Jani Nikulafe6fcdd2015-05-12 12:14:53 +0300414static struct attribute *connector_tv_dev_attrs[] = {
Jani Nikula4bb46372015-05-12 12:14:54 +0300415 &dev_attr_tv_subconnector.attr,
416 &dev_attr_tv_select_subconnector.attr,
Takashi Iwai335f1a62015-02-04 11:58:53 +0100417 NULL
Dave Airlief453ba02008-11-07 14:05:41 -0800418};
419
Jani Nikula4bb46372015-05-12 12:14:54 +0300420static DEVICE_ATTR_RO(dvii_subconnector);
421static DEVICE_ATTR_RO(dvii_select_subconnector);
422
423static struct attribute *connector_dvii_dev_attrs[] = {
424 &dev_attr_dvii_subconnector.attr,
425 &dev_attr_dvii_select_subconnector.attr,
Dave Airlief453ba02008-11-07 14:05:41 -0800426 NULL
427};
428
Jani Nikula7d4d3a52015-05-12 12:14:52 +0300429/* Connector type related helpers */
430static int kobj_connector_type(struct kobject *kobj)
Takashi Iwai335f1a62015-02-04 11:58:53 +0100431{
432 struct device *dev = kobj_to_dev(kobj);
433 struct drm_connector *connector = to_drm_connector(dev);
434
Jani Nikula7d4d3a52015-05-12 12:14:52 +0300435 return connector->connector_type;
436}
437
Jani Nikulafe6fcdd2015-05-12 12:14:53 +0300438static umode_t connector_is_dvii(struct kobject *kobj,
439 struct attribute *attr, int idx)
Jani Nikula7d4d3a52015-05-12 12:14:52 +0300440{
Jani Nikulafe6fcdd2015-05-12 12:14:53 +0300441 return kobj_connector_type(kobj) == DRM_MODE_CONNECTOR_DVII ?
442 attr->mode : 0;
443}
444
445static umode_t connector_is_tv(struct kobject *kobj,
446 struct attribute *attr, int idx)
447{
Jani Nikula7d4d3a52015-05-12 12:14:52 +0300448 switch (kobj_connector_type(kobj)) {
Takashi Iwai335f1a62015-02-04 11:58:53 +0100449 case DRM_MODE_CONNECTOR_Composite:
450 case DRM_MODE_CONNECTOR_SVIDEO:
451 case DRM_MODE_CONNECTOR_Component:
452 case DRM_MODE_CONNECTOR_TV:
453 return attr->mode;
454 }
455
456 return 0;
457}
458
Dave Airlief453ba02008-11-07 14:05:41 -0800459static struct bin_attribute edid_attr = {
460 .attr.name = "edid",
Keith Packarde36ebaf2009-05-30 20:42:26 -0700461 .attr.mode = 0444,
Adam Jackson7466f4c2010-03-29 21:43:23 +0000462 .size = 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800463 .read = edid_show,
464};
465
Takashi Iwai335f1a62015-02-04 11:58:53 +0100466static struct bin_attribute *connector_bin_attrs[] = {
467 &edid_attr,
468 NULL
469};
470
471static const struct attribute_group connector_dev_group = {
472 .attrs = connector_dev_attrs,
473 .bin_attrs = connector_bin_attrs,
474};
475
Jani Nikulafe6fcdd2015-05-12 12:14:53 +0300476static const struct attribute_group connector_tv_dev_group = {
477 .attrs = connector_tv_dev_attrs,
478 .is_visible = connector_is_tv,
479};
480
481static const struct attribute_group connector_dvii_dev_group = {
Jani Nikula4bb46372015-05-12 12:14:54 +0300482 .attrs = connector_dvii_dev_attrs,
Jani Nikulafe6fcdd2015-05-12 12:14:53 +0300483 .is_visible = connector_is_dvii,
Takashi Iwai335f1a62015-02-04 11:58:53 +0100484};
485
486static const struct attribute_group *connector_dev_groups[] = {
487 &connector_dev_group,
Jani Nikulafe6fcdd2015-05-12 12:14:53 +0300488 &connector_tv_dev_group,
489 &connector_dvii_dev_group,
Takashi Iwai335f1a62015-02-04 11:58:53 +0100490 NULL
491};
492
Dave Airlief453ba02008-11-07 14:05:41 -0800493/**
Laurent Pinchart3b02ab82012-05-17 13:27:20 +0200494 * drm_sysfs_connector_add - add a connector to sysfs
Dave Airlief453ba02008-11-07 14:05:41 -0800495 * @connector: connector to add
496 *
Laurent Pinchart3b02ab82012-05-17 13:27:20 +0200497 * Create a connector device in sysfs, along with its associated connector
Dave Airlief453ba02008-11-07 14:05:41 -0800498 * properties (so far, connection status, dpms, mode list & edid) and
499 * generate a hotplug event so userspace knows there's a new connector
500 * available.
Dave Airlief453ba02008-11-07 14:05:41 -0800501 */
502int drm_sysfs_connector_add(struct drm_connector *connector)
503{
504 struct drm_device *dev = connector->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800505
Dave Airlie5bdebb12013-10-11 14:07:25 +1000506 if (connector->kdev)
507 return 0;
508
Takashi Iwai335f1a62015-02-04 11:58:53 +0100509 connector->kdev =
510 device_create_with_groups(drm_class, dev->primary->kdev, 0,
511 connector, connector_dev_groups,
512 "card%d-%s", dev->primary->index,
513 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800514 DRM_DEBUG("adding \"%s\" to sysfs\n",
Jani Nikula25933822014-06-03 14:56:20 +0300515 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800516
Dave Airlie5bdebb12013-10-11 14:07:25 +1000517 if (IS_ERR(connector->kdev)) {
518 DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector->kdev));
Takashi Iwai335f1a62015-02-04 11:58:53 +0100519 return PTR_ERR(connector->kdev);
Dave Airlief453ba02008-11-07 14:05:41 -0800520 }
521
Dave Airlief453ba02008-11-07 14:05:41 -0800522 /* Let userspace know we have a new connector */
523 drm_sysfs_hotplug_event(dev);
524
525 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800526}
Dave Airlief453ba02008-11-07 14:05:41 -0800527
528/**
529 * drm_sysfs_connector_remove - remove an connector device from sysfs
530 * @connector: connector to remove
531 *
532 * Remove @connector and its associated attributes from sysfs. Note that
533 * the device model core will take care of sending the "remove" uevent
534 * at this time, so we don't need to do it.
535 *
536 * Note:
537 * This routine should only be called if the connector was previously
538 * successfully registered. If @connector hasn't been registered yet,
539 * you'll likely see a panic somewhere deep in sysfs code when called.
540 */
541void drm_sysfs_connector_remove(struct drm_connector *connector)
542{
Dave Airlie5bdebb12013-10-11 14:07:25 +1000543 if (!connector->kdev)
Dave Airlie1828fe62012-02-20 14:15:02 +0000544 return;
Dave Airlief453ba02008-11-07 14:05:41 -0800545 DRM_DEBUG("removing \"%s\" from sysfs\n",
Jani Nikula25933822014-06-03 14:56:20 +0300546 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800547
Dave Airlie5bdebb12013-10-11 14:07:25 +1000548 device_unregister(connector->kdev);
549 connector->kdev = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800550}
Dave Airlief453ba02008-11-07 14:05:41 -0800551
552/**
553 * drm_sysfs_hotplug_event - generate a DRM uevent
554 * @dev: DRM device
555 *
556 * Send a uevent for the DRM device specified by @dev. Currently we only
557 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
558 * deal with other types of events.
559 */
560void drm_sysfs_hotplug_event(struct drm_device *dev)
561{
562 char *event_string = "HOTPLUG=1";
563 char *envp[] = { event_string, NULL };
564
565 DRM_DEBUG("generating hotplug event\n");
566
Dave Airlie5bdebb12013-10-11 14:07:25 +1000567 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
Dave Airlief453ba02008-11-07 14:05:41 -0800568}
Jesse Barnes5ca58282009-03-31 14:11:15 -0700569EXPORT_SYMBOL(drm_sysfs_hotplug_event);
Dave Airlief453ba02008-11-07 14:05:41 -0800570
David Herrmann760c9602013-11-21 20:50:50 +1000571static void drm_sysfs_release(struct device *dev)
572{
573 kfree(dev);
574}
575
Jesse Barnese8b962b2007-11-22 14:02:38 +1000576/**
David Herrmanne1728072014-07-23 11:38:38 +0200577 * drm_sysfs_minor_alloc() - Allocate sysfs device for given minor
578 * @minor: minor to allocate sysfs device for
Jesse Barnese8b962b2007-11-22 14:02:38 +1000579 *
David Herrmanne1728072014-07-23 11:38:38 +0200580 * This allocates a new sysfs device for @minor and returns it. The device is
581 * not registered nor linked. The caller has to use device_add() and
582 * device_del() to register and unregister it.
583 *
584 * Note that dev_get_drvdata() on the new device will return the minor.
585 * However, the device does not hold a ref-count to the minor nor to the
586 * underlying drm_device. This is unproblematic as long as you access the
587 * private data only in sysfs callbacks. device_del() disables those
588 * synchronously, so they cannot be called after you cleanup a minor.
Jesse Barnese8b962b2007-11-22 14:02:38 +1000589 */
David Herrmanne1728072014-07-23 11:38:38 +0200590struct device *drm_sysfs_minor_alloc(struct drm_minor *minor)
Jesse Barnese8b962b2007-11-22 14:02:38 +1000591{
David Herrmanne1728072014-07-23 11:38:38 +0200592 const char *minor_str;
593 struct device *kdev;
David Herrmann760c9602013-11-21 20:50:50 +1000594 int r;
Jesse Barnese8b962b2007-11-22 14:02:38 +1000595
Dave Airlief453ba02008-11-07 14:05:41 -0800596 if (minor->type == DRM_MINOR_CONTROL)
597 minor_str = "controlD%d";
David Herrmanne1728072014-07-23 11:38:38 +0200598 else if (minor->type == DRM_MINOR_RENDER)
599 minor_str = "renderD%d";
600 else
601 minor_str = "card%d";
Jesse Barnese8b962b2007-11-22 14:02:38 +1000602
David Herrmanne1728072014-07-23 11:38:38 +0200603 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
604 if (!kdev)
605 return ERR_PTR(-ENOMEM);
David Herrmann760c9602013-11-21 20:50:50 +1000606
David Herrmanne1728072014-07-23 11:38:38 +0200607 device_initialize(kdev);
608 kdev->devt = MKDEV(DRM_MAJOR, minor->index);
609 kdev->class = drm_class;
610 kdev->type = &drm_sysfs_device_minor;
611 kdev->parent = minor->dev->dev;
612 kdev->release = drm_sysfs_release;
613 dev_set_drvdata(kdev, minor);
David Herrmann760c9602013-11-21 20:50:50 +1000614
David Herrmanne1728072014-07-23 11:38:38 +0200615 r = dev_set_name(kdev, minor_str, minor->index);
David Herrmann760c9602013-11-21 20:50:50 +1000616 if (r < 0)
David Herrmanne1728072014-07-23 11:38:38 +0200617 goto err_free;
David Herrmann760c9602013-11-21 20:50:50 +1000618
David Herrmanne1728072014-07-23 11:38:38 +0200619 return kdev;
David Herrmann760c9602013-11-21 20:50:50 +1000620
David Herrmanne1728072014-07-23 11:38:38 +0200621err_free:
622 put_device(kdev);
623 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
626/**
Thomas Hellstrom327c2252009-08-17 16:28:37 +0200627 * drm_class_device_register - Register a struct device in the drm class.
628 *
629 * @dev: pointer to struct device to register.
630 *
631 * @dev should have all relevant members pre-filled with the exception
632 * of the class member. In particular, the device_type member must
633 * be set.
634 */
635
636int drm_class_device_register(struct device *dev)
637{
Dave Airlie49099c42012-07-06 18:06:42 +0100638 if (!drm_class || IS_ERR(drm_class))
639 return -ENOENT;
640
Thomas Hellstrom327c2252009-08-17 16:28:37 +0200641 dev->class = drm_class;
642 return device_register(dev);
643}
644EXPORT_SYMBOL_GPL(drm_class_device_register);
645
646void drm_class_device_unregister(struct device *dev)
647{
648 return device_unregister(dev);
649}
650EXPORT_SYMBOL_GPL(drm_class_device_unregister);