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