drm: Move piles of functions from drmP.h to drm_internal.h
[deliverable/linux.git] / drivers / gpu / drm / drm_sysfs.c
CommitLineData
0650fd58 1
1da177e4
LT
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
1da177e4
LT
15#include <linux/device.h>
16#include <linux/kdev_t.h>
5a0e3ad6 17#include <linux/gfp.h>
1da177e4 18#include <linux/err.h>
2d1a8a48 19#include <linux/export.h>
1da177e4 20
760285e7
DH
21#include <drm/drm_sysfs.h>
22#include <drm/drm_core.h>
23#include <drm/drmP.h>
67d0ec4e 24#include "drm_internal.h"
1da177e4 25
5bdebb18
DA
26#define to_drm_minor(d) dev_get_drvdata(d)
27#define to_drm_connector(d) dev_get_drvdata(d)
e8b962b6 28
08e4d534
TH
29static struct device_type drm_sysfs_device_minor = {
30 .name = "drm_minor"
31};
32
e8b962b6 33/**
cf4b91f2 34 * __drm_class_suspend - internal DRM class suspend routine
e8b962b6
JB
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 */
cf4b91f2 41static int __drm_class_suspend(struct device *dev, pm_message_t state)
e8b962b6 42{
08e4d534
TH
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;
46
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 }
e8b962b6
JB
52 return 0;
53}
54
cf4b91f2
SK
55/**
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 */
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
e8b962b6 75/**
08e4d534 76 * drm_class_resume - DRM class resume hook
e8b962b6
JB
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 */
08e4d534 82static int drm_class_resume(struct device *dev)
e8b962b6 83{
08e4d534
TH
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;
87
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 }
e8b962b6
JB
93 return 0;
94}
95
cf4b91f2
SK
96static 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
2c9ede55 102static char *drm_devnode(struct device *dev, umode_t *mode)
02200d06
KS
103{
104 return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
105}
106
0933e2d9
AK
107static CLASS_ATTR_STRING(version, S_IRUGO,
108 CORE_NAME " "
109 __stringify(CORE_MAJOR) "."
110 __stringify(CORE_MINOR) "."
111 __stringify(CORE_PATCHLEVEL) " "
112 CORE_DATE);
1da177e4
LT
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 *
e8b962b6 119 * This is used to create DRM class pointer that can then be used
1da177e4
LT
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 */
0650fd58 125struct class *drm_sysfs_create(struct module *owner, char *name)
1da177e4 126{
0650fd58 127 struct class *class;
24f73c92 128 int err;
0650fd58
GKH
129
130 class = class_create(owner, name);
94f060bd
AM
131 if (IS_ERR(class)) {
132 err = PTR_ERR(class);
24f73c92
JG
133 goto err_out;
134 }
135
cf4b91f2 136 class->pm = &drm_class_dev_pm_ops;
e8b962b6 137
0933e2d9 138 err = class_create_file(class, &class_attr_version.attr);
24f73c92
JG
139 if (err)
140 goto err_out_class;
0650fd58 141
e454cea2 142 class->devnode = drm_devnode;
02200d06 143
0650fd58 144 return class;
24f73c92
JG
145
146err_out_class:
147 class_destroy(class);
148err_out:
149 return ERR_PTR(err);
1da177e4
LT
150}
151
152/**
e8b962b6 153 * drm_sysfs_destroy - destroys DRM class
1da177e4 154 *
e8b962b6 155 * Destroy the DRM device class.
1da177e4 156 */
e8b962b6 157void drm_sysfs_destroy(void)
1da177e4 158{
e8b962b6 159 if ((drm_class == NULL) || (IS_ERR(drm_class)))
1da177e4 160 return;
0933e2d9 161 class_remove_file(drm_class, &class_attr_version.attr);
e8b962b6 162 class_destroy(drm_class);
49099c49 163 drm_class = NULL;
1da177e4
LT
164}
165
f453ba04
DA
166/*
167 * Connector properties
168 */
169static 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;
007c80a5
CW
175 int ret;
176
177 ret = mutex_lock_interruptible(&connector->dev->mode_config.mutex);
178 if (ret)
179 return ret;
f453ba04 180
7b334fcb 181 status = connector->funcs->detect(connector, true);
007c80a5
CW
182 mutex_unlock(&connector->dev->mode_config.mutex);
183
75185c92 184 return snprintf(buf, PAGE_SIZE, "%s\n",
f453ba04
DA
185 drm_get_connector_status_name(status));
186}
187
188static 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
58495563 197 ret = drm_object_property_get_value(&connector->base,
f453ba04
DA
198 dev->mode_config.dpms_property,
199 &dpms_status);
200 if (ret)
201 return 0;
202
75185c92 203 return snprintf(buf, PAGE_SIZE, "%s\n",
f453ba04
DA
204 drm_get_dpms_name((int)dpms_status));
205}
206
207static 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
75185c92 213 return snprintf(buf, PAGE_SIZE, "%s\n", connector->encoder ? "enabled" :
f453ba04
DA
214 "disabled");
215}
216
2c3c8bea
CW
217static ssize_t edid_show(struct file *filp, struct kobject *kobj,
218 struct bin_attribute *attr, char *buf, loff_t off,
219 size_t count)
f453ba04
DA
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
244static 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
260static 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:
74bd3c26 278 case DRM_MODE_CONNECTOR_TV:
f453ba04
DA
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
58495563 292 ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
f453ba04
DA
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
301static 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:
74bd3c26 319 case DRM_MODE_CONNECTOR_TV:
f453ba04
DA
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
58495563 333 ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
f453ba04
DA
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
342static struct device_attribute connector_attrs[] = {
343 __ATTR_RO(status),
344 __ATTR_RO(enabled),
345 __ATTR_RO(dpms),
346 __ATTR_RO(modes),
347};
348
349/* These attributes are for both DVI-I connectors and all types of tv-out. */
350static struct device_attribute connector_attrs_opt1[] = {
351 __ATTR_RO(subconnector),
352 __ATTR_RO(select_subconnector),
353};
354
355static struct bin_attribute edid_attr = {
356 .attr.name = "edid",
e36ebaf4 357 .attr.mode = 0444,
7466f4cc 358 .size = 0,
f453ba04
DA
359 .read = edid_show,
360};
361
362/**
3b02ab88 363 * drm_sysfs_connector_add - add a connector to sysfs
f453ba04
DA
364 * @connector: connector to add
365 *
3b02ab88 366 * Create a connector device in sysfs, along with its associated connector
f453ba04
DA
367 * properties (so far, connection status, dpms, mode list & edid) and
368 * generate a hotplug event so userspace knows there's a new connector
369 * available.
f453ba04
DA
370 */
371int drm_sysfs_connector_add(struct drm_connector *connector)
372{
373 struct drm_device *dev = connector->dev;
a1c4560d
DC
374 int attr_cnt = 0;
375 int opt_cnt = 0;
376 int i;
4a1b0714 377 int ret;
f453ba04 378
5bdebb18
DA
379 if (connector->kdev)
380 return 0;
f453ba04 381
5bdebb18
DA
382 connector->kdev = device_create(drm_class, dev->primary->kdev,
383 0, connector, "card%d-%s",
25933820 384 dev->primary->index, connector->name);
f453ba04 385 DRM_DEBUG("adding \"%s\" to sysfs\n",
25933820 386 connector->name);
f453ba04 387
5bdebb18
DA
388 if (IS_ERR(connector->kdev)) {
389 DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector->kdev));
390 ret = PTR_ERR(connector->kdev);
f453ba04
DA
391 goto out;
392 }
393
394 /* Standard attributes */
395
a1c4560d 396 for (attr_cnt = 0; attr_cnt < ARRAY_SIZE(connector_attrs); attr_cnt++) {
5bdebb18 397 ret = device_create_file(connector->kdev, &connector_attrs[attr_cnt]);
f453ba04
DA
398 if (ret)
399 goto err_out_files;
400 }
401
402 /* Optional attributes */
403 /*
404 * In the long run it maybe a good idea to make one set of
405 * optionals per connector type.
406 */
407 switch (connector->connector_type) {
408 case DRM_MODE_CONNECTOR_DVII:
409 case DRM_MODE_CONNECTOR_Composite:
410 case DRM_MODE_CONNECTOR_SVIDEO:
411 case DRM_MODE_CONNECTOR_Component:
74bd3c26 412 case DRM_MODE_CONNECTOR_TV:
a1c4560d 413 for (opt_cnt = 0; opt_cnt < ARRAY_SIZE(connector_attrs_opt1); opt_cnt++) {
5bdebb18 414 ret = device_create_file(connector->kdev, &connector_attrs_opt1[opt_cnt]);
f453ba04
DA
415 if (ret)
416 goto err_out_files;
417 }
418 break;
419 default:
420 break;
421 }
422
5bdebb18 423 ret = sysfs_create_bin_file(&connector->kdev->kobj, &edid_attr);
f453ba04
DA
424 if (ret)
425 goto err_out_files;
426
427 /* Let userspace know we have a new connector */
428 drm_sysfs_hotplug_event(dev);
429
430 return 0;
431
432err_out_files:
a1c4560d 433 for (i = 0; i < opt_cnt; i++)
5bdebb18 434 device_remove_file(connector->kdev, &connector_attrs_opt1[i]);
a1c4560d 435 for (i = 0; i < attr_cnt; i++)
5bdebb18 436 device_remove_file(connector->kdev, &connector_attrs[i]);
5bdebb18 437 device_unregister(connector->kdev);
f453ba04
DA
438
439out:
440 return ret;
441}
f453ba04
DA
442
443/**
444 * drm_sysfs_connector_remove - remove an connector device from sysfs
445 * @connector: connector to remove
446 *
447 * Remove @connector and its associated attributes from sysfs. Note that
448 * the device model core will take care of sending the "remove" uevent
449 * at this time, so we don't need to do it.
450 *
451 * Note:
452 * This routine should only be called if the connector was previously
453 * successfully registered. If @connector hasn't been registered yet,
454 * you'll likely see a panic somewhere deep in sysfs code when called.
455 */
456void drm_sysfs_connector_remove(struct drm_connector *connector)
457{
458 int i;
459
5bdebb18 460 if (!connector->kdev)
1828fe6c 461 return;
f453ba04 462 DRM_DEBUG("removing \"%s\" from sysfs\n",
25933820 463 connector->name);
f453ba04
DA
464
465 for (i = 0; i < ARRAY_SIZE(connector_attrs); i++)
5bdebb18
DA
466 device_remove_file(connector->kdev, &connector_attrs[i]);
467 sysfs_remove_bin_file(&connector->kdev->kobj, &edid_attr);
5bdebb18
DA
468 device_unregister(connector->kdev);
469 connector->kdev = NULL;
f453ba04 470}
f453ba04
DA
471
472/**
473 * drm_sysfs_hotplug_event - generate a DRM uevent
474 * @dev: DRM device
475 *
476 * Send a uevent for the DRM device specified by @dev. Currently we only
477 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
478 * deal with other types of events.
479 */
480void drm_sysfs_hotplug_event(struct drm_device *dev)
481{
482 char *event_string = "HOTPLUG=1";
483 char *envp[] = { event_string, NULL };
484
485 DRM_DEBUG("generating hotplug event\n");
486
5bdebb18 487 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
f453ba04 488}
5ca58282 489EXPORT_SYMBOL(drm_sysfs_hotplug_event);
f453ba04 490
760c960b
DH
491static void drm_sysfs_release(struct device *dev)
492{
493 kfree(dev);
494}
495
1da177e4 496/**
e1728075
DH
497 * drm_sysfs_minor_alloc() - Allocate sysfs device for given minor
498 * @minor: minor to allocate sysfs device for
1da177e4 499 *
e1728075
DH
500 * This allocates a new sysfs device for @minor and returns it. The device is
501 * not registered nor linked. The caller has to use device_add() and
502 * device_del() to register and unregister it.
503 *
504 * Note that dev_get_drvdata() on the new device will return the minor.
505 * However, the device does not hold a ref-count to the minor nor to the
506 * underlying drm_device. This is unproblematic as long as you access the
507 * private data only in sysfs callbacks. device_del() disables those
508 * synchronously, so they cannot be called after you cleanup a minor.
1da177e4 509 */
e1728075 510struct device *drm_sysfs_minor_alloc(struct drm_minor *minor)
1da177e4 511{
e1728075
DH
512 const char *minor_str;
513 struct device *kdev;
760c960b 514 int r;
e8b962b6 515
f453ba04
DA
516 if (minor->type == DRM_MINOR_CONTROL)
517 minor_str = "controlD%d";
e1728075
DH
518 else if (minor->type == DRM_MINOR_RENDER)
519 minor_str = "renderD%d";
520 else
521 minor_str = "card%d";
522
523 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
524 if (!kdev)
525 return ERR_PTR(-ENOMEM);
526
527 device_initialize(kdev);
528 kdev->devt = MKDEV(DRM_MAJOR, minor->index);
529 kdev->class = drm_class;
530 kdev->type = &drm_sysfs_device_minor;
531 kdev->parent = minor->dev->dev;
532 kdev->release = drm_sysfs_release;
533 dev_set_drvdata(kdev, minor);
534
535 r = dev_set_name(kdev, minor_str, minor->index);
760c960b 536 if (r < 0)
e1728075 537 goto err_free;
760c960b 538
e1728075 539 return kdev;
1da177e4 540
e1728075
DH
541err_free:
542 put_device(kdev);
543 return ERR_PTR(r);
1da177e4 544}
327c225b 545
327c225b
TH
546/**
547 * drm_class_device_register - Register a struct device in the drm class.
548 *
549 * @dev: pointer to struct device to register.
550 *
551 * @dev should have all relevant members pre-filled with the exception
552 * of the class member. In particular, the device_type member must
553 * be set.
554 */
555
556int drm_class_device_register(struct device *dev)
557{
49099c49
DA
558 if (!drm_class || IS_ERR(drm_class))
559 return -ENOENT;
560
327c225b
TH
561 dev->class = drm_class;
562 return device_register(dev);
563}
564EXPORT_SYMBOL_GPL(drm_class_device_register);
565
566void drm_class_device_unregister(struct device *dev)
567{
568 return device_unregister(dev);
569}
570EXPORT_SYMBOL_GPL(drm_class_device_unregister);
This page took 0.714933 seconds and 5 git commands to generate.