2 * Copyright (c) 2006-2007 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
6 * DRM core CRTC related functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/list.h>
37 struct drm_prop_enum_list {
45 static struct drm_prop_enum_list drm_dpms_enum_list[] =
46 { { DPMSModeOn, "On" },
47 { DPMSModeStandby, "Standby" },
48 { DPMSModeSuspend, "Suspend" },
49 { DPMSModeOff, "Off" }
52 char *drm_get_dpms_name(int val)
56 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
57 if (drm_dpms_enum_list[i].type == val)
58 return drm_dpms_enum_list[i].name;
63 static struct drm_prop_enum_list drm_select_subconnector_enum_list[] =
65 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
66 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
67 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
68 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
69 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
70 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
73 char *drm_get_select_subconnector_name(int val)
77 for (i = 0; i < ARRAY_SIZE(drm_select_subconnector_enum_list); i++)
78 if (drm_select_subconnector_enum_list[i].type == val)
79 return drm_select_subconnector_enum_list[i].name;
84 static struct drm_prop_enum_list drm_subconnector_enum_list[] =
86 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
87 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
88 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
89 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
90 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
91 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
94 char *drm_get_subconnector_name(int val)
98 for (i = 0; i < ARRAY_SIZE(drm_subconnector_enum_list); i++)
99 if (drm_subconnector_enum_list[i].type == val)
100 return drm_subconnector_enum_list[i].name;
105 static struct drm_prop_enum_list drm_connector_enum_list[] =
106 { { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
107 { DRM_MODE_CONNECTOR_VGA, "VGA" },
108 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
109 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
110 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
111 { DRM_MODE_CONNECTOR_Composite, "Composite" },
112 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
113 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
114 { DRM_MODE_CONNECTOR_Component, "Component" },
115 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
116 { DRM_MODE_CONNECTOR_DisplayPort, "DisplayPort" },
117 { DRM_MODE_CONNECTOR_HDMIA, "HDMI Type A" },
118 { DRM_MODE_CONNECTOR_HDMIB, "HDMI Type B" },
120 static struct drm_prop_enum_list drm_encoder_enum_list[] =
121 { { DRM_MODE_ENCODER_NONE, "None" },
122 { DRM_MODE_ENCODER_DAC, "DAC" },
123 { DRM_MODE_ENCODER_TMDS, "TMDS" },
124 { DRM_MODE_ENCODER_LVDS, "LVDS" },
125 { DRM_MODE_ENCODER_TVDAC, "TV" },
128 char *drm_get_encoder_name(struct drm_encoder *encoder)
132 snprintf(buf, 32, "%s-%d", drm_encoder_enum_list[encoder->encoder_type].name,
137 char *drm_get_connector_name(struct drm_connector *connector)
141 snprintf(buf, 32, "%s-%d", drm_connector_enum_list[connector->connector_type].name,
142 connector->connector_type_id);
145 EXPORT_SYMBOL(drm_get_connector_name);
147 char *drm_get_connector_status_name(enum drm_connector_status status)
149 if (status == connector_status_connected)
151 else if (status == connector_status_disconnected)
152 return "disconnected";
158 * drm_idr_get - allocate a new identifier
160 * @ptr: object pointer, used to generate unique ID
163 * Caller must hold DRM mode_config lock.
165 * Create a unique identifier based on @ptr in @dev's identifier space. Used
166 * for tracking modes, CRTCs and connectors.
169 * New unique (relative to other objects in @dev) integer identifier for the
172 static int drm_mode_object_get(struct drm_device *dev, struct drm_mode_object *obj, uint32_t obj_type)
177 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
178 DRM_ERROR("Ran out memory getting a mode number\n");
182 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
187 obj->type = obj_type;
192 * drm_mode_object_put - free an identifer
197 * Caller must hold DRM mode_config lock.
199 * Free @id from @dev's unique identifier pool.
201 static void drm_mode_object_put(struct drm_device *dev, struct drm_mode_object *object)
203 idr_remove(&dev->mode_config.crtc_idr, object->id);
206 static void *drm_mode_object_find(struct drm_device *dev, uint32_t id, uint32_t type)
208 struct drm_mode_object *obj;
210 obj = idr_find(&dev->mode_config.crtc_idr, id);
211 if (!obj || (obj->type != type) || (obj->id != id))
218 * drm_crtc_from_fb - find the CRTC structure associated with an fb
220 * @fb: framebuffer in question
223 * Caller must hold mode_config lock.
225 * Find CRTC in the mode_config structure that matches @fb.
228 * Pointer to the CRTC or NULL if it wasn't found.
230 struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
231 struct drm_framebuffer *fb)
233 struct drm_crtc *crtc;
235 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
243 * drm_framebuffer_create - create a new framebuffer object
247 * Caller must hold mode config lock.
249 * Creates a new framebuffer objects and adds it to @dev's DRM mode_config.
252 * Pointer to new framebuffer or NULL on error.
254 struct drm_framebuffer *drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
255 const struct drm_framebuffer_funcs *funcs)
257 drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
260 dev->mode_config.num_fb++;
261 list_add(&fb->head, &dev->mode_config.fb_list);
265 EXPORT_SYMBOL(drm_framebuffer_init);
268 * drm_framebuffer_cleanup - remove a framebuffer object
269 * @fb: framebuffer to remove
272 * Caller must hold mode config lock.
274 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
275 * it, setting it to NULL.
277 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
279 struct drm_device *dev = fb->dev;
280 struct drm_crtc *crtc;
282 /* remove from any CRTC */
283 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
288 drm_mode_object_put(dev, &fb->base);
290 dev->mode_config.num_fb--;
292 EXPORT_SYMBOL(drm_framebuffer_cleanup);
295 * drm_crtc_init - Initialise a new CRTC object
297 * @crtc: CRTC object to init
298 * @funcs: callbacks for the new CRTC
301 * Caller must hold mode config lock.
303 * Inits a new object created as base part of an driver crtc object.
305 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
306 const struct drm_crtc_funcs *funcs)
311 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
313 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
314 dev->mode_config.num_crtc++;
316 EXPORT_SYMBOL(drm_crtc_init);
319 * drm_crtc_cleanup - Cleans up the core crtc usage.
320 * @crtc: CRTC to cleanup
323 * Caller must hold mode config lock.
325 * Cleanup @crtc. Removes from drm modesetting space
326 * does NOT free object, caller does that.
328 void drm_crtc_cleanup(struct drm_crtc *crtc)
330 struct drm_device *dev = crtc->dev;
332 if (crtc->gamma_store) {
333 kfree(crtc->gamma_store);
334 crtc->gamma_store = NULL;
337 drm_mode_object_put(dev, &crtc->base);
338 list_del(&crtc->head);
339 dev->mode_config.num_crtc--;
341 EXPORT_SYMBOL(drm_crtc_cleanup);
344 * drm_mode_probed_add - add a mode to the specified connector's probed mode list
345 * @connector: connector the new mode
349 * Caller must hold mode config lock.
351 * Add @mode to @connector's mode list for later use.
353 void drm_mode_probed_add(struct drm_connector *connector,
354 struct drm_display_mode *mode)
356 list_add(&mode->head, &connector->probed_modes);
358 EXPORT_SYMBOL(drm_mode_probed_add);
361 * drm_mode_remove - remove and free a mode
362 * @connector: connector list to modify
363 * @mode: mode to remove
366 * Caller must hold mode config lock.
368 * Remove @mode from @connector's mode list, then free it.
370 void drm_mode_remove(struct drm_connector *connector, struct drm_display_mode *mode)
372 list_del(&mode->head);
375 EXPORT_SYMBOL(drm_mode_remove);
378 * drm_connector_init - Init a preallocated connector
380 * @connector: the connector to init
381 * @funcs: callbacks for this connector
382 * @name: user visible name of the connector
385 * Caller must hold @dev's mode_config lock.
387 * Initialises a preallocated connector. Connectors should be
388 * subclassed as part of driver connector objects.
390 void drm_connector_init(struct drm_device *dev,
391 struct drm_connector *connector,
392 const struct drm_connector_funcs *funcs,
395 connector->dev = dev;
396 connector->funcs = funcs;
397 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
398 connector->connector_type = connector_type;
399 connector->connector_type_id = 1; /* TODO */
400 INIT_LIST_HEAD(&connector->user_modes);
401 INIT_LIST_HEAD(&connector->probed_modes);
402 INIT_LIST_HEAD(&connector->modes);
403 connector->edid_blob_ptr = NULL;
404 /* randr_connector? */
405 /* connector_set_monitor(connector)? */
406 /* check for connector_ignored(connector)? */
408 mutex_lock(&dev->mode_config.mutex);
409 list_add_tail(&connector->head, &dev->mode_config.connector_list);
410 dev->mode_config.num_connector++;
412 drm_connector_attach_property(connector, dev->mode_config.edid_property, 0);
414 drm_connector_attach_property(connector, dev->mode_config.dpms_property, 0);
416 mutex_unlock(&dev->mode_config.mutex);
418 EXPORT_SYMBOL(drm_connector_init);
421 * drm_connector_cleanup - cleans up an initialised connector
422 * @connector: connector to cleanup
425 * Caller must hold @dev's mode_config lock.
427 * Cleans up the connector but doesn't free the object.
429 void drm_connector_cleanup(struct drm_connector *connector)
431 struct drm_device *dev = connector->dev;
432 struct drm_display_mode *mode, *t;
434 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
435 drm_mode_remove(connector, mode);
437 list_for_each_entry_safe(mode, t, &connector->modes, head)
438 drm_mode_remove(connector, mode);
440 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
441 drm_mode_remove(connector, mode);
443 mutex_lock(&dev->mode_config.mutex);
444 drm_mode_object_put(dev, &connector->base);
445 list_del(&connector->head);
446 mutex_unlock(&dev->mode_config.mutex);
448 EXPORT_SYMBOL(drm_connector_cleanup);
450 void drm_encoder_init(struct drm_device *dev,
451 struct drm_encoder *encoder,
452 const struct drm_encoder_funcs *funcs,
457 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
458 encoder->encoder_type = encoder_type;
459 encoder->funcs = funcs;
461 mutex_lock(&dev->mode_config.mutex);
462 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
463 dev->mode_config.num_encoder++;
465 mutex_unlock(&dev->mode_config.mutex);
467 EXPORT_SYMBOL(drm_encoder_init);
469 void drm_encoder_cleanup(struct drm_encoder *encoder)
471 struct drm_device *dev = encoder->dev;
472 mutex_lock(&dev->mode_config.mutex);
473 drm_mode_object_put(dev, &encoder->base);
474 list_del(&encoder->head);
475 mutex_unlock(&dev->mode_config.mutex);
477 EXPORT_SYMBOL(drm_encoder_cleanup);
480 * drm_mode_create - create a new display mode
486 * Create a new drm_display_mode, give it an ID, and return it.
489 * Pointer to new mode on success, NULL on error.
491 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
493 struct drm_display_mode *nmode;
495 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
499 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
502 EXPORT_SYMBOL(drm_mode_create);
505 * drm_mode_destroy - remove a mode
507 * @mode: mode to remove
510 * Caller must hold mode config lock.
512 * Free @mode's unique identifier, then free it.
514 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
516 drm_mode_object_put(dev, &mode->base);
520 EXPORT_SYMBOL(drm_mode_destroy);
522 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
527 * Standard properties (apply to all connectors)
529 dev->mode_config.edid_property =
530 drm_property_create(dev, DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
533 dev->mode_config.dpms_property =
534 drm_property_create(dev, DRM_MODE_PROP_ENUM,
535 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
536 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
537 drm_property_add_enum(dev->mode_config.dpms_property, i, drm_dpms_enum_list[i].type, drm_dpms_enum_list[i].name);
543 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
546 * Called by a driver the first time a DVI-I connector is made.
548 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
552 if (dev->mode_config.dvi_i_select_subconnector_property)
555 dev->mode_config.dvi_i_select_subconnector_property = drm_property_create(dev, DRM_MODE_PROP_ENUM,
556 "select subconnector", 3);
557 /* add enum element 0-2 */
558 for (i = 0; i < 3; i++)
559 drm_property_add_enum(dev->mode_config.dvi_i_select_subconnector_property, i, drm_select_subconnector_enum_list[i].type,
560 drm_select_subconnector_enum_list[i].name);
562 /* This is a property which indicates the most likely thing to be connected. */
563 dev->mode_config.dvi_i_subconnector_property = drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
565 /* add enum element 0-2 */
566 for (i = 0; i < 3; i++)
567 drm_property_add_enum(dev->mode_config.dvi_i_subconnector_property, i, drm_subconnector_enum_list[i].type,
568 drm_subconnector_enum_list[i].name);
572 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
575 * drm_create_tv_properties - create TV specific connector properties
577 * @num_modes: number of different TV formats (modes) supported
578 * @modes: array of pointers to strings containing name of each format
580 * Called by a driver's TV initialization routine, this function creates
581 * the TV specific connector properties for a given device. Caller is
582 * responsible for allocating a list of format names and passing them to
585 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
590 if (dev->mode_config.tv_select_subconnector_property) /* already done */
593 dev->mode_config.tv_select_subconnector_property = drm_property_create(dev, DRM_MODE_PROP_ENUM,
594 "select subconnector", 4);
595 /* add enum element 0 */
596 drm_property_add_enum(dev->mode_config.tv_select_subconnector_property, 0, drm_select_subconnector_enum_list[0].type,
597 drm_select_subconnector_enum_list[0].name);
598 /* add enum element 3-5 */
599 for (i = 1; i < 4; i++)
600 drm_property_add_enum(dev->mode_config.tv_select_subconnector_property, i, drm_select_subconnector_enum_list[i + 2].type,
601 drm_select_subconnector_enum_list[i + 2].name);
603 /* This is a property which indicates the most likely thing to be connected. */
604 dev->mode_config.tv_subconnector_property = drm_property_create(dev, DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
606 /* add enum element 0 */
607 drm_property_add_enum(dev->mode_config.tv_subconnector_property, 0, drm_subconnector_enum_list[0].type,
608 drm_subconnector_enum_list[0].name);
609 /* add enum element 3-5 */
610 for (i = 1; i < 4; i++)
611 drm_property_add_enum(dev->mode_config.tv_subconnector_property, i, drm_subconnector_enum_list[i + 2].type,
612 drm_subconnector_enum_list[i + 2].name);
614 dev->mode_config.tv_left_margin_property =
615 drm_property_create(dev, DRM_MODE_PROP_RANGE,
617 dev->mode_config.tv_left_margin_property->values[0] = 0;
618 dev->mode_config.tv_left_margin_property->values[1] = 100;
620 dev->mode_config.tv_right_margin_property =
621 drm_property_create(dev, DRM_MODE_PROP_RANGE,
623 dev->mode_config.tv_right_margin_property->values[0] = 0;
624 dev->mode_config.tv_right_margin_property->values[1] = 100;
626 dev->mode_config.tv_top_margin_property =
627 drm_property_create(dev, DRM_MODE_PROP_RANGE,
629 dev->mode_config.tv_top_margin_property->values[0] = 0;
630 dev->mode_config.tv_top_margin_property->values[1] = 100;
632 dev->mode_config.tv_bottom_margin_property =
633 drm_property_create(dev, DRM_MODE_PROP_RANGE,
635 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
636 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
638 dev->mode_config.tv_mode_property =
639 drm_property_create(dev, DRM_MODE_PROP_ENUM,
641 for (i = 0; i < num_modes; i++)
642 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
647 EXPORT_SYMBOL(drm_mode_create_tv_properties);
650 * drm_mode_config_init - initialize DRM mode_configuration structure
654 * None, should happen single threaded at init time.
656 * Initialize @dev's mode_config structure, used for tracking the graphics
657 * configuration of @dev.
659 void drm_mode_config_init(struct drm_device *dev)
661 mutex_init(&dev->mode_config.mutex);
662 INIT_LIST_HEAD(&dev->mode_config.fb_list);
663 INIT_LIST_HEAD(&dev->mode_config.fb_kernel_list);
664 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
665 INIT_LIST_HEAD(&dev->mode_config.connector_list);
666 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
667 INIT_LIST_HEAD(&dev->mode_config.property_list);
668 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
669 idr_init(&dev->mode_config.crtc_idr);
671 drm_mode_create_standard_connector_properties(dev);
673 /* Just to be sure */
674 dev->mode_config.num_fb = 0;
675 dev->mode_config.num_connector = 0;
676 dev->mode_config.num_crtc = 0;
677 dev->mode_config.num_encoder = 0;
678 dev->mode_config.hotplug_counter = 0;
680 EXPORT_SYMBOL(drm_mode_config_init);
682 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
684 uint32_t total_objects = 0;
686 total_objects += dev->mode_config.num_crtc;
687 total_objects += dev->mode_config.num_connector;
688 total_objects += dev->mode_config.num_encoder;
690 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
694 group->num_crtcs = 0;
695 group->num_connectors = 0;
696 group->num_encoders = 0;
700 int drm_mode_group_init_legacy_group(struct drm_device *dev, struct drm_mode_group *group)
702 struct drm_crtc *crtc;
703 struct drm_encoder *encoder;
704 struct drm_connector *connector;
706 if (drm_mode_group_init(dev, group))
709 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
710 group->id_list[group->num_crtcs++] = crtc->base.id;
712 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
713 group->id_list[group->num_crtcs + group->num_encoders++] = encoder->base.id;
715 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
716 group->id_list[group->num_crtcs + group->num_encoders + group->num_connectors++] = connector->base.id;
722 * drm_mode_config_cleanup - free up DRM mode_config info
726 * Caller must hold mode config lock.
728 * Free up all the connectors and CRTCs associated with this DRM device, then
729 * free up the framebuffers and associated buffer objects.
731 * FIXME: cleanup any dangling user buffer objects too
733 void drm_mode_config_cleanup(struct drm_device *dev)
735 struct drm_connector *connector, *ot;
736 struct drm_crtc *crtc, *ct;
737 struct drm_encoder *encoder, *enct;
738 struct drm_framebuffer *fb, *fbt;
739 struct drm_property *property, *pt;
741 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list, head) {
742 encoder->funcs->destroy(encoder);
745 list_for_each_entry_safe(connector, ot, &dev->mode_config.connector_list, head) {
746 connector->funcs->destroy(connector);
749 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list, head) {
750 drm_property_destroy(dev, property);
753 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
754 fb->funcs->destroy(fb);
757 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
758 crtc->funcs->destroy(crtc);
762 EXPORT_SYMBOL(drm_mode_config_cleanup);
764 int drm_mode_hotplug_ioctl(struct drm_device *dev,
765 void *data, struct drm_file *file_priv)
767 struct drm_mode_hotplug *arg = data;
769 arg->counter = dev->mode_config.hotplug_counter;
775 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
776 * @out: drm_mode_modeinfo struct to return to the user
777 * @in: drm_display_mode to use
782 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
785 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out, struct drm_display_mode *in)
787 out->clock = in->clock;
788 out->hdisplay = in->hdisplay;
789 out->hsync_start = in->hsync_start;
790 out->hsync_end = in->hsync_end;
791 out->htotal = in->htotal;
792 out->hskew = in->hskew;
793 out->vdisplay = in->vdisplay;
794 out->vsync_start = in->vsync_start;
795 out->vsync_end = in->vsync_end;
796 out->vtotal = in->vtotal;
797 out->vscan = in->vscan;
798 out->vrefresh = in->vrefresh;
799 out->flags = in->flags;
800 out->type = in->type;
801 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
802 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
806 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
807 * @out: drm_display_mode to return to the user
808 * @in: drm_mode_modeinfo to use
813 * Convert a drmo_mode_modeinfo into a drm_display_mode structure to return to
816 void drm_crtc_convert_umode(struct drm_display_mode *out, struct drm_mode_modeinfo *in)
818 out->clock = in->clock;
819 out->hdisplay = in->hdisplay;
820 out->hsync_start = in->hsync_start;
821 out->hsync_end = in->hsync_end;
822 out->htotal = in->htotal;
823 out->hskew = in->hskew;
824 out->vdisplay = in->vdisplay;
825 out->vsync_start = in->vsync_start;
826 out->vsync_end = in->vsync_end;
827 out->vtotal = in->vtotal;
828 out->vscan = in->vscan;
829 out->vrefresh = in->vrefresh;
830 out->flags = in->flags;
831 out->type = in->type;
832 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
833 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
837 * drm_mode_getresources - get graphics configuration
838 * @inode: inode from the ioctl
839 * @filp: file * from the ioctl
840 * @cmd: cmd from ioctl
841 * @arg: arg from ioctl
844 * Takes mode config lock.
846 * Construct a set of configuration description structures and return
847 * them to the user, including CRTC, connector and framebuffer configuration.
849 * Called by the user via ioctl.
852 * Zero on success, errno on failure.
854 int drm_mode_getresources(struct drm_device *dev,
855 void *data, struct drm_file *file_priv)
857 struct drm_mode_card_res *card_res = data;
858 struct list_head *lh;
859 struct drm_framebuffer *fb;
860 struct drm_connector *connector;
861 struct drm_crtc *crtc;
862 struct drm_encoder *encoder;
864 int connector_count = 0;
867 int encoder_count = 0;
869 uint32_t __user *fb_id;
870 uint32_t __user *crtc_id;
871 uint32_t __user *connector_id;
872 uint32_t __user *encoder_id;
873 struct drm_mode_group *mode_group;
875 mutex_lock(&dev->mode_config.mutex);
877 /* for the non-control nodes we need to limit the list of resources by IDs in the
878 group list for this node */
879 list_for_each(lh, &file_priv->fbs)
882 mode_group = &file_priv->master->minor->mode_group;
883 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
885 list_for_each(lh, &dev->mode_config.crtc_list)
888 list_for_each(lh, &dev->mode_config.connector_list)
891 list_for_each(lh, &dev->mode_config.encoder_list)
895 crtc_count = mode_group->num_crtcs;
896 connector_count = mode_group->num_connectors;
897 encoder_count = mode_group->num_encoders;
900 card_res->max_height = dev->mode_config.max_height;
901 card_res->min_height = dev->mode_config.min_height;
902 card_res->max_width = dev->mode_config.max_width;
903 card_res->min_width = dev->mode_config.min_width;
905 /* handle this in 4 parts */
907 if (card_res->count_fbs >= fb_count) {
909 fb_id = (uint32_t *)(unsigned long)card_res->fb_id_ptr;
910 list_for_each_entry(fb, &file_priv->fbs, head) {
911 if (put_user(fb->base.id, fb_id + copied)) {
918 card_res->count_fbs = fb_count;
921 if (card_res->count_crtcs >= crtc_count) {
923 crtc_id = (uint32_t *)(unsigned long)card_res->crtc_id_ptr;
924 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
925 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
926 DRM_DEBUG("CRTC ID is %d\n", crtc->base.id);
927 if (put_user(crtc->base.id, crtc_id + copied)) {
934 for (i = 0; i < mode_group->num_crtcs; i++) {
935 if (put_user(mode_group->id_list[i], crtc_id + copied)) {
943 card_res->count_crtcs = crtc_count;
946 if (card_res->count_encoders >= encoder_count) {
948 encoder_id = (uint32_t *)(unsigned long)card_res->encoder_id_ptr;
949 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
950 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
952 DRM_DEBUG("ENCODER ID is %d\n", encoder->base.id);
953 if (put_user(encoder->base.id, encoder_id + copied)) {
960 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
961 if (put_user(mode_group->id_list[i], encoder_id + copied)) {
970 card_res->count_encoders = encoder_count;
973 if (card_res->count_connectors >= connector_count) {
975 connector_id = (uint32_t *)(unsigned long)card_res->connector_id_ptr;
976 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
977 list_for_each_entry(connector, &dev->mode_config.connector_list,
979 DRM_DEBUG("CONNECTOR ID is %d\n", connector->base.id);
980 if (put_user(connector->base.id, connector_id + copied)) {
987 int start = mode_group->num_crtcs + mode_group->num_encoders;
988 for (i = start; i < start + mode_group->num_connectors; i++) {
989 if (put_user(mode_group->id_list[i], connector_id + copied)) {
997 card_res->count_connectors = connector_count;
999 DRM_DEBUG("Counted %d %d %d\n", card_res->count_crtcs,
1000 card_res->count_connectors, card_res->count_encoders);
1003 mutex_unlock(&dev->mode_config.mutex);
1008 * drm_mode_getcrtc - get CRTC configuration
1009 * @inode: inode from the ioctl
1010 * @filp: file * from the ioctl
1011 * @cmd: cmd from ioctl
1012 * @arg: arg from ioctl
1017 * Construct a CRTC configuration structure to return to the user.
1019 * Called by the user via ioctl.
1022 * Zero on success, errno on failure.
1024 int drm_mode_getcrtc(struct drm_device *dev,
1025 void *data, struct drm_file *file_priv)
1027 struct drm_mode_crtc *crtc_resp = data;
1028 struct drm_crtc *crtc;
1029 struct drm_mode_object *obj;
1032 mutex_lock(&dev->mode_config.mutex);
1034 obj = drm_mode_object_find(dev, crtc_resp->crtc_id, DRM_MODE_OBJECT_CRTC);
1039 crtc = obj_to_crtc(obj);
1041 crtc_resp->x = crtc->x;
1042 crtc_resp->y = crtc->y;
1043 crtc_resp->gamma_size = crtc->gamma_size;
1045 crtc_resp->fb_id = crtc->fb->base.id;
1047 crtc_resp->fb_id = 0;
1049 if (crtc->enabled) {
1051 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1052 crtc_resp->mode_valid = 1;
1055 crtc_resp->mode_valid = 0;
1059 mutex_unlock(&dev->mode_config.mutex);
1064 * drm_mode_getconnector - get connector configuration
1065 * @inode: inode from the ioctl
1066 * @filp: file * from the ioctl
1067 * @cmd: cmd from ioctl
1068 * @arg: arg from ioctl
1073 * Construct a connector configuration structure to return to the user.
1075 * Called by the user via ioctl.
1078 * Zero on success, errno on failure.
1080 int drm_mode_getconnector(struct drm_device *dev,
1081 void *data, struct drm_file *file_priv)
1083 struct drm_mode_get_connector *out_resp = data;
1084 struct drm_mode_object *obj;
1085 struct drm_connector *connector;
1086 struct drm_display_mode *mode;
1088 int props_count = 0;
1089 int encoders_count = 0;
1093 struct drm_mode_modeinfo u_mode;
1094 struct drm_mode_modeinfo __user *mode_ptr;
1095 uint32_t __user *prop_ptr;
1096 uint64_t __user *prop_values;
1097 uint32_t __user *encoder_ptr;
1099 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1101 DRM_DEBUG("connector id %d:\n", out_resp->connector_id);
1103 mutex_lock(&dev->mode_config.mutex);
1105 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1110 connector = obj_to_connector(obj);
1112 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1113 if (connector->property_ids[i] != 0) {
1118 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1119 if (connector->encoder_ids[i] != 0) {
1124 if (out_resp->count_modes == 0) {
1125 connector->funcs->fill_modes(connector, dev->mode_config.max_width, dev->mode_config.max_height);
1128 /* delayed so we get modes regardless of pre-fill_modes state */
1129 list_for_each_entry(mode, &connector->modes, head)
1132 out_resp->connector_id = connector->base.id;
1133 out_resp->connector_type = connector->connector_type;
1134 out_resp->connector_type_id = connector->connector_type_id;
1135 out_resp->mm_width = connector->display_info.width_mm;
1136 out_resp->mm_height = connector->display_info.height_mm;
1137 out_resp->subpixel = connector->display_info.subpixel_order;
1138 out_resp->connection = connector->status;
1139 if (connector->encoder)
1140 out_resp->encoder_id = connector->encoder->base.id;
1142 out_resp->encoder_id = 0;
1144 /* this ioctl is called twice, once to determine how much space is needed, and the 2nd time to fill it */
1145 if ((out_resp->count_modes >= mode_count) && mode_count) {
1147 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1148 list_for_each_entry(mode, &connector->modes, head) {
1149 drm_crtc_convert_to_umode(&u_mode, mode);
1150 if (copy_to_user(mode_ptr + copied,
1151 &u_mode, sizeof(u_mode))) {
1158 out_resp->count_modes = mode_count;
1160 if ((out_resp->count_props >= props_count) && props_count) {
1162 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1163 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1164 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1165 if (connector->property_ids[i] != 0) {
1166 if (put_user(connector->property_ids[i], prop_ptr + copied)) {
1171 if (put_user(connector->property_values[i], prop_values + copied)) {
1179 out_resp->count_props = props_count;
1181 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1183 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1184 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1185 if (connector->encoder_ids[i] != 0) {
1186 if (put_user(connector->encoder_ids[i], encoder_ptr + copied)) {
1194 out_resp->count_encoders = encoders_count;
1197 mutex_unlock(&dev->mode_config.mutex);
1201 int drm_mode_getencoder(struct drm_device *dev,
1202 void *data, struct drm_file *file_priv)
1204 struct drm_mode_get_encoder *enc_resp = data;
1205 struct drm_mode_object *obj;
1206 struct drm_encoder *encoder;
1209 mutex_lock(&dev->mode_config.mutex);
1210 obj = drm_mode_object_find(dev, enc_resp->encoder_id, DRM_MODE_OBJECT_ENCODER);
1215 encoder = obj_to_encoder(obj);
1218 enc_resp->crtc_id = encoder->crtc->base.id;
1220 enc_resp->crtc_id = 0;
1221 enc_resp->encoder_type = encoder->encoder_type;
1222 enc_resp->encoder_id = encoder->base.id;
1223 enc_resp->possible_crtcs = encoder->possible_crtcs;
1224 enc_resp->possible_clones = encoder->possible_clones;
1227 mutex_unlock(&dev->mode_config.mutex);
1232 * drm_mode_setcrtc - set CRTC configuration
1233 * @inode: inode from the ioctl
1234 * @filp: file * from the ioctl
1235 * @cmd: cmd from ioctl
1236 * @arg: arg from ioctl
1241 * Build a new CRTC configuration based on user request.
1243 * Called by the user via ioctl.
1246 * Zero on success, errno on failure.
1248 int drm_mode_setcrtc(struct drm_device *dev,
1249 void *data, struct drm_file *file_priv)
1251 struct drm_mode_crtc *crtc_req = data;
1252 struct drm_mode_object *obj;
1253 struct drm_crtc *crtc, *crtcfb;
1254 struct drm_connector **connector_set = NULL, *connector;
1255 struct drm_framebuffer *fb = NULL;
1256 struct drm_display_mode *mode = NULL;
1257 struct drm_mode_set set;
1258 uint32_t __user *set_connectors_ptr;
1262 mutex_lock(&dev->mode_config.mutex);
1263 obj = drm_mode_object_find(dev, crtc_req->crtc_id, DRM_MODE_OBJECT_CRTC);
1265 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1269 crtc = obj_to_crtc(obj);
1271 if (crtc_req->mode_valid) {
1272 /* If we have a mode we need a framebuffer. */
1273 /* If we pass -1, set the mode with the currently bound fb */
1274 if (crtc_req->fb_id == -1) {
1275 list_for_each_entry(crtcfb, &dev->mode_config.crtc_list, head) {
1276 if (crtcfb == crtc) {
1277 DRM_DEBUG("Using current fb for setmode\n");
1282 obj = drm_mode_object_find(dev, crtc_req->fb_id, DRM_MODE_OBJECT_FB);
1284 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1288 fb = obj_to_fb(obj);
1291 mode = drm_mode_create(dev);
1292 drm_crtc_convert_umode(mode, &crtc_req->mode);
1293 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1296 if (crtc_req->count_connectors == 0 && mode) {
1297 DRM_DEBUG("Count connectors is 0 but mode set\n");
1302 if (crtc_req->count_connectors > 0 && !mode && !fb) {
1303 DRM_DEBUG("Count connectors is %d but no mode or fb set\n", crtc_req->count_connectors);
1308 if (crtc_req->count_connectors > 0) {
1310 /* Maybe we should check that count_connectors is a sensible value. */
1311 connector_set = kmalloc(crtc_req->count_connectors *
1312 sizeof(struct drm_connector *), GFP_KERNEL);
1313 if (!connector_set) {
1318 for (i = 0; i < crtc_req->count_connectors; i++) {
1319 set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1320 if (get_user(out_id, &set_connectors_ptr[i])) {
1325 obj = drm_mode_object_find(dev, out_id, DRM_MODE_OBJECT_CONNECTOR);
1327 DRM_DEBUG("Connector id %d unknown\n", out_id);
1331 connector = obj_to_connector(obj);
1333 connector_set[i] = connector;
1338 set.x = crtc_req->x;
1339 set.y = crtc_req->y;
1341 set.connectors = connector_set;
1342 set.num_connectors = crtc_req->count_connectors;
1344 ret = crtc->funcs->set_config(&set);
1347 kfree(connector_set);
1348 mutex_unlock(&dev->mode_config.mutex);
1352 int drm_mode_cursor_ioctl(struct drm_device *dev,
1353 void *data, struct drm_file *file_priv)
1355 struct drm_mode_cursor *req = data;
1356 struct drm_mode_object *obj;
1357 struct drm_crtc *crtc;
1363 DRM_ERROR("no operation set\n");
1367 mutex_lock(&dev->mode_config.mutex);
1368 obj = drm_mode_object_find(dev, req->crtc, DRM_MODE_OBJECT_CRTC);
1370 DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc);
1374 crtc = obj_to_crtc(obj);
1376 if (req->flags & DRM_MODE_CURSOR_BO) {
1377 /* Turn of the cursor if handle is 0 */
1378 if (crtc->funcs->cursor_set) {
1379 ret = crtc->funcs->cursor_set(crtc, req->handle, req->width, req->height);
1381 DRM_ERROR("crtc does not support cursor\n");
1387 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1388 if (crtc->funcs->cursor_move) {
1389 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1391 DRM_ERROR("crtc does not support cursor\n");
1397 mutex_unlock(&dev->mode_config.mutex);
1402 * drm_mode_addfb - add an FB to the graphics configuration
1403 * @inode: inode from the ioctl
1404 * @filp: file * from the ioctl
1405 * @cmd: cmd from ioctl
1406 * @arg: arg from ioctl
1409 * Takes mode config lock.
1411 * Add a new FB to the specified CRTC, given a user request.
1413 * Called by the user via ioctl.
1416 * Zero on success, errno on failure.
1418 int drm_mode_addfb(struct drm_device *dev,
1419 void *data, struct drm_file *file_priv)
1421 struct drm_mode_fb_cmd *r = data;
1422 struct drm_mode_config *config = &dev->mode_config;
1423 struct drm_framebuffer *fb;
1426 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1427 DRM_ERROR("mode new framebuffer width not within limits\n");
1430 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1431 DRM_ERROR("mode new framebuffer height not within limits\n");
1435 mutex_lock(&dev->mode_config.mutex);
1437 /* TODO check buffer is sufficently large */
1438 /* TODO setup destructor callback */
1440 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1442 DRM_ERROR("could not create framebuffer\n");
1447 r->buffer_id = fb->base.id;
1448 list_add(&fb->filp_head, &file_priv->fbs);
1451 mutex_unlock(&dev->mode_config.mutex);
1456 * drm_mode_rmfb - remove an FB from the configuration
1457 * @inode: inode from the ioctl
1458 * @filp: file * from the ioctl
1459 * @cmd: cmd from ioctl
1460 * @arg: arg from ioctl
1463 * Takes mode config lock.
1465 * Remove the FB specified by the user.
1467 * Called by the user via ioctl.
1470 * Zero on success, errno on failure.
1472 int drm_mode_rmfb(struct drm_device *dev,
1473 void *data, struct drm_file *file_priv)
1475 struct drm_mode_object *obj;
1476 struct drm_framebuffer *fb = NULL;
1477 struct drm_framebuffer *fbl = NULL;
1478 uint32_t *id = data;
1482 mutex_lock(&dev->mode_config.mutex);
1483 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1484 /* TODO check that we realy get a framebuffer back. */
1486 DRM_ERROR("mode invalid framebuffer id\n");
1490 fb = obj_to_fb(obj);
1492 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1497 DRM_ERROR("tried to remove a fb that we didn't own\n");
1502 /* TODO release all crtc connected to the framebuffer */
1503 /* TODO unhock the destructor from the buffer object */
1505 list_del(&fb->filp_head);
1506 fb->funcs->destroy(fb);
1509 mutex_unlock(&dev->mode_config.mutex);
1514 * drm_mode_getfb - get FB info
1515 * @inode: inode from the ioctl
1516 * @filp: file * from the ioctl
1517 * @cmd: cmd from ioctl
1518 * @arg: arg from ioctl
1523 * Lookup the FB given its ID and return info about it.
1525 * Called by the user via ioctl.
1528 * Zero on success, errno on failure.
1530 int drm_mode_getfb(struct drm_device *dev,
1531 void *data, struct drm_file *file_priv)
1533 struct drm_mode_fb_cmd *r = data;
1534 struct drm_mode_object *obj;
1535 struct drm_framebuffer *fb;
1538 mutex_lock(&dev->mode_config.mutex);
1539 obj = drm_mode_object_find(dev, r->buffer_id, DRM_MODE_OBJECT_FB);
1541 DRM_ERROR("invalid framebuffer id\n");
1545 fb = obj_to_fb(obj);
1547 r->height = fb->height;
1548 r->width = fb->width;
1549 r->depth = fb->depth;
1550 r->bpp = fb->bits_per_pixel;
1551 r->handle = fb->mm_handle;
1552 r->pitch = fb->pitch;
1555 mutex_unlock(&dev->mode_config.mutex);
1560 * drm_fb_release - remove and free the FBs on this file
1561 * @filp: file * from the ioctl
1564 * Takes mode config lock.
1566 * Destroy all the FBs associated with @filp.
1568 * Called by the user via ioctl.
1571 * Zero on success, errno on failure.
1573 void drm_fb_release(struct file *filp)
1575 struct drm_file *priv = filp->private_data;
1576 struct drm_device *dev = priv->minor->dev;
1577 struct drm_framebuffer *fb, *tfb;
1579 mutex_lock(&dev->mode_config.mutex);
1580 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1581 list_del(&fb->filp_head);
1582 fb->funcs->destroy(fb);
1584 mutex_unlock(&dev->mode_config.mutex);
1591 static int drm_mode_attachmode(struct drm_device *dev,
1592 struct drm_connector *connector,
1593 struct drm_display_mode *mode)
1597 list_add_tail(&mode->head, &connector->user_modes);
1601 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1602 struct drm_display_mode *mode)
1604 struct drm_connector *connector;
1606 struct drm_display_mode *dup_mode;
1608 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1609 if (!connector->encoder)
1611 if (connector->encoder->crtc == crtc) {
1613 dup_mode = drm_mode_duplicate(dev, mode);
1616 ret = drm_mode_attachmode(dev, connector, dup_mode);
1624 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1626 static int drm_mode_detachmode(struct drm_device *dev,
1627 struct drm_connector *connector,
1628 struct drm_display_mode *mode)
1632 struct drm_display_mode *match_mode, *t;
1634 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1635 if (drm_mode_equal(match_mode, mode)) {
1636 list_del(&match_mode->head);
1637 drm_mode_destroy(dev, match_mode);
1649 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1651 struct drm_connector *connector;
1653 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1654 drm_mode_detachmode(dev, connector, mode);
1658 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1661 * drm_fb_attachmode - Attach a user mode to an connector
1662 * @inode: inode from the ioctl
1663 * @filp: file * from the ioctl
1664 * @cmd: cmd from ioctl
1665 * @arg: arg from ioctl
1667 * This attaches a user specified mode to an connector.
1668 * Called by the user via ioctl.
1671 * Zero on success, errno on failure.
1673 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1674 void *data, struct drm_file *file_priv)
1676 struct drm_mode_mode_cmd *mode_cmd = data;
1677 struct drm_connector *connector;
1678 struct drm_display_mode *mode;
1679 struct drm_mode_object *obj;
1680 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1683 mutex_lock(&dev->mode_config.mutex);
1685 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1690 connector = obj_to_connector(obj);
1692 mode = drm_mode_create(dev);
1698 drm_crtc_convert_umode(mode, umode);
1700 ret = drm_mode_attachmode(dev, connector, mode);
1702 mutex_unlock(&dev->mode_config.mutex);
1708 * drm_fb_detachmode - Detach a user specified mode from an connector
1709 * @inode: inode from the ioctl
1710 * @filp: file * from the ioctl
1711 * @cmd: cmd from ioctl
1712 * @arg: arg from ioctl
1714 * Called by the user via ioctl.
1717 * Zero on success, errno on failure.
1719 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1720 void *data, struct drm_file *file_priv)
1722 struct drm_mode_object *obj;
1723 struct drm_mode_mode_cmd *mode_cmd = data;
1724 struct drm_connector *connector;
1725 struct drm_display_mode mode;
1726 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1729 mutex_lock(&dev->mode_config.mutex);
1731 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1736 connector = obj_to_connector(obj);
1738 drm_crtc_convert_umode(&mode, umode);
1739 ret = drm_mode_detachmode(dev, connector, &mode);
1741 mutex_unlock(&dev->mode_config.mutex);
1745 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1746 const char *name, int num_values)
1748 struct drm_property *property = NULL;
1750 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
1755 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1756 if (!property->values)
1760 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
1761 property->flags = flags;
1762 property->num_values = num_values;
1763 INIT_LIST_HEAD(&property->enum_blob_list);
1766 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1768 list_add_tail(&property->head, &dev->mode_config.property_list);
1774 EXPORT_SYMBOL(drm_property_create);
1776 int drm_property_add_enum(struct drm_property *property, int index,
1777 uint64_t value, const char *name)
1779 struct drm_property_enum *prop_enum;
1781 if (!(property->flags & DRM_MODE_PROP_ENUM))
1784 if (!list_empty(&property->enum_blob_list)) {
1785 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1786 if (prop_enum->value == value) {
1787 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1788 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1794 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1798 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1799 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1800 prop_enum->value = value;
1802 property->values[index] = value;
1803 list_add_tail(&prop_enum->head, &property->enum_blob_list);
1806 EXPORT_SYMBOL(drm_property_add_enum);
1808 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1810 struct drm_property_enum *prop_enum, *pt;
1812 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
1813 list_del(&prop_enum->head);
1817 if (property->num_values)
1818 kfree(property->values);
1819 drm_mode_object_put(dev, &property->base);
1820 list_del(&property->head);
1823 EXPORT_SYMBOL(drm_property_destroy);
1825 int drm_connector_attach_property(struct drm_connector *connector,
1826 struct drm_property *property, uint64_t init_val)
1830 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1831 if (connector->property_ids[i] == 0) {
1832 connector->property_ids[i] = property->base.id;
1833 connector->property_values[i] = init_val;
1838 if (i == DRM_CONNECTOR_MAX_PROPERTY)
1842 EXPORT_SYMBOL(drm_connector_attach_property);
1844 int drm_connector_property_set_value(struct drm_connector *connector,
1845 struct drm_property *property, uint64_t value)
1849 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1850 if (connector->property_ids[i] == property->base.id) {
1851 connector->property_values[i] = value;
1856 if (i == DRM_CONNECTOR_MAX_PROPERTY)
1860 EXPORT_SYMBOL(drm_connector_property_set_value);
1862 int drm_connector_property_get_value(struct drm_connector *connector,
1863 struct drm_property *property, uint64_t *val)
1867 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1868 if (connector->property_ids[i] == property->base.id) {
1869 *val = connector->property_values[i];
1874 if (i == DRM_CONNECTOR_MAX_PROPERTY)
1878 EXPORT_SYMBOL(drm_connector_property_get_value);
1880 int drm_mode_getproperty_ioctl(struct drm_device *dev,
1881 void *data, struct drm_file *file_priv)
1883 struct drm_mode_object *obj;
1884 struct drm_mode_get_property *out_resp = data;
1885 struct drm_property *property;
1888 int value_count = 0;
1891 struct drm_property_enum *prop_enum;
1892 struct drm_mode_property_enum __user *enum_ptr;
1893 struct drm_property_blob *prop_blob;
1894 uint32_t *blob_id_ptr;
1895 uint64_t __user *values_ptr;
1896 uint32_t __user *blob_length_ptr;
1898 mutex_lock(&dev->mode_config.mutex);
1899 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
1904 property = obj_to_property(obj);
1906 if (property->flags & DRM_MODE_PROP_ENUM) {
1907 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
1909 } else if (property->flags & DRM_MODE_PROP_BLOB) {
1910 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
1914 value_count = property->num_values;
1916 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
1917 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
1918 out_resp->flags = property->flags;
1920 if ((out_resp->count_values >= value_count) && value_count) {
1921 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
1922 for (i = 0; i < value_count; i++) {
1923 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
1929 out_resp->count_values = value_count;
1931 if (property->flags & DRM_MODE_PROP_ENUM) {
1932 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
1934 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
1935 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1937 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
1942 if (copy_to_user(&enum_ptr[copied].name,
1943 &prop_enum->name, DRM_PROP_NAME_LEN)) {
1950 out_resp->count_enum_blobs = enum_count;
1953 if (property->flags & DRM_MODE_PROP_BLOB) {
1954 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
1956 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
1957 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
1959 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
1960 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
1965 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
1973 out_resp->count_enum_blobs = blob_count;
1976 mutex_unlock(&dev->mode_config.mutex);
1980 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
1983 struct drm_property_blob *blob;
1985 if (!length || !data)
1988 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
1992 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
1993 blob->length = length;
1995 memcpy(blob->data, data, length);
1997 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
1999 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2003 static void drm_property_destroy_blob(struct drm_device *dev,
2004 struct drm_property_blob *blob)
2006 drm_mode_object_put(dev, &blob->base);
2007 list_del(&blob->head);
2011 int drm_mode_getblob_ioctl(struct drm_device *dev,
2012 void *data, struct drm_file *file_priv)
2014 struct drm_mode_object *obj;
2015 struct drm_mode_get_blob *out_resp = data;
2016 struct drm_property_blob *blob;
2020 mutex_lock(&dev->mode_config.mutex);
2021 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2026 blob = obj_to_blob(obj);
2028 if (out_resp->length == blob->length) {
2029 blob_ptr = (void *)(unsigned long)out_resp->data;
2030 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2035 out_resp->length = blob->length;
2038 mutex_unlock(&dev->mode_config.mutex);
2042 int drm_mode_connector_update_edid_property(struct drm_connector *connector, struct edid *edid)
2044 struct drm_device *dev = connector->dev;
2046 if (connector->edid_blob_ptr)
2047 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2049 connector->edid_blob_ptr = drm_property_create_blob(connector->dev, 128, edid);
2051 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, connector->edid_blob_ptr->base.id);
2054 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2056 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2057 void *data, struct drm_file *file_priv)
2059 struct drm_mode_connector_set_property *out_resp = data;
2060 struct drm_mode_object *obj;
2061 struct drm_property *property;
2062 struct drm_connector *connector;
2066 mutex_lock(&dev->mode_config.mutex);
2068 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2072 connector = obj_to_connector(obj);
2074 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2075 if (connector->property_ids[i] == out_resp->prop_id)
2079 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2083 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2087 property = obj_to_property(obj);
2089 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2092 if (property->flags & DRM_MODE_PROP_RANGE) {
2093 if (out_resp->value < property->values[0])
2096 if (out_resp->value > property->values[1])
2100 for (i = 0; i < property->num_values; i++) {
2101 if (property->values[i] == out_resp->value) {
2111 /* store the property value */
2112 drm_connector_property_set_value(connector, property, out_resp->value);
2114 if (connector->funcs->set_property)
2115 ret = connector->funcs->set_property(connector, property, out_resp->value);
2118 mutex_unlock(&dev->mode_config.mutex);
2123 int drm_mode_replacefb(struct drm_device *dev,
2124 void *data, struct drm_file *file_priv)
2126 struct drm_mode_fb_cmd *r = data;
2127 struct drm_mode_object *obj;
2128 struct drm_framebuffer *fb;
2130 struct drm_framebuffer *fbl = NULL;
2133 /* right replace the current bo attached to this fb with a new bo */
2134 mutex_lock(&dev->mode_config.mutex);
2135 obj = drm_mode_object_find(dev, r->buffer_id, DRM_MODE_OBJECT_FB);
2140 fb = obj_to_fb(obj);
2142 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2147 DRM_ERROR("tried to replace an fb we didn't own\n");
2152 if (dev->mode_config.funcs->resize_fb)
2153 ret = dev->mode_config.funcs->resize_fb(dev, file_priv, fb, r);
2157 mutex_unlock(&dev->mode_config.mutex);
2162 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2163 struct drm_encoder *encoder)
2167 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2168 if (connector->encoder_ids[i] == 0) {
2169 connector->encoder_ids[i] = encoder->base.id;
2175 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2177 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2178 struct drm_encoder *encoder)
2181 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2182 if (connector->encoder_ids[i] == encoder->base.id) {
2183 connector->encoder_ids[i] = 0;
2184 if (connector->encoder == encoder)
2185 connector->encoder = NULL;
2190 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2192 bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2195 crtc->gamma_size = gamma_size;
2197 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2198 if (!crtc->gamma_store) {
2199 crtc->gamma_size = 0;
2205 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2207 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2208 void *data, struct drm_file *file_priv)
2210 struct drm_mode_crtc_lut *crtc_lut = data;
2211 struct drm_mode_object *obj;
2212 struct drm_crtc *crtc;
2213 void *r_base, *g_base, *b_base;
2217 mutex_lock(&dev->mode_config.mutex);
2218 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2223 crtc = obj_to_crtc(obj);
2225 /* memcpy into gamma store */
2226 if (crtc_lut->gamma_size != crtc->gamma_size) {
2231 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2232 r_base = crtc->gamma_store;
2233 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2238 g_base = r_base + size;
2239 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2244 b_base = g_base + size;
2245 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2250 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
2253 mutex_unlock(&dev->mode_config.mutex);
2258 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2259 void *data, struct drm_file *file_priv)
2261 struct drm_mode_crtc_lut *crtc_lut = data;
2262 struct drm_mode_object *obj;
2263 struct drm_crtc *crtc;
2264 void *r_base, *g_base, *b_base;
2268 mutex_lock(&dev->mode_config.mutex);
2269 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2274 crtc = obj_to_crtc(obj);
2276 /* memcpy into gamma store */
2277 if (crtc_lut->gamma_size != crtc->gamma_size) {
2282 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2283 r_base = crtc->gamma_store;
2284 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2289 g_base = r_base + size;
2290 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2295 b_base = g_base + size;
2296 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2301 mutex_unlock(&dev->mode_config.mutex);