blob: ffc305fc20768c29af6883eeb2d70553839cfa6e [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
Jesse Barnese8b962b2007-11-22 14:02:38 +100033/**
Shuah Khancf4b91f2013-07-01 16:06:02 -060034 * __drm_class_suspend - internal DRM class suspend routine
Jesse Barnese8b962b2007-11-22 14:02:38 +100035 * @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 Khancf4b91f2013-07-01 16:06:02 -060041static int __drm_class_suspend(struct device *dev, pm_message_t state)
Jesse Barnese8b962b2007-11-22 14:02:38 +100042{
Thomas Hellstrom08e4d532009-08-20 19:02:31 +100043 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 Barnese8b962b2007-11-22 14:02:38 +100046
Thomas Hellstrom08e4d532009-08-20 19:02:31 +100047 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 Barnese8b962b2007-11-22 14:02:38 +100052 return 0;
53}
54
55/**
Shuah Khancf4b91f2013-07-01 16:06:02 -060056 * 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 */
60static 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 */
70static int drm_class_freeze(struct device *dev)
71{
72 return __drm_class_suspend(dev, PMSG_FREEZE);
73}
74
75/**
Thomas Hellstrom08e4d532009-08-20 19:02:31 +100076 * drm_class_resume - DRM class resume hook
Jesse Barnese8b962b2007-11-22 14:02:38 +100077 * @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 Hellstrom08e4d532009-08-20 19:02:31 +100082static int drm_class_resume(struct device *dev)
Jesse Barnese8b962b2007-11-22 14:02:38 +100083{
Thomas Hellstrom08e4d532009-08-20 19:02:31 +100084 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 Barnese8b962b2007-11-22 14:02:38 +100087
Thomas Hellstrom08e4d532009-08-20 19:02:31 +100088 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 Barnese8b962b2007-11-22 14:02:38 +100093 return 0;
94}
95
Shuah Khancf4b91f2013-07-01 16:06:02 -060096static 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 Viro2c9ede52011-07-23 20:24:48 -0400102static char *drm_devnode(struct device *dev, umode_t *mode)
Kay Sievers02200d02009-04-30 15:23:42 +0200103{
104 return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
105}
106
Andi Kleen0933e2d2010-01-05 12:48:09 +0100107static 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 Torvalds1da177e2005-04-16 15:20:36 -0700113
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 Barnese8b962b2007-11-22 14:02:38 +1000119 * This is used to create DRM class pointer that can then be used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * 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-Hartman0650fd52006-01-20 14:08:59 -0800125struct class *drm_sysfs_create(struct module *owner, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -0800127 struct class *class;
Jeff Garzik24f73c92006-10-10 14:23:37 -0700128 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -0800130 class = class_create(owner, name);
Akinobu Mita94f060b2006-12-09 10:49:47 +1100131 if (IS_ERR(class)) {
132 err = PTR_ERR(class);
Jeff Garzik24f73c92006-10-10 14:23:37 -0700133 goto err_out;
134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Shuah Khancf4b91f2013-07-01 16:06:02 -0600136 class->pm = &drm_class_dev_pm_ops;
Jesse Barnese8b962b2007-11-22 14:02:38 +1000137
Andi Kleen0933e2d2010-01-05 12:48:09 +0100138 err = class_create_file(class, &class_attr_version.attr);
Jeff Garzik24f73c92006-10-10 14:23:37 -0700139 if (err)
140 goto err_out_class;
141
Kay Sieverse454cea2009-09-18 23:01:12 +0200142 class->devnode = drm_devnode;
Kay Sievers02200d02009-04-30 15:23:42 +0200143
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -0800144 return class;
Jeff Garzik24f73c92006-10-10 14:23:37 -0700145
146err_out_class:
147 class_destroy(class);
148err_out:
149 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152/**
Jesse Barnese8b962b2007-11-22 14:02:38 +1000153 * drm_sysfs_destroy - destroys DRM class
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 *
Jesse Barnese8b962b2007-11-22 14:02:38 +1000155 * Destroy the DRM device class.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
Jesse Barnese8b962b2007-11-22 14:02:38 +1000157void drm_sysfs_destroy(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Jesse Barnese8b962b2007-11-22 14:02:38 +1000159 if ((drm_class == NULL) || (IS_ERR(drm_class)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return;
Andi Kleen0933e2d2010-01-05 12:48:09 +0100161 class_remove_file(drm_class, &class_attr_version.attr);
Jesse Barnese8b962b2007-11-22 14:02:38 +1000162 class_destroy(drm_class);
Dave Airlie49099c42012-07-06 18:06:42 +0100163 drm_class = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Dave Airlief453ba02008-11-07 14:05:41 -0800166/*
167 * Connector properties
168 */
Chris Wilsonc484f022015-03-06 12:36:42 +0000169static 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
220 return ret;
221}
222
Dave Airlief453ba02008-11-07 14:05:41 -0800223static 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 Wilson007c80a2011-03-15 11:40:00 +0000228
Keith Packard75185c92009-05-30 20:42:25 -0700229 return snprintf(buf, PAGE_SIZE, "%s\n",
Chris Wilsonc484f022015-03-06 12:36:42 +0000230 drm_get_connector_status_name(connector->status));
Dave Airlief453ba02008-11-07 14:05:41 -0800231}
232
233static 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 Clark58495562012-10-11 20:50:56 -0500242 ret = drm_object_property_get_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800243 dev->mode_config.dpms_property,
244 &dpms_status);
245 if (ret)
246 return 0;
247
Keith Packard75185c92009-05-30 20:42:25 -0700248 return snprintf(buf, PAGE_SIZE, "%s\n",
Dave Airlief453ba02008-11-07 14:05:41 -0800249 drm_get_dpms_name((int)dpms_status));
250}
251
252static 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 Packard75185c92009-05-30 20:42:25 -0700258 return snprintf(buf, PAGE_SIZE, "%s\n", connector->encoder ? "enabled" :
Dave Airlief453ba02008-11-07 14:05:41 -0800259 "disabled");
260}
261
Chris Wright2c3c8be2010-05-12 18:28:57 -0700262static 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 Airlief453ba02008-11-07 14:05:41 -0800265{
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
289static 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
305static ssize_t subconnector_show(struct device *device,
306 struct device_attribute *attr,
307 char *buf)
308{
309 struct drm_connector *connector = to_drm_connector(device);
310 struct drm_device *dev = connector->dev;
311 struct drm_property *prop = NULL;
312 uint64_t subconnector;
313 int is_tv = 0;
314 int ret;
315
316 switch (connector->connector_type) {
317 case DRM_MODE_CONNECTOR_DVII:
318 prop = dev->mode_config.dvi_i_subconnector_property;
319 break;
320 case DRM_MODE_CONNECTOR_Composite:
321 case DRM_MODE_CONNECTOR_SVIDEO:
322 case DRM_MODE_CONNECTOR_Component:
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200323 case DRM_MODE_CONNECTOR_TV:
Dave Airlief453ba02008-11-07 14:05:41 -0800324 prop = dev->mode_config.tv_subconnector_property;
325 is_tv = 1;
326 break;
327 default:
328 DRM_ERROR("Wrong connector type for this property\n");
329 return 0;
330 }
331
332 if (!prop) {
333 DRM_ERROR("Unable to find subconnector property\n");
334 return 0;
335 }
336
Rob Clark58495562012-10-11 20:50:56 -0500337 ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
Dave Airlief453ba02008-11-07 14:05:41 -0800338 if (ret)
339 return 0;
340
341 return snprintf(buf, PAGE_SIZE, "%s", is_tv ?
342 drm_get_tv_subconnector_name((int)subconnector) :
343 drm_get_dvi_i_subconnector_name((int)subconnector));
344}
345
346static ssize_t select_subconnector_show(struct device *device,
347 struct device_attribute *attr,
348 char *buf)
349{
350 struct drm_connector *connector = to_drm_connector(device);
351 struct drm_device *dev = connector->dev;
352 struct drm_property *prop = NULL;
353 uint64_t subconnector;
354 int is_tv = 0;
355 int ret;
356
357 switch (connector->connector_type) {
358 case DRM_MODE_CONNECTOR_DVII:
359 prop = dev->mode_config.dvi_i_select_subconnector_property;
360 break;
361 case DRM_MODE_CONNECTOR_Composite:
362 case DRM_MODE_CONNECTOR_SVIDEO:
363 case DRM_MODE_CONNECTOR_Component:
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200364 case DRM_MODE_CONNECTOR_TV:
Dave Airlief453ba02008-11-07 14:05:41 -0800365 prop = dev->mode_config.tv_select_subconnector_property;
366 is_tv = 1;
367 break;
368 default:
369 DRM_ERROR("Wrong connector type for this property\n");
370 return 0;
371 }
372
373 if (!prop) {
374 DRM_ERROR("Unable to find select subconnector property\n");
375 return 0;
376 }
377
Rob Clark58495562012-10-11 20:50:56 -0500378 ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
Dave Airlief453ba02008-11-07 14:05:41 -0800379 if (ret)
380 return 0;
381
382 return snprintf(buf, PAGE_SIZE, "%s", is_tv ?
383 drm_get_tv_select_name((int)subconnector) :
384 drm_get_dvi_i_select_name((int)subconnector));
385}
386
Chris Wilsonc484f022015-03-06 12:36:42 +0000387static DEVICE_ATTR_RW(status);
Takashi Iwai335f1a62015-02-04 11:58:53 +0100388static DEVICE_ATTR_RO(enabled);
389static DEVICE_ATTR_RO(dpms);
390static DEVICE_ATTR_RO(modes);
391
392static struct attribute *connector_dev_attrs[] = {
393 &dev_attr_status.attr,
394 &dev_attr_enabled.attr,
395 &dev_attr_dpms.attr,
396 &dev_attr_modes.attr,
397 NULL
Dave Airlief453ba02008-11-07 14:05:41 -0800398};
399
400/* These attributes are for both DVI-I connectors and all types of tv-out. */
Takashi Iwai335f1a62015-02-04 11:58:53 +0100401static DEVICE_ATTR_RO(subconnector);
402static DEVICE_ATTR_RO(select_subconnector);
403
404static struct attribute *connector_opt_dev_attrs[] = {
405 &dev_attr_subconnector.attr,
406 &dev_attr_select_subconnector.attr,
407 NULL
Dave Airlief453ba02008-11-07 14:05:41 -0800408};
409
Takashi Iwai335f1a62015-02-04 11:58:53 +0100410static umode_t connector_opt_dev_is_visible(struct kobject *kobj,
411 struct attribute *attr, int idx)
412{
413 struct device *dev = kobj_to_dev(kobj);
414 struct drm_connector *connector = to_drm_connector(dev);
415
416 /*
417 * In the long run it maybe a good idea to make one set of
418 * optionals per connector type.
419 */
420 switch (connector->connector_type) {
421 case DRM_MODE_CONNECTOR_DVII:
422 case DRM_MODE_CONNECTOR_Composite:
423 case DRM_MODE_CONNECTOR_SVIDEO:
424 case DRM_MODE_CONNECTOR_Component:
425 case DRM_MODE_CONNECTOR_TV:
426 return attr->mode;
427 }
428
429 return 0;
430}
431
Dave Airlief453ba02008-11-07 14:05:41 -0800432static struct bin_attribute edid_attr = {
433 .attr.name = "edid",
Keith Packarde36ebaf2009-05-30 20:42:26 -0700434 .attr.mode = 0444,
Adam Jackson7466f4c2010-03-29 21:43:23 +0000435 .size = 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800436 .read = edid_show,
437};
438
Takashi Iwai335f1a62015-02-04 11:58:53 +0100439static struct bin_attribute *connector_bin_attrs[] = {
440 &edid_attr,
441 NULL
442};
443
444static const struct attribute_group connector_dev_group = {
445 .attrs = connector_dev_attrs,
446 .bin_attrs = connector_bin_attrs,
447};
448
449static const struct attribute_group connector_opt_dev_group = {
450 .attrs = connector_opt_dev_attrs,
451 .is_visible = connector_opt_dev_is_visible,
452};
453
454static const struct attribute_group *connector_dev_groups[] = {
455 &connector_dev_group,
456 &connector_opt_dev_group,
457 NULL
458};
459
Dave Airlief453ba02008-11-07 14:05:41 -0800460/**
Laurent Pinchart3b02ab82012-05-17 13:27:20 +0200461 * drm_sysfs_connector_add - add a connector to sysfs
Dave Airlief453ba02008-11-07 14:05:41 -0800462 * @connector: connector to add
463 *
Laurent Pinchart3b02ab82012-05-17 13:27:20 +0200464 * Create a connector device in sysfs, along with its associated connector
Dave Airlief453ba02008-11-07 14:05:41 -0800465 * properties (so far, connection status, dpms, mode list & edid) and
466 * generate a hotplug event so userspace knows there's a new connector
467 * available.
Dave Airlief453ba02008-11-07 14:05:41 -0800468 */
469int drm_sysfs_connector_add(struct drm_connector *connector)
470{
471 struct drm_device *dev = connector->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800472
Dave Airlie5bdebb12013-10-11 14:07:25 +1000473 if (connector->kdev)
474 return 0;
475
Takashi Iwai335f1a62015-02-04 11:58:53 +0100476 connector->kdev =
477 device_create_with_groups(drm_class, dev->primary->kdev, 0,
478 connector, connector_dev_groups,
479 "card%d-%s", dev->primary->index,
480 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800481 DRM_DEBUG("adding \"%s\" to sysfs\n",
Jani Nikula25933822014-06-03 14:56:20 +0300482 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800483
Dave Airlie5bdebb12013-10-11 14:07:25 +1000484 if (IS_ERR(connector->kdev)) {
485 DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector->kdev));
Takashi Iwai335f1a62015-02-04 11:58:53 +0100486 return PTR_ERR(connector->kdev);
Dave Airlief453ba02008-11-07 14:05:41 -0800487 }
488
Dave Airlief453ba02008-11-07 14:05:41 -0800489 /* Let userspace know we have a new connector */
490 drm_sysfs_hotplug_event(dev);
491
492 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800493}
Dave Airlief453ba02008-11-07 14:05:41 -0800494
495/**
496 * drm_sysfs_connector_remove - remove an connector device from sysfs
497 * @connector: connector to remove
498 *
499 * Remove @connector and its associated attributes from sysfs. Note that
500 * the device model core will take care of sending the "remove" uevent
501 * at this time, so we don't need to do it.
502 *
503 * Note:
504 * This routine should only be called if the connector was previously
505 * successfully registered. If @connector hasn't been registered yet,
506 * you'll likely see a panic somewhere deep in sysfs code when called.
507 */
508void drm_sysfs_connector_remove(struct drm_connector *connector)
509{
Dave Airlie5bdebb12013-10-11 14:07:25 +1000510 if (!connector->kdev)
Dave Airlie1828fe62012-02-20 14:15:02 +0000511 return;
Dave Airlief453ba02008-11-07 14:05:41 -0800512 DRM_DEBUG("removing \"%s\" from sysfs\n",
Jani Nikula25933822014-06-03 14:56:20 +0300513 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800514
Dave Airlie5bdebb12013-10-11 14:07:25 +1000515 device_unregister(connector->kdev);
516 connector->kdev = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800517}
Dave Airlief453ba02008-11-07 14:05:41 -0800518
519/**
520 * drm_sysfs_hotplug_event - generate a DRM uevent
521 * @dev: DRM device
522 *
523 * Send a uevent for the DRM device specified by @dev. Currently we only
524 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
525 * deal with other types of events.
526 */
527void drm_sysfs_hotplug_event(struct drm_device *dev)
528{
529 char *event_string = "HOTPLUG=1";
530 char *envp[] = { event_string, NULL };
531
532 DRM_DEBUG("generating hotplug event\n");
533
Dave Airlie5bdebb12013-10-11 14:07:25 +1000534 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
Dave Airlief453ba02008-11-07 14:05:41 -0800535}
Jesse Barnes5ca58282009-03-31 14:11:15 -0700536EXPORT_SYMBOL(drm_sysfs_hotplug_event);
Dave Airlief453ba02008-11-07 14:05:41 -0800537
David Herrmann760c9602013-11-21 20:50:50 +1000538static void drm_sysfs_release(struct device *dev)
539{
540 kfree(dev);
541}
542
Jesse Barnese8b962b2007-11-22 14:02:38 +1000543/**
David Herrmanne1728072014-07-23 11:38:38 +0200544 * drm_sysfs_minor_alloc() - Allocate sysfs device for given minor
545 * @minor: minor to allocate sysfs device for
Jesse Barnese8b962b2007-11-22 14:02:38 +1000546 *
David Herrmanne1728072014-07-23 11:38:38 +0200547 * This allocates a new sysfs device for @minor and returns it. The device is
548 * not registered nor linked. The caller has to use device_add() and
549 * device_del() to register and unregister it.
550 *
551 * Note that dev_get_drvdata() on the new device will return the minor.
552 * However, the device does not hold a ref-count to the minor nor to the
553 * underlying drm_device. This is unproblematic as long as you access the
554 * private data only in sysfs callbacks. device_del() disables those
555 * synchronously, so they cannot be called after you cleanup a minor.
Jesse Barnese8b962b2007-11-22 14:02:38 +1000556 */
David Herrmanne1728072014-07-23 11:38:38 +0200557struct device *drm_sysfs_minor_alloc(struct drm_minor *minor)
Jesse Barnese8b962b2007-11-22 14:02:38 +1000558{
David Herrmanne1728072014-07-23 11:38:38 +0200559 const char *minor_str;
560 struct device *kdev;
David Herrmann760c9602013-11-21 20:50:50 +1000561 int r;
Jesse Barnese8b962b2007-11-22 14:02:38 +1000562
Dave Airlief453ba02008-11-07 14:05:41 -0800563 if (minor->type == DRM_MINOR_CONTROL)
564 minor_str = "controlD%d";
David Herrmanne1728072014-07-23 11:38:38 +0200565 else if (minor->type == DRM_MINOR_RENDER)
566 minor_str = "renderD%d";
567 else
568 minor_str = "card%d";
Jesse Barnese8b962b2007-11-22 14:02:38 +1000569
David Herrmanne1728072014-07-23 11:38:38 +0200570 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
571 if (!kdev)
572 return ERR_PTR(-ENOMEM);
David Herrmann760c9602013-11-21 20:50:50 +1000573
David Herrmanne1728072014-07-23 11:38:38 +0200574 device_initialize(kdev);
575 kdev->devt = MKDEV(DRM_MAJOR, minor->index);
576 kdev->class = drm_class;
577 kdev->type = &drm_sysfs_device_minor;
578 kdev->parent = minor->dev->dev;
579 kdev->release = drm_sysfs_release;
580 dev_set_drvdata(kdev, minor);
David Herrmann760c9602013-11-21 20:50:50 +1000581
David Herrmanne1728072014-07-23 11:38:38 +0200582 r = dev_set_name(kdev, minor_str, minor->index);
David Herrmann760c9602013-11-21 20:50:50 +1000583 if (r < 0)
David Herrmanne1728072014-07-23 11:38:38 +0200584 goto err_free;
David Herrmann760c9602013-11-21 20:50:50 +1000585
David Herrmanne1728072014-07-23 11:38:38 +0200586 return kdev;
David Herrmann760c9602013-11-21 20:50:50 +1000587
David Herrmanne1728072014-07-23 11:38:38 +0200588err_free:
589 put_device(kdev);
590 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593/**
Thomas Hellstrom327c2252009-08-17 16:28:37 +0200594 * drm_class_device_register - Register a struct device in the drm class.
595 *
596 * @dev: pointer to struct device to register.
597 *
598 * @dev should have all relevant members pre-filled with the exception
599 * of the class member. In particular, the device_type member must
600 * be set.
601 */
602
603int drm_class_device_register(struct device *dev)
604{
Dave Airlie49099c42012-07-06 18:06:42 +0100605 if (!drm_class || IS_ERR(drm_class))
606 return -ENOENT;
607
Thomas Hellstrom327c2252009-08-17 16:28:37 +0200608 dev->class = drm_class;
609 return device_register(dev);
610}
611EXPORT_SYMBOL_GPL(drm_class_device_register);
612
613void drm_class_device_unregister(struct device *dev)
614{
615 return device_unregister(dev);
616}
617EXPORT_SYMBOL_GPL(drm_class_device_unregister);