]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/gpu/drm/drm_sysfs.c
b95aaf23596e1a21ebe3b6d6ea2df30c01da1b67
[linux-beck.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/err.h>
18
19 #include "drm_sysfs.h"
20 #include "drm_core.h"
21 #include "drmP.h"
22
23 #define to_drm_minor(d) container_of(d, struct drm_minor, kdev)
24 #define to_drm_connector(d) container_of(d, struct drm_connector, kdev)
25
26 static struct device_type drm_sysfs_device_minor = {
27         .name = "drm_minor"
28 };
29
30 /**
31  * drm_class_suspend - DRM class suspend hook
32  * @dev: Linux device to suspend
33  * @state: power state to enter
34  *
35  * Just figures out what the actual struct drm_device associated with
36  * @dev is and calls its suspend hook, if present.
37  */
38 static int drm_class_suspend(struct device *dev, pm_message_t state)
39 {
40         if (dev->type == &drm_sysfs_device_minor) {
41                 struct drm_minor *drm_minor = to_drm_minor(dev);
42                 struct drm_device *drm_dev = drm_minor->dev;
43
44                 if (drm_minor->type == DRM_MINOR_LEGACY &&
45                     !drm_core_check_feature(drm_dev, DRIVER_MODESET) &&
46                     drm_dev->driver->suspend)
47                         return drm_dev->driver->suspend(drm_dev, state);
48         }
49         return 0;
50 }
51
52 /**
53  * drm_class_resume - DRM class resume hook
54  * @dev: Linux device to resume
55  *
56  * Just figures out what the actual struct drm_device associated with
57  * @dev is and calls its resume hook, if present.
58  */
59 static int drm_class_resume(struct device *dev)
60 {
61         if (dev->type == &drm_sysfs_device_minor) {
62                 struct drm_minor *drm_minor = to_drm_minor(dev);
63                 struct drm_device *drm_dev = drm_minor->dev;
64
65                 if (drm_minor->type == DRM_MINOR_LEGACY &&
66                     !drm_core_check_feature(drm_dev, DRIVER_MODESET) &&
67                     drm_dev->driver->resume)
68                         return drm_dev->driver->resume(drm_dev);
69         }
70         return 0;
71 }
72
73 /* Display the version of drm_core. This doesn't work right in current design */
74 static ssize_t version_show(struct class *dev, struct class_attribute *attr,
75                                 char *buf)
76 {
77         return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR,
78                        CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
79 }
80
81 static char *drm_devnode(struct device *dev, mode_t *mode)
82 {
83         return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
84 }
85
86 static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
87
88 /**
89  * drm_sysfs_create - create a struct drm_sysfs_class structure
90  * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
91  * @name: pointer to a string for the name of this class.
92  *
93  * This is used to create DRM class pointer that can then be used
94  * in calls to drm_sysfs_device_add().
95  *
96  * Note, the pointer created here is to be destroyed when finished by making a
97  * call to drm_sysfs_destroy().
98  */
99 struct class *drm_sysfs_create(struct module *owner, char *name)
100 {
101         struct class *class;
102         int err;
103
104         class = class_create(owner, name);
105         if (IS_ERR(class)) {
106                 err = PTR_ERR(class);
107                 goto err_out;
108         }
109
110         class->suspend = drm_class_suspend;
111         class->resume = drm_class_resume;
112
113         err = class_create_file(class, &class_attr_version);
114         if (err)
115                 goto err_out_class;
116
117         class->devnode = drm_devnode;
118
119         return class;
120
121 err_out_class:
122         class_destroy(class);
123 err_out:
124         return ERR_PTR(err);
125 }
126
127 /**
128  * drm_sysfs_destroy - destroys DRM class
129  *
130  * Destroy the DRM device class.
131  */
132 void drm_sysfs_destroy(void)
133 {
134         if ((drm_class == NULL) || (IS_ERR(drm_class)))
135                 return;
136         class_remove_file(drm_class, &class_attr_version);
137         class_destroy(drm_class);
138 }
139
140 /**
141  * drm_sysfs_device_release - do nothing
142  * @dev: Linux device
143  *
144  * Normally, this would free the DRM device associated with @dev, along
145  * with cleaning up any other stuff.  But we do that in the DRM core, so
146  * this function can just return and hope that the core does its job.
147  */
148 static void drm_sysfs_device_release(struct device *dev)
149 {
150         memset(dev, 0, sizeof(struct device));
151         return;
152 }
153
154 /*
155  * Connector properties
156  */
157 static ssize_t status_show(struct device *device,
158                            struct device_attribute *attr,
159                            char *buf)
160 {
161         struct drm_connector *connector = to_drm_connector(device);
162         enum drm_connector_status status;
163
164         status = connector->funcs->detect(connector);
165         return snprintf(buf, PAGE_SIZE, "%s\n",
166                         drm_get_connector_status_name(status));
167 }
168
169 static ssize_t dpms_show(struct device *device,
170                            struct device_attribute *attr,
171                            char *buf)
172 {
173         struct drm_connector *connector = to_drm_connector(device);
174         struct drm_device *dev = connector->dev;
175         uint64_t dpms_status;
176         int ret;
177
178         ret = drm_connector_property_get_value(connector,
179                                             dev->mode_config.dpms_property,
180                                             &dpms_status);
181         if (ret)
182                 return 0;
183
184         return snprintf(buf, PAGE_SIZE, "%s\n",
185                         drm_get_dpms_name((int)dpms_status));
186 }
187
188 static ssize_t enabled_show(struct device *device,
189                             struct device_attribute *attr,
190                            char *buf)
191 {
192         struct drm_connector *connector = to_drm_connector(device);
193
194         return snprintf(buf, PAGE_SIZE, "%s\n", connector->encoder ? "enabled" :
195                         "disabled");
196 }
197
198 static ssize_t edid_show(struct kobject *kobj, struct bin_attribute *attr,
199                          char *buf, loff_t off, size_t count)
200 {
201         struct device *connector_dev = container_of(kobj, struct device, kobj);
202         struct drm_connector *connector = to_drm_connector(connector_dev);
203         unsigned char *edid;
204         size_t size;
205
206         if (!connector->edid_blob_ptr)
207                 return 0;
208
209         edid = connector->edid_blob_ptr->data;
210         size = connector->edid_blob_ptr->length;
211         if (!edid)
212                 return 0;
213
214         if (off >= size)
215                 return 0;
216
217         if (off + count > size)
218                 count = size - off;
219         memcpy(buf, edid + off, count);
220
221         return count;
222 }
223
224 static ssize_t modes_show(struct device *device,
225                            struct device_attribute *attr,
226                            char *buf)
227 {
228         struct drm_connector *connector = to_drm_connector(device);
229         struct drm_display_mode *mode;
230         int written = 0;
231
232         list_for_each_entry(mode, &connector->modes, head) {
233                 written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
234                                     mode->name);
235         }
236
237         return written;
238 }
239
240 static ssize_t subconnector_show(struct device *device,
241                            struct device_attribute *attr,
242                            char *buf)
243 {
244         struct drm_connector *connector = to_drm_connector(device);
245         struct drm_device *dev = connector->dev;
246         struct drm_property *prop = NULL;
247         uint64_t subconnector;
248         int is_tv = 0;
249         int ret;
250
251         switch (connector->connector_type) {
252                 case DRM_MODE_CONNECTOR_DVII:
253                         prop = dev->mode_config.dvi_i_subconnector_property;
254                         break;
255                 case DRM_MODE_CONNECTOR_Composite:
256                 case DRM_MODE_CONNECTOR_SVIDEO:
257                 case DRM_MODE_CONNECTOR_Component:
258                 case DRM_MODE_CONNECTOR_TV:
259                         prop = dev->mode_config.tv_subconnector_property;
260                         is_tv = 1;
261                         break;
262                 default:
263                         DRM_ERROR("Wrong connector type for this property\n");
264                         return 0;
265         }
266
267         if (!prop) {
268                 DRM_ERROR("Unable to find subconnector property\n");
269                 return 0;
270         }
271
272         ret = drm_connector_property_get_value(connector, prop, &subconnector);
273         if (ret)
274                 return 0;
275
276         return snprintf(buf, PAGE_SIZE, "%s", is_tv ?
277                         drm_get_tv_subconnector_name((int)subconnector) :
278                         drm_get_dvi_i_subconnector_name((int)subconnector));
279 }
280
281 static ssize_t select_subconnector_show(struct device *device,
282                            struct device_attribute *attr,
283                            char *buf)
284 {
285         struct drm_connector *connector = to_drm_connector(device);
286         struct drm_device *dev = connector->dev;
287         struct drm_property *prop = NULL;
288         uint64_t subconnector;
289         int is_tv = 0;
290         int ret;
291
292         switch (connector->connector_type) {
293                 case DRM_MODE_CONNECTOR_DVII:
294                         prop = dev->mode_config.dvi_i_select_subconnector_property;
295                         break;
296                 case DRM_MODE_CONNECTOR_Composite:
297                 case DRM_MODE_CONNECTOR_SVIDEO:
298                 case DRM_MODE_CONNECTOR_Component:
299                 case DRM_MODE_CONNECTOR_TV:
300                         prop = dev->mode_config.tv_select_subconnector_property;
301                         is_tv = 1;
302                         break;
303                 default:
304                         DRM_ERROR("Wrong connector type for this property\n");
305                         return 0;
306         }
307
308         if (!prop) {
309                 DRM_ERROR("Unable to find select subconnector property\n");
310                 return 0;
311         }
312
313         ret = drm_connector_property_get_value(connector, prop, &subconnector);
314         if (ret)
315                 return 0;
316
317         return snprintf(buf, PAGE_SIZE, "%s", is_tv ?
318                         drm_get_tv_select_name((int)subconnector) :
319                         drm_get_dvi_i_select_name((int)subconnector));
320 }
321
322 static struct device_attribute connector_attrs[] = {
323         __ATTR_RO(status),
324         __ATTR_RO(enabled),
325         __ATTR_RO(dpms),
326         __ATTR_RO(modes),
327 };
328
329 /* These attributes are for both DVI-I connectors and all types of tv-out. */
330 static struct device_attribute connector_attrs_opt1[] = {
331         __ATTR_RO(subconnector),
332         __ATTR_RO(select_subconnector),
333 };
334
335 static struct bin_attribute edid_attr = {
336         .attr.name = "edid",
337         .attr.mode = 0444,
338         .size = 128,
339         .read = edid_show,
340 };
341
342 /**
343  * drm_sysfs_connector_add - add an connector to sysfs
344  * @connector: connector to add
345  *
346  * Create an connector device in sysfs, along with its associated connector
347  * properties (so far, connection status, dpms, mode list & edid) and
348  * generate a hotplug event so userspace knows there's a new connector
349  * available.
350  *
351  * Note:
352  * This routine should only be called *once* for each DRM minor registered.
353  * A second call for an already registered device will trigger the BUG_ON
354  * below.
355  */
356 int drm_sysfs_connector_add(struct drm_connector *connector)
357 {
358         struct drm_device *dev = connector->dev;
359         int ret = 0, i, j;
360
361         /* We shouldn't get called more than once for the same connector */
362         BUG_ON(device_is_registered(&connector->kdev));
363
364         connector->kdev.parent = &dev->primary->kdev;
365         connector->kdev.class = drm_class;
366         connector->kdev.release = drm_sysfs_device_release;
367
368         DRM_DEBUG("adding \"%s\" to sysfs\n",
369                   drm_get_connector_name(connector));
370
371         dev_set_name(&connector->kdev, "card%d-%s",
372                      dev->primary->index, drm_get_connector_name(connector));
373         ret = device_register(&connector->kdev);
374
375         if (ret) {
376                 DRM_ERROR("failed to register connector device: %d\n", ret);
377                 goto out;
378         }
379
380         /* Standard attributes */
381
382         for (i = 0; i < ARRAY_SIZE(connector_attrs); i++) {
383                 ret = device_create_file(&connector->kdev, &connector_attrs[i]);
384                 if (ret)
385                         goto err_out_files;
386         }
387
388         /* Optional attributes */
389         /*
390          * In the long run it maybe a good idea to make one set of
391          * optionals per connector type.
392          */
393         switch (connector->connector_type) {
394                 case DRM_MODE_CONNECTOR_DVII:
395                 case DRM_MODE_CONNECTOR_Composite:
396                 case DRM_MODE_CONNECTOR_SVIDEO:
397                 case DRM_MODE_CONNECTOR_Component:
398                 case DRM_MODE_CONNECTOR_TV:
399                         for (i = 0; i < ARRAY_SIZE(connector_attrs_opt1); i++) {
400                                 ret = device_create_file(&connector->kdev, &connector_attrs_opt1[i]);
401                                 if (ret)
402                                         goto err_out_files;
403                         }
404                         break;
405                 default:
406                         break;
407         }
408
409         ret = sysfs_create_bin_file(&connector->kdev.kobj, &edid_attr);
410         if (ret)
411                 goto err_out_files;
412
413         /* Let userspace know we have a new connector */
414         drm_sysfs_hotplug_event(dev);
415
416         return 0;
417
418 err_out_files:
419         if (i > 0)
420                 for (j = 0; j < i; j++)
421                         device_remove_file(&connector->kdev,
422                                            &connector_attrs[i]);
423         device_unregister(&connector->kdev);
424
425 out:
426         return ret;
427 }
428 EXPORT_SYMBOL(drm_sysfs_connector_add);
429
430 /**
431  * drm_sysfs_connector_remove - remove an connector device from sysfs
432  * @connector: connector to remove
433  *
434  * Remove @connector and its associated attributes from sysfs.  Note that
435  * the device model core will take care of sending the "remove" uevent
436  * at this time, so we don't need to do it.
437  *
438  * Note:
439  * This routine should only be called if the connector was previously
440  * successfully registered.  If @connector hasn't been registered yet,
441  * you'll likely see a panic somewhere deep in sysfs code when called.
442  */
443 void drm_sysfs_connector_remove(struct drm_connector *connector)
444 {
445         int i;
446
447         DRM_DEBUG("removing \"%s\" from sysfs\n",
448                   drm_get_connector_name(connector));
449
450         for (i = 0; i < ARRAY_SIZE(connector_attrs); i++)
451                 device_remove_file(&connector->kdev, &connector_attrs[i]);
452         sysfs_remove_bin_file(&connector->kdev.kobj, &edid_attr);
453         device_unregister(&connector->kdev);
454 }
455 EXPORT_SYMBOL(drm_sysfs_connector_remove);
456
457 /**
458  * drm_sysfs_hotplug_event - generate a DRM uevent
459  * @dev: DRM device
460  *
461  * Send a uevent for the DRM device specified by @dev.  Currently we only
462  * set HOTPLUG=1 in the uevent environment, but this could be expanded to
463  * deal with other types of events.
464  */
465 void drm_sysfs_hotplug_event(struct drm_device *dev)
466 {
467         char *event_string = "HOTPLUG=1";
468         char *envp[] = { event_string, NULL };
469
470         DRM_DEBUG("generating hotplug event\n");
471
472         kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, envp);
473 }
474 EXPORT_SYMBOL(drm_sysfs_hotplug_event);
475
476 /**
477  * drm_sysfs_device_add - adds a class device to sysfs for a character driver
478  * @dev: DRM device to be added
479  * @head: DRM head in question
480  *
481  * Add a DRM device to the DRM's device model class.  We use @dev's PCI device
482  * as the parent for the Linux device, and make sure it has a file containing
483  * the driver we're using (for userspace compatibility).
484  */
485 int drm_sysfs_device_add(struct drm_minor *minor)
486 {
487         int err;
488         char *minor_str;
489
490         minor->kdev.parent = &minor->dev->pdev->dev;
491         minor->kdev.class = drm_class;
492         minor->kdev.release = drm_sysfs_device_release;
493         minor->kdev.devt = minor->device;
494         minor->kdev.type = &drm_sysfs_device_minor;
495         if (minor->type == DRM_MINOR_CONTROL)
496                 minor_str = "controlD%d";
497         else if (minor->type == DRM_MINOR_RENDER)
498                 minor_str = "renderD%d";
499         else
500                 minor_str = "card%d";
501
502         dev_set_name(&minor->kdev, minor_str, minor->index);
503
504         err = device_register(&minor->kdev);
505         if (err) {
506                 DRM_ERROR("device add failed: %d\n", err);
507                 goto err_out;
508         }
509
510         return 0;
511
512 err_out:
513         return err;
514 }
515
516 /**
517  * drm_sysfs_device_remove - remove DRM device
518  * @dev: DRM device to remove
519  *
520  * This call unregisters and cleans up a class device that was created with a
521  * call to drm_sysfs_device_add()
522  */
523 void drm_sysfs_device_remove(struct drm_minor *minor)
524 {
525         device_unregister(&minor->kdev);
526 }
527
528
529 /**
530  * drm_class_device_register - Register a struct device in the drm class.
531  *
532  * @dev: pointer to struct device to register.
533  *
534  * @dev should have all relevant members pre-filled with the exception
535  * of the class member. In particular, the device_type member must
536  * be set.
537  */
538
539 int drm_class_device_register(struct device *dev)
540 {
541         dev->class = drm_class;
542         return device_register(dev);
543 }
544 EXPORT_SYMBOL_GPL(drm_class_device_register);
545
546 void drm_class_device_unregister(struct device *dev)
547 {
548         return device_unregister(dev);
549 }
550 EXPORT_SYMBOL_GPL(drm_class_device_unregister);