OSDN Git Service

drm/sysfs: add a helper for extracting connector type from kobject
[uclinux-h8/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 /**
34  * __drm_class_suspend - internal DRM class suspend routine
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  */
41 static int __drm_class_suspend(struct device *dev, pm_message_t state)
42 {
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         }
52         return 0;
53 }
54
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  */
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 /**
76  * drm_class_resume - DRM class resume hook
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  */
82 static int drm_class_resume(struct device *dev)
83 {
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         }
93         return 0;
94 }
95
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
102 static char *drm_devnode(struct device *dev, umode_t *mode)
103 {
104         return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
105 }
106
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);
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  *
119  * This is used to create DRM class pointer that can then be used
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  */
125 struct class *drm_sysfs_create(struct module *owner, char *name)
126 {
127         struct class *class;
128         int err;
129
130         class = class_create(owner, name);
131         if (IS_ERR(class)) {
132                 err = PTR_ERR(class);
133                 goto err_out;
134         }
135
136         class->pm = &drm_class_dev_pm_ops;
137
138         err = class_create_file(class, &class_attr_version.attr);
139         if (err)
140                 goto err_out_class;
141
142         class->devnode = drm_devnode;
143
144         return class;
145
146 err_out_class:
147         class_destroy(class);
148 err_out:
149         return ERR_PTR(err);
150 }
151
152 /**
153  * drm_sysfs_destroy - destroys DRM class
154  *
155  * Destroy the DRM device class.
156  */
157 void drm_sysfs_destroy(void)
158 {
159         if ((drm_class == NULL) || (IS_ERR(drm_class)))
160                 return;
161         class_remove_file(drm_class, &class_attr_version.attr);
162         class_destroy(drm_class);
163         drm_class = NULL;
164 }
165
166 /*
167  * Connector properties
168  */
169 static 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
223 static 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);
228
229         return snprintf(buf, PAGE_SIZE, "%s\n",
230                         drm_get_connector_status_name(connector->status));
231 }
232
233 static 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
242         ret = drm_object_property_get_value(&connector->base,
243                                             dev->mode_config.dpms_property,
244                                             &dpms_status);
245         if (ret)
246                 return 0;
247
248         return snprintf(buf, PAGE_SIZE, "%s\n",
249                         drm_get_dpms_name((int)dpms_status));
250 }
251
252 static 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
258         return snprintf(buf, PAGE_SIZE, "%s\n", connector->encoder ? "enabled" :
259                         "disabled");
260 }
261
262 static ssize_t edid_show(struct file *filp, struct kobject *kobj,
263                          struct bin_attribute *attr, char *buf, loff_t off,
264                          size_t count)
265 {
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
289 static 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
305 static 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:
323                 case DRM_MODE_CONNECTOR_TV:
324                         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
337         ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
338         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
346 static 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:
364                 case DRM_MODE_CONNECTOR_TV:
365                         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
378         ret = drm_object_property_get_value(&connector->base, prop, &subconnector);
379         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
387 static DEVICE_ATTR_RW(status);
388 static DEVICE_ATTR_RO(enabled);
389 static DEVICE_ATTR_RO(dpms);
390 static DEVICE_ATTR_RO(modes);
391
392 static 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
398 };
399
400 /* These attributes are for both DVI-I connectors and all types of tv-out. */
401 static DEVICE_ATTR_RO(subconnector);
402 static DEVICE_ATTR_RO(select_subconnector);
403
404 static struct attribute *connector_opt_dev_attrs[] = {
405         &dev_attr_subconnector.attr,
406         &dev_attr_select_subconnector.attr,
407         NULL
408 };
409
410 /* Connector type related helpers */
411 static int kobj_connector_type(struct kobject *kobj)
412 {
413         struct device *dev = kobj_to_dev(kobj);
414         struct drm_connector *connector = to_drm_connector(dev);
415
416         return connector->connector_type;
417 }
418
419 static umode_t connector_opt_dev_is_visible(struct kobject *kobj,
420                                             struct attribute *attr, int idx)
421 {
422         /*
423          * In the long run it maybe a good idea to make one set of
424          * optionals per connector type.
425          */
426         switch (kobj_connector_type(kobj)) {
427         case DRM_MODE_CONNECTOR_DVII:
428         case DRM_MODE_CONNECTOR_Composite:
429         case DRM_MODE_CONNECTOR_SVIDEO:
430         case DRM_MODE_CONNECTOR_Component:
431         case DRM_MODE_CONNECTOR_TV:
432                 return attr->mode;
433         }
434
435         return 0;
436 }
437
438 static struct bin_attribute edid_attr = {
439         .attr.name = "edid",
440         .attr.mode = 0444,
441         .size = 0,
442         .read = edid_show,
443 };
444
445 static struct bin_attribute *connector_bin_attrs[] = {
446         &edid_attr,
447         NULL
448 };
449
450 static const struct attribute_group connector_dev_group = {
451         .attrs = connector_dev_attrs,
452         .bin_attrs = connector_bin_attrs,
453 };
454
455 static const struct attribute_group connector_opt_dev_group = {
456         .attrs = connector_opt_dev_attrs,
457         .is_visible = connector_opt_dev_is_visible,
458 };
459
460 static const struct attribute_group *connector_dev_groups[] = {
461         &connector_dev_group,
462         &connector_opt_dev_group,
463         NULL
464 };
465
466 /**
467  * drm_sysfs_connector_add - add a connector to sysfs
468  * @connector: connector to add
469  *
470  * Create a connector device in sysfs, along with its associated connector
471  * properties (so far, connection status, dpms, mode list & edid) and
472  * generate a hotplug event so userspace knows there's a new connector
473  * available.
474  */
475 int drm_sysfs_connector_add(struct drm_connector *connector)
476 {
477         struct drm_device *dev = connector->dev;
478
479         if (connector->kdev)
480                 return 0;
481
482         connector->kdev =
483                 device_create_with_groups(drm_class, dev->primary->kdev, 0,
484                                           connector, connector_dev_groups,
485                                           "card%d-%s", dev->primary->index,
486                                           connector->name);
487         DRM_DEBUG("adding \"%s\" to sysfs\n",
488                   connector->name);
489
490         if (IS_ERR(connector->kdev)) {
491                 DRM_ERROR("failed to register connector device: %ld\n", PTR_ERR(connector->kdev));
492                 return PTR_ERR(connector->kdev);
493         }
494
495         /* Let userspace know we have a new connector */
496         drm_sysfs_hotplug_event(dev);
497
498         return 0;
499 }
500
501 /**
502  * drm_sysfs_connector_remove - remove an connector device from sysfs
503  * @connector: connector to remove
504  *
505  * Remove @connector and its associated attributes from sysfs.  Note that
506  * the device model core will take care of sending the "remove" uevent
507  * at this time, so we don't need to do it.
508  *
509  * Note:
510  * This routine should only be called if the connector was previously
511  * successfully registered.  If @connector hasn't been registered yet,
512  * you'll likely see a panic somewhere deep in sysfs code when called.
513  */
514 void drm_sysfs_connector_remove(struct drm_connector *connector)
515 {
516         if (!connector->kdev)
517                 return;
518         DRM_DEBUG("removing \"%s\" from sysfs\n",
519                   connector->name);
520
521         device_unregister(connector->kdev);
522         connector->kdev = NULL;
523 }
524
525 /**
526  * drm_sysfs_hotplug_event - generate a DRM uevent
527  * @dev: DRM device
528  *
529  * Send a uevent for the DRM device specified by @dev.  Currently we only
530  * set HOTPLUG=1 in the uevent environment, but this could be expanded to
531  * deal with other types of events.
532  */
533 void drm_sysfs_hotplug_event(struct drm_device *dev)
534 {
535         char *event_string = "HOTPLUG=1";
536         char *envp[] = { event_string, NULL };
537
538         DRM_DEBUG("generating hotplug event\n");
539
540         kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
541 }
542 EXPORT_SYMBOL(drm_sysfs_hotplug_event);
543
544 static void drm_sysfs_release(struct device *dev)
545 {
546         kfree(dev);
547 }
548
549 /**
550  * drm_sysfs_minor_alloc() - Allocate sysfs device for given minor
551  * @minor: minor to allocate sysfs device for
552  *
553  * This allocates a new sysfs device for @minor and returns it. The device is
554  * not registered nor linked. The caller has to use device_add() and
555  * device_del() to register and unregister it.
556  *
557  * Note that dev_get_drvdata() on the new device will return the minor.
558  * However, the device does not hold a ref-count to the minor nor to the
559  * underlying drm_device. This is unproblematic as long as you access the
560  * private data only in sysfs callbacks. device_del() disables those
561  * synchronously, so they cannot be called after you cleanup a minor.
562  */
563 struct device *drm_sysfs_minor_alloc(struct drm_minor *minor)
564 {
565         const char *minor_str;
566         struct device *kdev;
567         int r;
568
569         if (minor->type == DRM_MINOR_CONTROL)
570                 minor_str = "controlD%d";
571         else if (minor->type == DRM_MINOR_RENDER)
572                 minor_str = "renderD%d";
573         else
574                 minor_str = "card%d";
575
576         kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
577         if (!kdev)
578                 return ERR_PTR(-ENOMEM);
579
580         device_initialize(kdev);
581         kdev->devt = MKDEV(DRM_MAJOR, minor->index);
582         kdev->class = drm_class;
583         kdev->type = &drm_sysfs_device_minor;
584         kdev->parent = minor->dev->dev;
585         kdev->release = drm_sysfs_release;
586         dev_set_drvdata(kdev, minor);
587
588         r = dev_set_name(kdev, minor_str, minor->index);
589         if (r < 0)
590                 goto err_free;
591
592         return kdev;
593
594 err_free:
595         put_device(kdev);
596         return ERR_PTR(r);
597 }
598
599 /**
600  * drm_class_device_register - Register a struct device in the drm class.
601  *
602  * @dev: pointer to struct device to register.
603  *
604  * @dev should have all relevant members pre-filled with the exception
605  * of the class member. In particular, the device_type member must
606  * be set.
607  */
608
609 int drm_class_device_register(struct device *dev)
610 {
611         if (!drm_class || IS_ERR(drm_class))
612                 return -ENOENT;
613
614         dev->class = drm_class;
615         return device_register(dev);
616 }
617 EXPORT_SYMBOL_GPL(drm_class_device_register);
618
619 void drm_class_device_unregister(struct device *dev)
620 {
621         return device_unregister(dev);
622 }
623 EXPORT_SYMBOL_GPL(drm_class_device_unregister);