blob: de154556c4059b66f1f153f822d6a3f5c2207ffb [file] [log] [blame]
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -08001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/device.h>
16#include <linux/kdev_t.h>
17#include <linux/err.h>
18
Thomas Hellstrom327c2252009-08-17 16:28:37 +020019#include "drm_sysfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "drm_core.h"
Dave Airlief2109732005-09-05 21:33:44 +100021#include "drmP.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Dave Airlie2c14f282008-04-21 16:47:32 +100023#define to_drm_minor(d) container_of(d, struct drm_minor, kdev)
Dave Airlief453ba02008-11-07 14:05:41 -080024#define to_drm_connector(d) container_of(d, struct drm_connector, kdev)
Jesse Barnese8b962b2007-11-22 14:02:38 +100025
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 */
34static int drm_sysfs_suspend(struct device *dev, pm_message_t state)
35{
Dave Airlie2c14f282008-04-21 16:47:32 +100036 struct drm_minor *drm_minor = to_drm_minor(dev);
37 struct drm_device *drm_dev = drm_minor->dev;
Jesse Barnese8b962b2007-11-22 14:02:38 +100038
Kristian Høgsberg112b7152009-01-04 16:55:33 -050039 if (drm_minor->type == DRM_MINOR_LEGACY &&
40 !drm_core_check_feature(drm_dev, DRIVER_MODESET) &&
41 drm_dev->driver->suspend)
Dave Airlieb932ccb2008-02-20 10:02:20 +100042 return drm_dev->driver->suspend(drm_dev, state);
Jesse Barnese8b962b2007-11-22 14:02:38 +100043
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 */
54static int drm_sysfs_resume(struct device *dev)
55{
Dave Airlie2c14f282008-04-21 16:47:32 +100056 struct drm_minor *drm_minor = to_drm_minor(dev);
57 struct drm_device *drm_dev = drm_minor->dev;
Jesse Barnese8b962b2007-11-22 14:02:38 +100058
Kristian Høgsberg112b7152009-01-04 16:55:33 -050059 if (drm_minor->type == DRM_MINOR_LEGACY &&
60 !drm_core_check_feature(drm_dev, DRIVER_MODESET) &&
61 drm_dev->driver->resume)
Jesse Barnese8b962b2007-11-22 14:02:38 +100062 return drm_dev->driver->resume(drm_dev);
63
64 return 0;
65}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/* Display the version of drm_core. This doesn't work right in current design */
68static 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
Kay Sievers02200d02009-04-30 15:23:42 +020074static char *drm_nodename(struct device *dev)
75{
76 return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
77}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static 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 *
Jesse Barnese8b962b2007-11-22 14:02:38 +100086 * This is used to create DRM class pointer that can then be used
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * 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 */
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -080092struct class *drm_sysfs_create(struct module *owner, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -080094 struct class *class;
Jeff Garzik24f73c92006-10-10 14:23:37 -070095 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -080097 class = class_create(owner, name);
Akinobu Mita94f060b2006-12-09 10:49:47 +110098 if (IS_ERR(class)) {
99 err = PTR_ERR(class);
Jeff Garzik24f73c92006-10-10 14:23:37 -0700100 goto err_out;
101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Jesse Barnese8b962b2007-11-22 14:02:38 +1000103 class->suspend = drm_sysfs_suspend;
104 class->resume = drm_sysfs_resume;
105
Jeff Garzik24f73c92006-10-10 14:23:37 -0700106 err = class_create_file(class, &class_attr_version);
107 if (err)
108 goto err_out_class;
109
Kay Sievers02200d02009-04-30 15:23:42 +0200110 class->nodename = drm_nodename;
111
Greg Kroah-Hartman0650fd52006-01-20 14:08:59 -0800112 return class;
Jeff Garzik24f73c92006-10-10 14:23:37 -0700113
114err_out_class:
115 class_destroy(class);
116err_out:
117 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120/**
Jesse Barnese8b962b2007-11-22 14:02:38 +1000121 * drm_sysfs_destroy - destroys DRM class
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 *
Jesse Barnese8b962b2007-11-22 14:02:38 +1000123 * Destroy the DRM device class.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 */
Jesse Barnese8b962b2007-11-22 14:02:38 +1000125void drm_sysfs_destroy(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Jesse Barnese8b962b2007-11-22 14:02:38 +1000127 if ((drm_class == NULL) || (IS_ERR(drm_class)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return;
Jesse Barnese8b962b2007-11-22 14:02:38 +1000129 class_remove_file(drm_class, &class_attr_version);
130 class_destroy(drm_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133/**
Jesse Barnese8b962b2007-11-22 14:02:38 +1000134 * drm_sysfs_device_release - do nothing
135 * @dev: Linux device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 *
Jesse Barnese8b962b2007-11-22 14:02:38 +1000137 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
Jesse Barnese8b962b2007-11-22 14:02:38 +1000141static void drm_sysfs_device_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Ma Ling77d26dc2009-04-16 17:51:25 +0800143 memset(dev, 0, sizeof(struct device));
Jesse Barnese8b962b2007-11-22 14:02:38 +1000144 return;
145}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Dave Airlief453ba02008-11-07 14:05:41 -0800147/*
148 * Connector properties
149 */
150static 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);
Keith Packard75185c92009-05-30 20:42:25 -0700158 return snprintf(buf, PAGE_SIZE, "%s\n",
Dave Airlief453ba02008-11-07 14:05:41 -0800159 drm_get_connector_status_name(status));
160}
161
162static 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
Keith Packard75185c92009-05-30 20:42:25 -0700177 return snprintf(buf, PAGE_SIZE, "%s\n",
Dave Airlief453ba02008-11-07 14:05:41 -0800178 drm_get_dpms_name((int)dpms_status));
179}
180
181static 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
Keith Packard75185c92009-05-30 20:42:25 -0700187 return snprintf(buf, PAGE_SIZE, "%s\n", connector->encoder ? "enabled" :
Dave Airlief453ba02008-11-07 14:05:41 -0800188 "disabled");
189}
190
191static 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
217static 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
233static 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:
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200251 case DRM_MODE_CONNECTOR_TV:
Dave Airlief453ba02008-11-07 14:05:41 -0800252 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
274static 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:
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200292 case DRM_MODE_CONNECTOR_TV:
Dave Airlief453ba02008-11-07 14:05:41 -0800293 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
315static 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. */
323static struct device_attribute connector_attrs_opt1[] = {
324 __ATTR_RO(subconnector),
325 __ATTR_RO(select_subconnector),
326};
327
328static struct bin_attribute edid_attr = {
329 .attr.name = "edid",
Keith Packarde36ebaf2009-05-30 20:42:26 -0700330 .attr.mode = 0444,
Dave Airlief453ba02008-11-07 14:05:41 -0800331 .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 */
349int 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
Kay Sievers2ead0542009-03-24 16:38:22 -0700364 dev_set_name(&connector->kdev, "card%d-%s",
365 dev->primary->index, drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -0800366 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:
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200391 case DRM_MODE_CONNECTOR_TV:
Dave Airlief453ba02008-11-07 14:05:41 -0800392 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
411err_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
418out:
419 return ret;
420}
421EXPORT_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 */
436void 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}
448EXPORT_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 */
458void 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}
Jesse Barnes5ca58282009-03-31 14:11:15 -0700467EXPORT_SYMBOL(drm_sysfs_hotplug_event);
Dave Airlief453ba02008-11-07 14:05:41 -0800468
Jesse Barnese8b962b2007-11-22 14:02:38 +1000469/**
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 */
Dave Airlie2c14f282008-04-21 16:47:32 +1000478int drm_sysfs_device_add(struct drm_minor *minor)
Jesse Barnese8b962b2007-11-22 14:02:38 +1000479{
480 int err;
Dave Airlie2c14f282008-04-21 16:47:32 +1000481 char *minor_str;
Jesse Barnese8b962b2007-11-22 14:02:38 +1000482
Dave Airlie2c14f282008-04-21 16:47:32 +1000483 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;
Dave Airlief453ba02008-11-07 14:05:41 -0800487 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";
Jesse Barnese8b962b2007-11-22 14:02:38 +1000493
Kay Sievers8f4bbd92009-01-06 10:44:41 -0800494 dev_set_name(&minor->kdev, minor_str, minor->index);
Dave Airlie2c14f282008-04-21 16:47:32 +1000495
496 err = device_register(&minor->kdev);
Jesse Barnese8b962b2007-11-22 14:02:38 +1000497 if (err) {
498 DRM_ERROR("device add failed: %d\n", err);
Jeff Garzik24f73c92006-10-10 14:23:37 -0700499 goto err_out;
500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Jesse Barnese8b962b2007-11-22 14:02:38 +1000502 return 0;
Jeff Garzik24f73c92006-10-10 14:23:37 -0700503
Jeff Garzik24f73c92006-10-10 14:23:37 -0700504err_out:
Jesse Barnese8b962b2007-11-22 14:02:38 +1000505 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
508/**
Jesse Barnese8b962b2007-11-22 14:02:38 +1000509 * drm_sysfs_device_remove - remove DRM device
510 * @dev: DRM device to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 *
512 * This call unregisters and cleans up a class device that was created with a
513 * call to drm_sysfs_device_add()
514 */
Dave Airlie2c14f282008-04-21 16:47:32 +1000515void drm_sysfs_device_remove(struct drm_minor *minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Dave Airlie2c14f282008-04-21 16:47:32 +1000517 device_unregister(&minor->kdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
Thomas Hellstrom327c2252009-08-17 16:28:37 +0200519
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
531int drm_class_device_register(struct device *dev)
532{
533 dev->class = drm_class;
534 return device_register(dev);
535}
536EXPORT_SYMBOL_GPL(drm_class_device_register);
537
538void drm_class_device_unregister(struct device *dev)
539{
540 return device_unregister(dev);
541}
542EXPORT_SYMBOL_GPL(drm_class_device_unregister);