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