f08873f6489c24a9fbf1ace8280692ff92263a14
[deliverable/linux.git] / drivers / gpu / drm / drm_sysfs.c
1
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
15 #include <linux/device.h>
16 #include <linux/kdev_t.h>
17 #include <linux/gfp.h>
18 #include <linux/err.h>
19 #include <linux/export.h>
20
21 #include <drm/drm_sysfs.h>
22 #include <drm/drm_core.h>
23 #include <drm/drmP.h>
24 #include "drm_internal.h"
25
26 #define to_drm_minor(d) dev_get_drvdata(d)
27 #define to_drm_connector(d) dev_get_drvdata(d)
28
29 static struct device_type drm_sysfs_device_minor = {
30 .name = "drm_minor"
31 };
32
33 struct class *drm_class;
34
35 /**
36 * __drm_class_suspend - internal DRM class suspend routine
37 * @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 */
43 static int __drm_class_suspend(struct device *dev, pm_message_t state)
44 {
45 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;
48
49 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 }
54 return 0;
55 }
56
57 /**
58 * 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 */
62 static 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 */
72 static int drm_class_freeze(struct device *dev)
73 {
74 return __drm_class_suspend(dev, PMSG_FREEZE);
75 }
76
77 /**
78 * drm_class_resume - DRM class resume hook
79 * @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 */
84 static int drm_class_resume(struct device *dev)
85 {
86 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;
89
90 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 }
95 return 0;
96 }
97
98 static 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
104 static char *drm_devnode(struct device *dev, umode_t *mode)
105 {
106 return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
107 }
108
109 static CLASS_ATTR_STRING(version, S_IRUGO,
110 CORE_NAME " "
111 __stringify(CORE_MAJOR) "."
112 __stringify(CORE_MINOR) "."
113 __stringify(CORE_PATCHLEVEL) " "
114 CORE_DATE);
115
116 /**
117 * drm_sysfs_init - initialize sysfs helpers
118 *
119 * This is used to create the DRM class, which is the implicit parent of any
120 * other top-level DRM sysfs objects.
121 *
122 * You must call drm_sysfs_destroy() to release the allocated resources.
123 *
124 * Return: 0 on success, negative error code on failure.
125 */
126 int drm_sysfs_init(void)
127 {
128 int err;
129
130 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;
141 }
142
143 drm_class->devnode = drm_devnode;
144 return 0;
145 }
146
147 /**
148 * drm_sysfs_destroy - destroys DRM class
149 *
150 * Destroy the DRM device class.
151 */
152 void drm_sysfs_destroy(void)
153 {
154 if (IS_ERR_OR_NULL(drm_class))
155 return;
156 class_remove_file(drm_class, &class_attr_version.attr);
157 class_destroy(drm_class);
158 drm_class = NULL;
159 }
160
161 /*
162 * Connector properties
163 */
164 static 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
215 return ret ? ret : count;
216 }
217
218 static 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);
223
224 return snprintf(buf, PAGE_SIZE, "%s\n",
225 drm_get_connector_status_name(connector->status));
226 }
227
228 static 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);
233 struct drm_device *dev = connector->dev;
234 uint64_t dpms_status;
235 int ret;
236
237 ret = drm_object_property_get_value(&connector->base,
238 dev->mode_config.dpms_property,
239 &dpms_status);
240 if (ret)
241 return 0;
242
243 return snprintf(buf, PAGE_SIZE, "%s\n",
244 drm_get_dpms_name((int)dpms_status));
245 }
246
247 static ssize_t enabled_show(struct device *device,
248 struct device_attribute *attr,
249 char *buf)
250 {
251 struct drm_connector *connector = to_drm_connector(device);
252
253 return snprintf(buf, PAGE_SIZE, "%s\n", connector->encoder ? "enabled" :
254 "disabled");
255 }
256
257 static ssize_t edid_show(struct file *filp, struct kobject *kobj,
258 struct bin_attribute *attr, char *buf, loff_t off,
259 size_t count)
260 {
261 struct device *connector_dev = container_of(kobj, struct device, kobj);
262 struct drm_connector *connector = to_drm_connector(connector_dev);
263 unsigned char *edid;
264 size_t size;
265
266 if (!connector->edid_blob_ptr)
267 return 0;
268
269 edid = connector->edid_blob_ptr->data;
270 size = connector->edid_blob_ptr->length;
271 if (!edid)
272 return 0;
273
274 if (off >= size)
275 return 0;
276
277 if (off + count > size)
278 count = size - off;
279 memcpy(buf, edid + off, count);
280
281 return count;
282 }
283
284 static 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
292 list_for_each_entry(mode, &connector->modes, head) {
293 written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
294 mode->name);
295 }
296
297 return written;
298 }
299
300 static ssize_t tv_subconnector_show(struct device *device,
301 struct device_attribute *attr,
302 char *buf)
303 {
304 struct drm_connector *connector = to_drm_connector(device);
305 struct drm_device *dev = connector->dev;
306 struct drm_property *prop;
307 uint64_t subconnector;
308 int ret;
309
310 prop = dev->mode_config.tv_subconnector_property;
311 if (!prop) {
312 DRM_ERROR("Unable to find subconnector property\n");
313 return 0;
314 }
315
316 ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
317 if (ret)
318 return 0;
319
320 return snprintf(buf, PAGE_SIZE, "%s",
321 drm_get_tv_subconnector_name((int)subconnector));
322 }
323
324 static ssize_t tv_select_subconnector_show(struct device *device,
325 struct device_attribute *attr,
326 char *buf)
327 {
328 struct drm_connector *connector = to_drm_connector(device);
329 struct drm_device *dev = connector->dev;
330 struct drm_property *prop;
331 uint64_t subconnector;
332 int ret;
333
334 prop = dev->mode_config.tv_select_subconnector_property;
335 if (!prop) {
336 DRM_ERROR("Unable to find select subconnector property\n");
337 return 0;
338 }
339
340 ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
341 if (ret)
342 return 0;
343
344 return snprintf(buf, PAGE_SIZE, "%s",
345 drm_get_tv_select_name((int)subconnector));
346 }
347
348 static ssize_t dvii_subconnector_show(struct device *device,
349 struct device_attribute *attr,
350 char *buf)
351 {
352 struct drm_connector *connector = to_drm_connector(device);
353 struct drm_device *dev = connector->dev;
354 struct drm_property *prop;
355 uint64_t subconnector;
356 int ret;
357
358 prop = dev->mode_config.dvi_i_subconnector_property;
359 if (!prop) {
360 DRM_ERROR("Unable to find subconnector property\n");
361 return 0;
362 }
363
364 ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
365 if (ret)
366 return 0;
367
368 return snprintf(buf, PAGE_SIZE, "%s",
369 drm_get_dvi_i_subconnector_name((int)subconnector));
370 }
371
372 static ssize_t dvii_select_subconnector_show(struct device *device,
373 struct device_attribute *attr,
374 char *buf)
375 {
376 struct drm_connector *connector = to_drm_connector(device);
377 struct drm_device *dev = connector->dev;
378 struct drm_property *prop;
379 uint64_t subconnector;
380 int ret;
381
382 prop = dev->mode_config.dvi_i_select_subconnector_property;
383 if (!prop) {
384 DRM_ERROR("Unable to find select subconnector property\n");
385 return 0;
386 }
387
388 ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
389 if (ret)
390 return 0;
391
392 return snprintf(buf, PAGE_SIZE, "%s",
393 drm_get_dvi_i_select_name((int)subconnector));
394 }
395
396 static DEVICE_ATTR_RW(status);
397 static DEVICE_ATTR_RO(enabled);
398 static DEVICE_ATTR_RO(dpms);
399 static DEVICE_ATTR_RO(modes);
400
401 static struct attribute *connector_dev_attrs[] = {
402 &dev_attr_status.attr,
403 &dev_attr_enabled.attr,
404 &dev_attr_dpms.attr,
405 &dev_attr_modes.attr,
406 NULL
407 };
408
409 static DEVICE_ATTR_RO(tv_subconnector);
410 static DEVICE_ATTR_RO(tv_select_subconnector);
411
412 static struct attribute *connector_tv_dev_attrs[] = {
413 &dev_attr_tv_subconnector.attr,
414 &dev_attr_tv_select_subconnector.attr,
415 NULL
416 };
417
418 static DEVICE_ATTR_RO(dvii_subconnector);
419 static DEVICE_ATTR_RO(dvii_select_subconnector);
420
421 static struct attribute *connector_dvii_dev_attrs[] = {
422 &dev_attr_dvii_subconnector.attr,
423 &dev_attr_dvii_select_subconnector.attr,
424 NULL
425 };
426
427 /* Connector type related helpers */
428 static int kobj_connector_type(struct kobject *kobj)
429 {
430 struct device *dev = kobj_to_dev(kobj);
431 struct drm_connector *connector = to_drm_connector(dev);
432
433 return connector->connector_type;
434 }
435
436 static umode_t connector_is_dvii(struct kobject *kobj,
437 struct attribute *attr, int idx)
438 {
439 return kobj_connector_type(kobj) == DRM_MODE_CONNECTOR_DVII ?
440 attr->mode : 0;
441 }
442
443 static umode_t connector_is_tv(struct kobject *kobj,
444 struct attribute *attr, int idx)
445 {
446 switch (kobj_connector_type(kobj)) {
447 case DRM_MODE_CONNECTOR_Composite:
448 case DRM_MODE_CONNECTOR_SVIDEO:
449 case DRM_MODE_CONNECTOR_Component:
450 case DRM_MODE_CONNECTOR_TV:
451 return attr->mode;
452 }
453
454 return 0;
455 }
456
457 static struct bin_attribute edid_attr = {
458 .attr.name = "edid",
459 .attr.mode = 0444,
460 .size = 0,
461 .read = edid_show,
462 };
463
464 static struct bin_attribute *connector_bin_attrs[] = {
465 &edid_attr,
466 NULL
467 };
468
469 static const struct attribute_group connector_dev_group = {
470 .attrs = connector_dev_attrs,
471 .bin_attrs = connector_bin_attrs,
472 };
473
474 static const struct attribute_group connector_tv_dev_group = {
475 .attrs = connector_tv_dev_attrs,
476 .is_visible = connector_is_tv,
477 };
478
479 static const struct attribute_group connector_dvii_dev_group = {
480 .attrs = connector_dvii_dev_attrs,
481 .is_visible = connector_is_dvii,
482 };
483
484 static const struct attribute_group *connector_dev_groups[] = {
485 &connector_dev_group,
486 &connector_tv_dev_group,
487 &connector_dvii_dev_group,
488 NULL
489 };
490
491 /**
492 * drm_sysfs_connector_add - add a connector to sysfs
493 * @connector: connector to add
494 *
495 * Create a connector device in sysfs, along with its associated connector
496 * properties (so far, connection status, dpms, mode list & edid) and
497 * generate a hotplug event so userspace knows there's a new connector
498 * available.
499 */
500 int drm_sysfs_connector_add(struct drm_connector *connector)
501 {
502 struct drm_device *dev = connector->dev;
503
504 if (connector->kdev)
505 return 0;
506
507 connector->kdev =
508 device_create_with_groups(drm_class, dev->primary->kdev, 0,
509 connector, connector_dev_groups,
510 "card%d-%s", dev->primary->index,
511 connector->name);
512 DRM_DEBUG("adding \"%s\" to sysfs\n",
513 connector->name);
514
515 if (IS_ERR(connector->kdev)) {
516 DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector->kdev));
517 return PTR_ERR(connector->kdev);
518 }
519
520 /* Let userspace know we have a new connector */
521 drm_sysfs_hotplug_event(dev);
522
523 return 0;
524 }
525
526 /**
527 * drm_sysfs_connector_remove - remove an connector device from sysfs
528 * @connector: connector to remove
529 *
530 * Remove @connector and its associated attributes from sysfs. Note that
531 * the device model core will take care of sending the "remove" uevent
532 * at this time, so we don't need to do it.
533 *
534 * Note:
535 * This routine should only be called if the connector was previously
536 * successfully registered. If @connector hasn't been registered yet,
537 * you'll likely see a panic somewhere deep in sysfs code when called.
538 */
539 void drm_sysfs_connector_remove(struct drm_connector *connector)
540 {
541 if (!connector->kdev)
542 return;
543 DRM_DEBUG("removing \"%s\" from sysfs\n",
544 connector->name);
545
546 device_unregister(connector->kdev);
547 connector->kdev = NULL;
548 }
549
550 /**
551 * drm_sysfs_hotplug_event - generate a DRM uevent
552 * @dev: DRM device
553 *
554 * Send a uevent for the DRM device specified by @dev. Currently we only
555 * set HOTPLUG=1 in the uevent environment, but this could be expanded to
556 * deal with other types of events.
557 */
558 void drm_sysfs_hotplug_event(struct drm_device *dev)
559 {
560 char *event_string = "HOTPLUG=1";
561 char *envp[] = { event_string, NULL };
562
563 DRM_DEBUG("generating hotplug event\n");
564
565 kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
566 }
567 EXPORT_SYMBOL(drm_sysfs_hotplug_event);
568
569 static void drm_sysfs_release(struct device *dev)
570 {
571 kfree(dev);
572 }
573
574 /**
575 * drm_sysfs_minor_alloc() - Allocate sysfs device for given minor
576 * @minor: minor to allocate sysfs device for
577 *
578 * This allocates a new sysfs device for @minor and returns it. The device is
579 * not registered nor linked. The caller has to use device_add() and
580 * device_del() to register and unregister it.
581 *
582 * Note that dev_get_drvdata() on the new device will return the minor.
583 * However, the device does not hold a ref-count to the minor nor to the
584 * underlying drm_device. This is unproblematic as long as you access the
585 * private data only in sysfs callbacks. device_del() disables those
586 * synchronously, so they cannot be called after you cleanup a minor.
587 */
588 struct device *drm_sysfs_minor_alloc(struct drm_minor *minor)
589 {
590 const char *minor_str;
591 struct device *kdev;
592 int r;
593
594 if (minor->type == DRM_MINOR_CONTROL)
595 minor_str = "controlD%d";
596 else if (minor->type == DRM_MINOR_RENDER)
597 minor_str = "renderD%d";
598 else
599 minor_str = "card%d";
600
601 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
602 if (!kdev)
603 return ERR_PTR(-ENOMEM);
604
605 device_initialize(kdev);
606 kdev->devt = MKDEV(DRM_MAJOR, minor->index);
607 kdev->class = drm_class;
608 kdev->type = &drm_sysfs_device_minor;
609 kdev->parent = minor->dev->dev;
610 kdev->release = drm_sysfs_release;
611 dev_set_drvdata(kdev, minor);
612
613 r = dev_set_name(kdev, minor_str, minor->index);
614 if (r < 0)
615 goto err_free;
616
617 return kdev;
618
619 err_free:
620 put_device(kdev);
621 return ERR_PTR(r);
622 }
623
624 /**
625 * drm_class_device_register - Register a struct device in the drm class.
626 *
627 * @dev: pointer to struct device to register.
628 *
629 * @dev should have all relevant members pre-filled with the exception
630 * of the class member. In particular, the device_type member must
631 * be set.
632 */
633
634 int drm_class_device_register(struct device *dev)
635 {
636 if (!drm_class || IS_ERR(drm_class))
637 return -ENOENT;
638
639 dev->class = drm_class;
640 return device_register(dev);
641 }
642 EXPORT_SYMBOL_GPL(drm_class_device_register);
643
644 void drm_class_device_unregister(struct device *dev)
645 {
646 return device_unregister(dev);
647 }
648 EXPORT_SYMBOL_GPL(drm_class_device_unregister);
This page took 0.054317 seconds and 5 git commands to generate.