[modesetting-101] update mode count after fill_modes.
[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 edid_show(struct kobject *kobj, struct bin_attribute *attr,
180                          char *buf, loff_t off, size_t count)
181 {
182         struct device *connector_dev = container_of(kobj, struct device, kobj);
183         struct drm_connector *connector = container_of(connector_dev, struct drm_connector,
184                                                  kdev);
185         unsigned char *edid;
186         size_t size;
187
188         if (!connector->edid_blob_ptr)
189                 return 0;
190
191         edid = connector->edid_blob_ptr->data;
192         size = connector->edid_blob_ptr->length;
193         if (!edid)
194                 return 0;
195                 
196         if (off >= size)
197                 return 0;
198
199         if (off + count > size)
200                 count = size - off;
201         memcpy(buf, edid + off, count);
202
203         return count;
204 }
205
206 static ssize_t modes_show(struct device *device,
207                            struct device_attribute *attr,
208                            char *buf)
209 {
210         struct drm_connector *connector = container_of(device, struct drm_connector, kdev);
211         struct drm_display_mode *mode;
212         int written = 0;
213
214         list_for_each_entry(mode, &connector->modes, head) {
215                 written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
216                                     mode->name);
217         }
218
219         return written;
220 }
221
222 static struct device_attribute connector_attrs[] = {
223         __ATTR_RO(status),
224         __ATTR_RO(dpms),
225         __ATTR_RO(modes),
226 };
227
228 static struct bin_attribute edid_attr = {
229         .attr.name = "edid",
230         .size = 128,
231         .read = edid_show,
232 };
233
234 /**
235  * drm_sysfs_connector_add - add an connector to sysfs
236  * @connector: connector to add
237  *
238  * Create an connector device in sysfs, along with its associated connector
239  * properties (so far, connection status, dpms, mode list & edid) and
240  * generate a hotplug event so userspace knows there's a new connector
241  * available.
242  *
243  * Note:
244  * This routine should only be called *once* for each DRM minor registered.
245  * A second call for an already registered device will trigger the BUG_ON
246  * below.
247  */
248 int drm_sysfs_connector_add(struct drm_connector *connector)
249 {
250         struct drm_device *dev = connector->dev;
251         int ret = 0, i, j;
252
253         /* We shouldn't get called more than once for the same connector */
254         BUG_ON(device_is_registered(&connector->kdev));
255
256         connector->kdev.parent = &dev->primary->kdev;
257         connector->kdev.class = drm_class;
258         connector->kdev.release = drm_sysfs_device_release;
259
260         DRM_DEBUG("adding \"%s\" to sysfs\n",
261                   drm_get_connector_name(connector));
262
263         snprintf(connector->kdev.bus_id, BUS_ID_SIZE, "card%d-%s",
264                  dev->primary->index, drm_get_connector_name(connector));
265         ret = device_register(&connector->kdev);
266
267         if (ret) {
268                 DRM_ERROR("failed to register connector device: %d\n", ret);
269                 goto out;
270         }
271
272         for (i = 0; i < ARRAY_SIZE(connector_attrs); i++) {
273                 ret = device_create_file(&connector->kdev, &connector_attrs[i]);
274                 if (ret)
275                         goto err_out_files;
276         }
277
278         ret = sysfs_create_bin_file(&connector->kdev.kobj, &edid_attr);
279         if (ret)
280                 goto err_out_files;
281
282         /* Let userspace know we have a new connector */
283         drm_sysfs_hotplug_event(dev);
284
285         return 0;
286
287 err_out_files:
288         if (i > 0)
289                 for (j = 0; j < i; j++)
290                         device_remove_file(&connector->kdev, &connector_attrs[i]);
291         device_unregister(&connector->kdev);
292
293 out:
294         return ret;
295 }
296 EXPORT_SYMBOL(drm_sysfs_connector_add);
297
298 /**
299  * drm_sysfs_connector_remove - remove an connector device from sysfs
300  * @connector: connector to remove
301  *
302  * Remove @connector and its associated attributes from sysfs.  Note that
303  * the device model core will take care of sending the "remove" uevent
304  * at this time, so we don't need to do it.
305  *
306  * Note:
307  * This routine should only be called if the connector was previously
308  * successfully registered.  If @connector hasn't been registered yet,
309  * you'll likely see a panic somewhere deep in sysfs code when called.
310  */
311 void drm_sysfs_connector_remove(struct drm_connector *connector)
312 {
313         int i;
314
315         DRM_DEBUG("removing \"%s\" from sysfs\n",
316                   drm_get_connector_name(connector));
317
318         for (i = 0; i < ARRAY_SIZE(connector_attrs); i++)
319                 device_remove_file(&connector->kdev, &connector_attrs[i]);
320         sysfs_remove_bin_file(&connector->kdev.kobj, &edid_attr);
321         device_unregister(&connector->kdev);
322 }
323 EXPORT_SYMBOL(drm_sysfs_connector_remove);
324
325 /**
326  * drm_sysfs_hotplug_event - generate a DRM uevent
327  * @dev: DRM device
328  *
329  * Send a uevent for the DRM device specified by @dev.  Currently we only
330  * set HOTPLUG=1 in the uevent environment, but this could be expanded to
331  * deal with other types of events.
332  */
333 void drm_sysfs_hotplug_event(struct drm_device *dev)
334 {
335         char *event_string = "HOTPLUG=1";
336         char *envp[] = { event_string, NULL };
337
338         DRM_DEBUG("generating hotplug event\n");
339
340         kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, envp);
341 }
342
343 static struct device_attribute dri_attrs[] = {
344         __ATTR(dri_library_name, S_IRUGO, show_dri, NULL),
345 };
346
347 /**
348  * drm_sysfs_device_add - adds a class device to sysfs for a character driver
349  * @dev: DRM device to be added
350  * @head: DRM head in question
351  *
352  * Add a DRM device to the DRM's device model class.  We use @dev's PCI device
353  * as the parent for the Linux device, and make sure it has a file containing
354  * the driver we're using (for userspace compatibility).
355  *
356  * Note:
357  * This routine should only be called *once* for each DRM minor registered.
358  * A second call for an already registered device will trigger the BUG_ON
359  * below.
360  */
361 int drm_sysfs_device_add(struct drm_minor *minor)
362 {
363         int err;
364         int i, j;
365         char *minor_str;
366
367         minor->kdev.parent = &minor->dev->pdev->dev;
368         minor->kdev.class = drm_class;
369         minor->kdev.release = drm_sysfs_device_release;
370         minor->kdev.devt = minor->device;
371         if (minor->type == DRM_MINOR_CONTROL)
372                 minor_str = "controlD%d";
373         else if (minor->type == DRM_MINOR_RENDER)
374                 minor_str = "renderD%d";
375         else
376                 minor_str = "card%d";
377         
378         snprintf(minor->kdev.bus_id, BUS_ID_SIZE, minor_str, minor->index);
379
380         /* Shouldn't register more than once */
381         BUG_ON(device_is_registered(&minor->kdev));
382
383         DRM_DEBUG("registering DRM device \"%s\"\n", minor->kdev.bus_id);
384
385         err = device_register(&minor->kdev);
386         if (err) {
387                 DRM_ERROR("device add failed: %d\n", err);
388                 goto err_out;
389         }
390
391         for (i = 0; i < ARRAY_SIZE(dri_attrs); i++) {
392                 err = device_create_file(&minor->kdev, &dri_attrs[i]);
393                 if (err)
394                         goto err_out_files;
395         }
396
397         return 0;
398
399 err_out_files:
400         if (i > 0)
401                 for (j = 0; j < i; j++)
402                         device_remove_file(&minor->kdev, &dri_attrs[i]);
403         device_unregister(&minor->kdev);
404 err_out:
405
406         return err;
407 }
408
409 /**
410  * drm_sysfs_device_remove - remove DRM device
411  * @dev: DRM device to remove
412  *
413  * This call unregisters and cleans up a class device that was created with a
414  * call to drm_sysfs_device_add()
415  */
416 void drm_sysfs_device_remove(struct drm_minor *minor)
417 {
418         int i;
419
420         for (i = 0; i < ARRAY_SIZE(dri_attrs); i++)
421                 device_remove_file(&minor->kdev, &dri_attrs[i]);
422         device_unregister(&minor->kdev);
423 }