]>
Commit | Line | Data |
---|---|---|
0650fd58 | 1 | |
1da177e4 LT |
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 | ||
1da177e4 LT |
15 | #include <linux/device.h> |
16 | #include <linux/kdev_t.h> | |
17 | #include <linux/err.h> | |
18 | ||
327c225b | 19 | #include "drm_sysfs.h" |
1da177e4 | 20 | #include "drm_core.h" |
f210973b | 21 | #include "drmP.h" |
1da177e4 | 22 | |
2c14f28b | 23 | #define to_drm_minor(d) container_of(d, struct drm_minor, kdev) |
f453ba04 | 24 | #define to_drm_connector(d) container_of(d, struct drm_connector, kdev) |
e8b962b6 | 25 | |
08e4d534 TH |
26 | static struct device_type drm_sysfs_device_minor = { |
27 | .name = "drm_minor" | |
28 | }; | |
29 | ||
e8b962b6 | 30 | /** |
08e4d534 | 31 | * drm_class_suspend - DRM class suspend hook |
e8b962b6 JB |
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 | */ | |
08e4d534 | 38 | static int drm_class_suspend(struct device *dev, pm_message_t state) |
e8b962b6 | 39 | { |
08e4d534 TH |
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 | } | |
e8b962b6 JB |
49 | return 0; |
50 | } | |
51 | ||
52 | /** | |
08e4d534 | 53 | * drm_class_resume - DRM class resume hook |
e8b962b6 JB |
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 | */ | |
08e4d534 | 59 | static int drm_class_resume(struct device *dev) |
e8b962b6 | 60 | { |
08e4d534 TH |
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 | } | |
e8b962b6 JB |
70 | return 0; |
71 | } | |
72 | ||
1da177e4 | 73 | /* Display the version of drm_core. This doesn't work right in current design */ |
28812fe1 AK |
74 | static ssize_t version_show(struct class *dev, struct class_attribute *attr, |
75 | char *buf) | |
1da177e4 LT |
76 | { |
77 | return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR, | |
78 | CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE); | |
79 | } | |
80 | ||
e454cea2 | 81 | static char *drm_devnode(struct device *dev, mode_t *mode) |
02200d06 KS |
82 | { |
83 | return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev)); | |
84 | } | |
85 | ||
1da177e4 LT |
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 | * | |
e8b962b6 | 93 | * This is used to create DRM class pointer that can then be used |
1da177e4 LT |
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 | */ | |
0650fd58 | 99 | struct class *drm_sysfs_create(struct module *owner, char *name) |
1da177e4 | 100 | { |
0650fd58 | 101 | struct class *class; |
24f73c92 | 102 | int err; |
0650fd58 GKH |
103 | |
104 | class = class_create(owner, name); | |
94f060bd AM |
105 | if (IS_ERR(class)) { |
106 | err = PTR_ERR(class); | |
24f73c92 JG |
107 | goto err_out; |
108 | } | |
109 | ||
08e4d534 TH |
110 | class->suspend = drm_class_suspend; |
111 | class->resume = drm_class_resume; | |
e8b962b6 | 112 | |
24f73c92 JG |
113 | err = class_create_file(class, &class_attr_version); |
114 | if (err) | |
115 | goto err_out_class; | |
0650fd58 | 116 | |
e454cea2 | 117 | class->devnode = drm_devnode; |
02200d06 | 118 | |
0650fd58 | 119 | return class; |
24f73c92 JG |
120 | |
121 | err_out_class: | |
122 | class_destroy(class); | |
123 | err_out: | |
124 | return ERR_PTR(err); | |
1da177e4 LT |
125 | } |
126 | ||
127 | /** | |
e8b962b6 | 128 | * drm_sysfs_destroy - destroys DRM class |
1da177e4 | 129 | * |
e8b962b6 | 130 | * Destroy the DRM device class. |
1da177e4 | 131 | */ |
e8b962b6 | 132 | void drm_sysfs_destroy(void) |
1da177e4 | 133 | { |
e8b962b6 | 134 | if ((drm_class == NULL) || (IS_ERR(drm_class))) |
1da177e4 | 135 | return; |
e8b962b6 JB |
136 | class_remove_file(drm_class, &class_attr_version); |
137 | class_destroy(drm_class); | |
1da177e4 LT |
138 | } |
139 | ||
e8b962b6 JB |
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 | { | |
77d26dc9 | 150 | memset(dev, 0, sizeof(struct device)); |
e8b962b6 JB |
151 | return; |
152 | } | |
153 | ||
f453ba04 DA |
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); | |
75185c92 | 165 | return snprintf(buf, PAGE_SIZE, "%s\n", |
f453ba04 DA |
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 | ||
75185c92 | 184 | return snprintf(buf, PAGE_SIZE, "%s\n", |
f453ba04 DA |
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 | ||
75185c92 | 194 | return snprintf(buf, PAGE_SIZE, "%s\n", connector->encoder ? "enabled" : |
f453ba04 DA |
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: | |
74bd3c26 | 258 | case DRM_MODE_CONNECTOR_TV: |
f453ba04 DA |
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: | |
74bd3c26 | 299 | case DRM_MODE_CONNECTOR_TV: |
f453ba04 DA |
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", | |
e36ebaf4 | 337 | .attr.mode = 0444, |
f453ba04 DA |
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 | ||
2ead054c KS |
371 | dev_set_name(&connector->kdev, "card%d-%s", |
372 | dev->primary->index, drm_get_connector_name(connector)); | |
f453ba04 DA |
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: | |
74bd3c26 | 398 | case DRM_MODE_CONNECTOR_TV: |
f453ba04 DA |
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 | } | |
5ca58282 | 474 | EXPORT_SYMBOL(drm_sysfs_hotplug_event); |
f453ba04 | 475 | |
1da177e4 LT |
476 | /** |
477 | * drm_sysfs_device_add - adds a class device to sysfs for a character driver | |
e8b962b6 JB |
478 | * @dev: DRM device to be added |
479 | * @head: DRM head in question | |
1da177e4 | 480 | * |
e8b962b6 JB |
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). | |
1da177e4 | 484 | */ |
2c14f28b | 485 | int drm_sysfs_device_add(struct drm_minor *minor) |
1da177e4 | 486 | { |
e8b962b6 | 487 | int err; |
2c14f28b | 488 | char *minor_str; |
e8b962b6 | 489 | |
2c14f28b DA |
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; | |
08e4d534 | 494 | minor->kdev.type = &drm_sysfs_device_minor; |
f453ba04 DA |
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"; | |
e8b962b6 | 501 | |
8f4bbd9f | 502 | dev_set_name(&minor->kdev, minor_str, minor->index); |
2c14f28b DA |
503 | |
504 | err = device_register(&minor->kdev); | |
e8b962b6 JB |
505 | if (err) { |
506 | DRM_ERROR("device add failed: %d\n", err); | |
24f73c92 JG |
507 | goto err_out; |
508 | } | |
1da177e4 | 509 | |
e8b962b6 | 510 | return 0; |
24f73c92 | 511 | |
24f73c92 | 512 | err_out: |
e8b962b6 | 513 | return err; |
1da177e4 LT |
514 | } |
515 | ||
516 | /** | |
e8b962b6 JB |
517 | * drm_sysfs_device_remove - remove DRM device |
518 | * @dev: DRM device to remove | |
1da177e4 LT |
519 | * |
520 | * This call unregisters and cleans up a class device that was created with a | |
521 | * call to drm_sysfs_device_add() | |
522 | */ | |
2c14f28b | 523 | void drm_sysfs_device_remove(struct drm_minor *minor) |
1da177e4 | 524 | { |
2c14f28b | 525 | device_unregister(&minor->kdev); |
1da177e4 | 526 | } |
327c225b TH |
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); |