drm: split crtc/fb helpers into a separate module
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpu / drm / drm_crtc.c
blobba728ad77f2a6611409a5f90c0f639bfa10aef9b
1 /*
2 * Copyright (c) 2006-2008 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
24 * OF THIS SOFTWARE.
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/list.h>
33 #include "drm.h"
34 #include "drmP.h"
35 #include "drm_crtc.h"
37 struct drm_prop_enum_list {
38 int type;
39 char *name;
42 /* Avoid boilerplate. I'm tired of typing. */
43 #define DRM_ENUM_NAME_FN(fnname, list) \
44 char *fnname(int val) \
45 { \
46 int i; \
47 for (i = 0; i < ARRAY_SIZE(list); i++) { \
48 if (list[i].type == val) \
49 return list[i].name; \
50 } \
51 return "(unknown)"; \
55 * Global properties
57 static struct drm_prop_enum_list drm_dpms_enum_list[] =
58 { { DRM_MODE_DPMS_ON, "On" },
59 { DRM_MODE_DPMS_STANDBY, "Standby" },
60 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
61 { DRM_MODE_DPMS_OFF, "Off" }
64 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
67 * Optional properties
69 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
71 { DRM_MODE_SCALE_NONE, "None" },
72 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
73 { DRM_MODE_SCALE_CENTER, "Center" },
74 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
77 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
79 { DRM_MODE_DITHERING_OFF, "Off" },
80 { DRM_MODE_DITHERING_ON, "On" },
84 * Non-global properties, but "required" for certain connectors.
86 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
88 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
89 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
90 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
93 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
95 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
97 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
98 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
99 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
102 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
103 drm_dvi_i_subconnector_enum_list)
105 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
107 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
108 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
109 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
110 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
111 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
114 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
116 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
118 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
125 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
126 drm_tv_subconnector_enum_list)
128 struct drm_conn_prop_enum_list {
129 int type;
130 char *name;
131 int count;
135 * Connector and encoder types.
137 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
138 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
139 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
140 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
141 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
142 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
143 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
144 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
145 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
146 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
147 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN", 0 },
148 { DRM_MODE_CONNECTOR_DisplayPort, "DisplayPort", 0 },
149 { DRM_MODE_CONNECTOR_HDMIA, "HDMI Type A", 0 },
150 { DRM_MODE_CONNECTOR_HDMIB, "HDMI Type B", 0 },
151 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
154 static struct drm_prop_enum_list drm_encoder_enum_list[] =
155 { { DRM_MODE_ENCODER_NONE, "None" },
156 { DRM_MODE_ENCODER_DAC, "DAC" },
157 { DRM_MODE_ENCODER_TMDS, "TMDS" },
158 { DRM_MODE_ENCODER_LVDS, "LVDS" },
159 { DRM_MODE_ENCODER_TVDAC, "TV" },
162 char *drm_get_encoder_name(struct drm_encoder *encoder)
164 static char buf[32];
166 snprintf(buf, 32, "%s-%d",
167 drm_encoder_enum_list[encoder->encoder_type].name,
168 encoder->base.id);
169 return buf;
171 EXPORT_SYMBOL(drm_get_encoder_name);
173 char *drm_get_connector_name(struct drm_connector *connector)
175 static char buf[32];
177 snprintf(buf, 32, "%s-%d",
178 drm_connector_enum_list[connector->connector_type].name,
179 connector->connector_type_id);
180 return buf;
182 EXPORT_SYMBOL(drm_get_connector_name);
184 char *drm_get_connector_status_name(enum drm_connector_status status)
186 if (status == connector_status_connected)
187 return "connected";
188 else if (status == connector_status_disconnected)
189 return "disconnected";
190 else
191 return "unknown";
195 * drm_mode_object_get - allocate a new identifier
196 * @dev: DRM device
197 * @ptr: object pointer, used to generate unique ID
198 * @type: object type
200 * LOCKING:
202 * Create a unique identifier based on @ptr in @dev's identifier space. Used
203 * for tracking modes, CRTCs and connectors.
205 * RETURNS:
206 * New unique (relative to other objects in @dev) integer identifier for the
207 * object.
209 static int drm_mode_object_get(struct drm_device *dev,
210 struct drm_mode_object *obj, uint32_t obj_type)
212 int new_id = 0;
213 int ret;
215 again:
216 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
217 DRM_ERROR("Ran out memory getting a mode number\n");
218 return -EINVAL;
221 mutex_lock(&dev->mode_config.idr_mutex);
222 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
223 mutex_unlock(&dev->mode_config.idr_mutex);
224 if (ret == -EAGAIN)
225 goto again;
227 obj->id = new_id;
228 obj->type = obj_type;
229 return 0;
233 * drm_mode_object_put - free an identifer
234 * @dev: DRM device
235 * @id: ID to free
237 * LOCKING:
238 * Caller must hold DRM mode_config lock.
240 * Free @id from @dev's unique identifier pool.
242 static void drm_mode_object_put(struct drm_device *dev,
243 struct drm_mode_object *object)
245 mutex_lock(&dev->mode_config.idr_mutex);
246 idr_remove(&dev->mode_config.crtc_idr, object->id);
247 mutex_unlock(&dev->mode_config.idr_mutex);
250 void *drm_mode_object_find(struct drm_device *dev, uint32_t id, uint32_t type)
252 struct drm_mode_object *obj = NULL;
254 mutex_lock(&dev->mode_config.idr_mutex);
255 obj = idr_find(&dev->mode_config.crtc_idr, id);
256 if (!obj || (obj->type != type) || (obj->id != id))
257 obj = NULL;
258 mutex_unlock(&dev->mode_config.idr_mutex);
260 return obj;
262 EXPORT_SYMBOL(drm_mode_object_find);
265 * drm_framebuffer_init - initialize a framebuffer
266 * @dev: DRM device
268 * LOCKING:
269 * Caller must hold mode config lock.
271 * Allocates an ID for the framebuffer's parent mode object, sets its mode
272 * functions & device file and adds it to the master fd list.
274 * RETURNS:
275 * Zero on success, error code on falure.
277 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
278 const struct drm_framebuffer_funcs *funcs)
280 int ret;
282 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
283 if (ret) {
284 return ret;
287 fb->dev = dev;
288 fb->funcs = funcs;
289 dev->mode_config.num_fb++;
290 list_add(&fb->head, &dev->mode_config.fb_list);
292 return 0;
294 EXPORT_SYMBOL(drm_framebuffer_init);
297 * drm_framebuffer_cleanup - remove a framebuffer object
298 * @fb: framebuffer to remove
300 * LOCKING:
301 * Caller must hold mode config lock.
303 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
304 * it, setting it to NULL.
306 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
308 struct drm_device *dev = fb->dev;
309 struct drm_crtc *crtc;
310 struct drm_mode_set set;
311 int ret;
313 /* remove from any CRTC */
314 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
315 if (crtc->fb == fb) {
316 /* should turn off the crtc */
317 memset(&set, 0, sizeof(struct drm_mode_set));
318 set.crtc = crtc;
319 set.fb = NULL;
320 ret = crtc->funcs->set_config(&set);
321 if (ret)
322 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
326 drm_mode_object_put(dev, &fb->base);
327 list_del(&fb->head);
328 dev->mode_config.num_fb--;
330 EXPORT_SYMBOL(drm_framebuffer_cleanup);
333 * drm_crtc_init - Initialise a new CRTC object
334 * @dev: DRM device
335 * @crtc: CRTC object to init
336 * @funcs: callbacks for the new CRTC
338 * LOCKING:
339 * Caller must hold mode config lock.
341 * Inits a new object created as base part of an driver crtc object.
343 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
344 const struct drm_crtc_funcs *funcs)
346 crtc->dev = dev;
347 crtc->funcs = funcs;
349 mutex_lock(&dev->mode_config.mutex);
350 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
352 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
353 dev->mode_config.num_crtc++;
354 mutex_unlock(&dev->mode_config.mutex);
356 EXPORT_SYMBOL(drm_crtc_init);
359 * drm_crtc_cleanup - Cleans up the core crtc usage.
360 * @crtc: CRTC to cleanup
362 * LOCKING:
363 * Caller must hold mode config lock.
365 * Cleanup @crtc. Removes from drm modesetting space
366 * does NOT free object, caller does that.
368 void drm_crtc_cleanup(struct drm_crtc *crtc)
370 struct drm_device *dev = crtc->dev;
372 if (crtc->gamma_store) {
373 kfree(crtc->gamma_store);
374 crtc->gamma_store = NULL;
377 drm_mode_object_put(dev, &crtc->base);
378 list_del(&crtc->head);
379 dev->mode_config.num_crtc--;
381 EXPORT_SYMBOL(drm_crtc_cleanup);
384 * drm_mode_probed_add - add a mode to a connector's probed mode list
385 * @connector: connector the new mode
386 * @mode: mode data
388 * LOCKING:
389 * Caller must hold mode config lock.
391 * Add @mode to @connector's mode list for later use.
393 void drm_mode_probed_add(struct drm_connector *connector,
394 struct drm_display_mode *mode)
396 list_add(&mode->head, &connector->probed_modes);
398 EXPORT_SYMBOL(drm_mode_probed_add);
401 * drm_mode_remove - remove and free a mode
402 * @connector: connector list to modify
403 * @mode: mode to remove
405 * LOCKING:
406 * Caller must hold mode config lock.
408 * Remove @mode from @connector's mode list, then free it.
410 void drm_mode_remove(struct drm_connector *connector,
411 struct drm_display_mode *mode)
413 list_del(&mode->head);
414 kfree(mode);
416 EXPORT_SYMBOL(drm_mode_remove);
419 * drm_connector_init - Init a preallocated connector
420 * @dev: DRM device
421 * @connector: the connector to init
422 * @funcs: callbacks for this connector
423 * @name: user visible name of the connector
425 * LOCKING:
426 * Caller must hold @dev's mode_config lock.
428 * Initialises a preallocated connector. Connectors should be
429 * subclassed as part of driver connector objects.
431 void drm_connector_init(struct drm_device *dev,
432 struct drm_connector *connector,
433 const struct drm_connector_funcs *funcs,
434 int connector_type)
436 mutex_lock(&dev->mode_config.mutex);
438 connector->dev = dev;
439 connector->funcs = funcs;
440 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
441 connector->connector_type = connector_type;
442 connector->connector_type_id =
443 ++drm_connector_enum_list[connector_type].count; /* TODO */
444 INIT_LIST_HEAD(&connector->user_modes);
445 INIT_LIST_HEAD(&connector->probed_modes);
446 INIT_LIST_HEAD(&connector->modes);
447 connector->edid_blob_ptr = NULL;
449 list_add_tail(&connector->head, &dev->mode_config.connector_list);
450 dev->mode_config.num_connector++;
452 drm_connector_attach_property(connector,
453 dev->mode_config.edid_property, 0);
455 drm_connector_attach_property(connector,
456 dev->mode_config.dpms_property, 0);
458 mutex_unlock(&dev->mode_config.mutex);
460 EXPORT_SYMBOL(drm_connector_init);
463 * drm_connector_cleanup - cleans up an initialised connector
464 * @connector: connector to cleanup
466 * LOCKING:
467 * Caller must hold @dev's mode_config lock.
469 * Cleans up the connector but doesn't free the object.
471 void drm_connector_cleanup(struct drm_connector *connector)
473 struct drm_device *dev = connector->dev;
474 struct drm_display_mode *mode, *t;
476 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
477 drm_mode_remove(connector, mode);
479 list_for_each_entry_safe(mode, t, &connector->modes, head)
480 drm_mode_remove(connector, mode);
482 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
483 drm_mode_remove(connector, mode);
485 mutex_lock(&dev->mode_config.mutex);
486 drm_mode_object_put(dev, &connector->base);
487 list_del(&connector->head);
488 mutex_unlock(&dev->mode_config.mutex);
490 EXPORT_SYMBOL(drm_connector_cleanup);
492 void drm_encoder_init(struct drm_device *dev,
493 struct drm_encoder *encoder,
494 const struct drm_encoder_funcs *funcs,
495 int encoder_type)
497 mutex_lock(&dev->mode_config.mutex);
499 encoder->dev = dev;
501 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
502 encoder->encoder_type = encoder_type;
503 encoder->funcs = funcs;
505 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
506 dev->mode_config.num_encoder++;
508 mutex_unlock(&dev->mode_config.mutex);
510 EXPORT_SYMBOL(drm_encoder_init);
512 void drm_encoder_cleanup(struct drm_encoder *encoder)
514 struct drm_device *dev = encoder->dev;
515 mutex_lock(&dev->mode_config.mutex);
516 drm_mode_object_put(dev, &encoder->base);
517 list_del(&encoder->head);
518 mutex_unlock(&dev->mode_config.mutex);
520 EXPORT_SYMBOL(drm_encoder_cleanup);
523 * drm_mode_create - create a new display mode
524 * @dev: DRM device
526 * LOCKING:
527 * Caller must hold DRM mode_config lock.
529 * Create a new drm_display_mode, give it an ID, and return it.
531 * RETURNS:
532 * Pointer to new mode on success, NULL on error.
534 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
536 struct drm_display_mode *nmode;
538 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
539 if (!nmode)
540 return NULL;
542 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
543 return nmode;
545 EXPORT_SYMBOL(drm_mode_create);
548 * drm_mode_destroy - remove a mode
549 * @dev: DRM device
550 * @mode: mode to remove
552 * LOCKING:
553 * Caller must hold mode config lock.
555 * Free @mode's unique identifier, then free it.
557 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
559 drm_mode_object_put(dev, &mode->base);
561 kfree(mode);
563 EXPORT_SYMBOL(drm_mode_destroy);
565 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
567 struct drm_property *edid;
568 struct drm_property *dpms;
569 int i;
572 * Standard properties (apply to all connectors)
574 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
575 DRM_MODE_PROP_IMMUTABLE,
576 "EDID", 0);
577 dev->mode_config.edid_property = edid;
579 dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
580 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
581 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
582 drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
583 drm_dpms_enum_list[i].name);
584 dev->mode_config.dpms_property = dpms;
586 return 0;
590 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
591 * @dev: DRM device
593 * Called by a driver the first time a DVI-I connector is made.
595 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
597 struct drm_property *dvi_i_selector;
598 struct drm_property *dvi_i_subconnector;
599 int i;
601 if (dev->mode_config.dvi_i_select_subconnector_property)
602 return 0;
604 dvi_i_selector =
605 drm_property_create(dev, DRM_MODE_PROP_ENUM,
606 "select subconnector",
607 ARRAY_SIZE(drm_dvi_i_select_enum_list));
608 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
609 drm_property_add_enum(dvi_i_selector, i,
610 drm_dvi_i_select_enum_list[i].type,
611 drm_dvi_i_select_enum_list[i].name);
612 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
614 dvi_i_subconnector =
615 drm_property_create(dev, DRM_MODE_PROP_ENUM |
616 DRM_MODE_PROP_IMMUTABLE,
617 "subconnector",
618 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
619 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
620 drm_property_add_enum(dvi_i_subconnector, i,
621 drm_dvi_i_subconnector_enum_list[i].type,
622 drm_dvi_i_subconnector_enum_list[i].name);
623 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
625 return 0;
627 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
630 * drm_create_tv_properties - create TV specific connector properties
631 * @dev: DRM device
632 * @num_modes: number of different TV formats (modes) supported
633 * @modes: array of pointers to strings containing name of each format
635 * Called by a driver's TV initialization routine, this function creates
636 * the TV specific connector properties for a given device. Caller is
637 * responsible for allocating a list of format names and passing them to
638 * this routine.
640 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
641 char *modes[])
643 struct drm_property *tv_selector;
644 struct drm_property *tv_subconnector;
645 int i;
647 if (dev->mode_config.tv_select_subconnector_property)
648 return 0;
651 * Basic connector properties
653 tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
654 "select subconnector",
655 ARRAY_SIZE(drm_tv_select_enum_list));
656 for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
657 drm_property_add_enum(tv_selector, i,
658 drm_tv_select_enum_list[i].type,
659 drm_tv_select_enum_list[i].name);
660 dev->mode_config.tv_select_subconnector_property = tv_selector;
662 tv_subconnector =
663 drm_property_create(dev, DRM_MODE_PROP_ENUM |
664 DRM_MODE_PROP_IMMUTABLE, "subconnector",
665 ARRAY_SIZE(drm_tv_subconnector_enum_list));
666 for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
667 drm_property_add_enum(tv_subconnector, i,
668 drm_tv_subconnector_enum_list[i].type,
669 drm_tv_subconnector_enum_list[i].name);
670 dev->mode_config.tv_subconnector_property = tv_subconnector;
673 * Other, TV specific properties: margins & TV modes.
675 dev->mode_config.tv_left_margin_property =
676 drm_property_create(dev, DRM_MODE_PROP_RANGE,
677 "left margin", 2);
678 dev->mode_config.tv_left_margin_property->values[0] = 0;
679 dev->mode_config.tv_left_margin_property->values[1] = 100;
681 dev->mode_config.tv_right_margin_property =
682 drm_property_create(dev, DRM_MODE_PROP_RANGE,
683 "right margin", 2);
684 dev->mode_config.tv_right_margin_property->values[0] = 0;
685 dev->mode_config.tv_right_margin_property->values[1] = 100;
687 dev->mode_config.tv_top_margin_property =
688 drm_property_create(dev, DRM_MODE_PROP_RANGE,
689 "top margin", 2);
690 dev->mode_config.tv_top_margin_property->values[0] = 0;
691 dev->mode_config.tv_top_margin_property->values[1] = 100;
693 dev->mode_config.tv_bottom_margin_property =
694 drm_property_create(dev, DRM_MODE_PROP_RANGE,
695 "bottom margin", 2);
696 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
697 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
699 dev->mode_config.tv_mode_property =
700 drm_property_create(dev, DRM_MODE_PROP_ENUM,
701 "mode", num_modes);
702 for (i = 0; i < num_modes; i++)
703 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
704 i, modes[i]);
706 dev->mode_config.tv_brightness_property =
707 drm_property_create(dev, DRM_MODE_PROP_RANGE,
708 "brightness", 2);
709 dev->mode_config.tv_brightness_property->values[0] = 0;
710 dev->mode_config.tv_brightness_property->values[1] = 100;
712 dev->mode_config.tv_contrast_property =
713 drm_property_create(dev, DRM_MODE_PROP_RANGE,
714 "contrast", 2);
715 dev->mode_config.tv_contrast_property->values[0] = 0;
716 dev->mode_config.tv_contrast_property->values[1] = 100;
718 dev->mode_config.tv_flicker_reduction_property =
719 drm_property_create(dev, DRM_MODE_PROP_RANGE,
720 "flicker reduction", 2);
721 dev->mode_config.tv_flicker_reduction_property->values[0] = 0;
722 dev->mode_config.tv_flicker_reduction_property->values[1] = 100;
724 dev->mode_config.tv_overscan_property =
725 drm_property_create(dev, DRM_MODE_PROP_RANGE,
726 "overscan", 2);
727 dev->mode_config.tv_overscan_property->values[0] = 0;
728 dev->mode_config.tv_overscan_property->values[1] = 100;
730 dev->mode_config.tv_saturation_property =
731 drm_property_create(dev, DRM_MODE_PROP_RANGE,
732 "saturation", 2);
733 dev->mode_config.tv_saturation_property->values[0] = 0;
734 dev->mode_config.tv_saturation_property->values[1] = 100;
736 dev->mode_config.tv_hue_property =
737 drm_property_create(dev, DRM_MODE_PROP_RANGE,
738 "hue", 2);
739 dev->mode_config.tv_hue_property->values[0] = 0;
740 dev->mode_config.tv_hue_property->values[1] = 100;
742 return 0;
744 EXPORT_SYMBOL(drm_mode_create_tv_properties);
747 * drm_mode_create_scaling_mode_property - create scaling mode property
748 * @dev: DRM device
750 * Called by a driver the first time it's needed, must be attached to desired
751 * connectors.
753 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
755 struct drm_property *scaling_mode;
756 int i;
758 if (dev->mode_config.scaling_mode_property)
759 return 0;
761 scaling_mode =
762 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
763 ARRAY_SIZE(drm_scaling_mode_enum_list));
764 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
765 drm_property_add_enum(scaling_mode, i,
766 drm_scaling_mode_enum_list[i].type,
767 drm_scaling_mode_enum_list[i].name);
769 dev->mode_config.scaling_mode_property = scaling_mode;
771 return 0;
773 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
776 * drm_mode_create_dithering_property - create dithering property
777 * @dev: DRM device
779 * Called by a driver the first time it's needed, must be attached to desired
780 * connectors.
782 int drm_mode_create_dithering_property(struct drm_device *dev)
784 struct drm_property *dithering_mode;
785 int i;
787 if (dev->mode_config.dithering_mode_property)
788 return 0;
790 dithering_mode =
791 drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
792 ARRAY_SIZE(drm_dithering_mode_enum_list));
793 for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
794 drm_property_add_enum(dithering_mode, i,
795 drm_dithering_mode_enum_list[i].type,
796 drm_dithering_mode_enum_list[i].name);
797 dev->mode_config.dithering_mode_property = dithering_mode;
799 return 0;
801 EXPORT_SYMBOL(drm_mode_create_dithering_property);
804 * drm_mode_config_init - initialize DRM mode_configuration structure
805 * @dev: DRM device
807 * LOCKING:
808 * None, should happen single threaded at init time.
810 * Initialize @dev's mode_config structure, used for tracking the graphics
811 * configuration of @dev.
813 void drm_mode_config_init(struct drm_device *dev)
815 mutex_init(&dev->mode_config.mutex);
816 mutex_init(&dev->mode_config.idr_mutex);
817 INIT_LIST_HEAD(&dev->mode_config.fb_list);
818 INIT_LIST_HEAD(&dev->mode_config.fb_kernel_list);
819 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
820 INIT_LIST_HEAD(&dev->mode_config.connector_list);
821 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
822 INIT_LIST_HEAD(&dev->mode_config.property_list);
823 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
824 idr_init(&dev->mode_config.crtc_idr);
826 mutex_lock(&dev->mode_config.mutex);
827 drm_mode_create_standard_connector_properties(dev);
828 mutex_unlock(&dev->mode_config.mutex);
830 /* Just to be sure */
831 dev->mode_config.num_fb = 0;
832 dev->mode_config.num_connector = 0;
833 dev->mode_config.num_crtc = 0;
834 dev->mode_config.num_encoder = 0;
836 EXPORT_SYMBOL(drm_mode_config_init);
838 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
840 uint32_t total_objects = 0;
842 total_objects += dev->mode_config.num_crtc;
843 total_objects += dev->mode_config.num_connector;
844 total_objects += dev->mode_config.num_encoder;
846 if (total_objects == 0)
847 return -EINVAL;
849 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
850 if (!group->id_list)
851 return -ENOMEM;
853 group->num_crtcs = 0;
854 group->num_connectors = 0;
855 group->num_encoders = 0;
856 return 0;
859 int drm_mode_group_init_legacy_group(struct drm_device *dev,
860 struct drm_mode_group *group)
862 struct drm_crtc *crtc;
863 struct drm_encoder *encoder;
864 struct drm_connector *connector;
865 int ret;
867 if ((ret = drm_mode_group_init(dev, group)))
868 return ret;
870 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
871 group->id_list[group->num_crtcs++] = crtc->base.id;
873 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
874 group->id_list[group->num_crtcs + group->num_encoders++] =
875 encoder->base.id;
877 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
878 group->id_list[group->num_crtcs + group->num_encoders +
879 group->num_connectors++] = connector->base.id;
881 return 0;
885 * drm_mode_config_cleanup - free up DRM mode_config info
886 * @dev: DRM device
888 * LOCKING:
889 * Caller must hold mode config lock.
891 * Free up all the connectors and CRTCs associated with this DRM device, then
892 * free up the framebuffers and associated buffer objects.
894 * FIXME: cleanup any dangling user buffer objects too
896 void drm_mode_config_cleanup(struct drm_device *dev)
898 struct drm_connector *connector, *ot;
899 struct drm_crtc *crtc, *ct;
900 struct drm_encoder *encoder, *enct;
901 struct drm_framebuffer *fb, *fbt;
902 struct drm_property *property, *pt;
904 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
905 head) {
906 encoder->funcs->destroy(encoder);
909 list_for_each_entry_safe(connector, ot,
910 &dev->mode_config.connector_list, head) {
911 connector->funcs->destroy(connector);
914 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
915 head) {
916 drm_property_destroy(dev, property);
919 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
920 fb->funcs->destroy(fb);
923 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
924 crtc->funcs->destroy(crtc);
928 EXPORT_SYMBOL(drm_mode_config_cleanup);
931 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
932 * @out: drm_mode_modeinfo struct to return to the user
933 * @in: drm_display_mode to use
935 * LOCKING:
936 * None.
938 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
939 * the user.
941 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
942 struct drm_display_mode *in)
944 out->clock = in->clock;
945 out->hdisplay = in->hdisplay;
946 out->hsync_start = in->hsync_start;
947 out->hsync_end = in->hsync_end;
948 out->htotal = in->htotal;
949 out->hskew = in->hskew;
950 out->vdisplay = in->vdisplay;
951 out->vsync_start = in->vsync_start;
952 out->vsync_end = in->vsync_end;
953 out->vtotal = in->vtotal;
954 out->vscan = in->vscan;
955 out->vrefresh = in->vrefresh;
956 out->flags = in->flags;
957 out->type = in->type;
958 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
959 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
963 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
964 * @out: drm_display_mode to return to the user
965 * @in: drm_mode_modeinfo to use
967 * LOCKING:
968 * None.
970 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
971 * the caller.
973 void drm_crtc_convert_umode(struct drm_display_mode *out,
974 struct drm_mode_modeinfo *in)
976 out->clock = in->clock;
977 out->hdisplay = in->hdisplay;
978 out->hsync_start = in->hsync_start;
979 out->hsync_end = in->hsync_end;
980 out->htotal = in->htotal;
981 out->hskew = in->hskew;
982 out->vdisplay = in->vdisplay;
983 out->vsync_start = in->vsync_start;
984 out->vsync_end = in->vsync_end;
985 out->vtotal = in->vtotal;
986 out->vscan = in->vscan;
987 out->vrefresh = in->vrefresh;
988 out->flags = in->flags;
989 out->type = in->type;
990 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
991 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
995 * drm_mode_getresources - get graphics configuration
996 * @inode: inode from the ioctl
997 * @filp: file * from the ioctl
998 * @cmd: cmd from ioctl
999 * @arg: arg from ioctl
1001 * LOCKING:
1002 * Takes mode config lock.
1004 * Construct a set of configuration description structures and return
1005 * them to the user, including CRTC, connector and framebuffer configuration.
1007 * Called by the user via ioctl.
1009 * RETURNS:
1010 * Zero on success, errno on failure.
1012 int drm_mode_getresources(struct drm_device *dev, void *data,
1013 struct drm_file *file_priv)
1015 struct drm_mode_card_res *card_res = data;
1016 struct list_head *lh;
1017 struct drm_framebuffer *fb;
1018 struct drm_connector *connector;
1019 struct drm_crtc *crtc;
1020 struct drm_encoder *encoder;
1021 int ret = 0;
1022 int connector_count = 0;
1023 int crtc_count = 0;
1024 int fb_count = 0;
1025 int encoder_count = 0;
1026 int copied = 0, i;
1027 uint32_t __user *fb_id;
1028 uint32_t __user *crtc_id;
1029 uint32_t __user *connector_id;
1030 uint32_t __user *encoder_id;
1031 struct drm_mode_group *mode_group;
1033 mutex_lock(&dev->mode_config.mutex);
1036 * For the non-control nodes we need to limit the list of resources
1037 * by IDs in the group list for this node
1039 list_for_each(lh, &file_priv->fbs)
1040 fb_count++;
1042 mode_group = &file_priv->master->minor->mode_group;
1043 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1045 list_for_each(lh, &dev->mode_config.crtc_list)
1046 crtc_count++;
1048 list_for_each(lh, &dev->mode_config.connector_list)
1049 connector_count++;
1051 list_for_each(lh, &dev->mode_config.encoder_list)
1052 encoder_count++;
1053 } else {
1055 crtc_count = mode_group->num_crtcs;
1056 connector_count = mode_group->num_connectors;
1057 encoder_count = mode_group->num_encoders;
1060 card_res->max_height = dev->mode_config.max_height;
1061 card_res->min_height = dev->mode_config.min_height;
1062 card_res->max_width = dev->mode_config.max_width;
1063 card_res->min_width = dev->mode_config.min_width;
1065 /* handle this in 4 parts */
1066 /* FBs */
1067 if (card_res->count_fbs >= fb_count) {
1068 copied = 0;
1069 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1070 list_for_each_entry(fb, &file_priv->fbs, head) {
1071 if (put_user(fb->base.id, fb_id + copied)) {
1072 ret = -EFAULT;
1073 goto out;
1075 copied++;
1078 card_res->count_fbs = fb_count;
1080 /* CRTCs */
1081 if (card_res->count_crtcs >= crtc_count) {
1082 copied = 0;
1083 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1084 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1085 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1086 head) {
1087 DRM_DEBUG_KMS("CRTC ID is %d\n", crtc->base.id);
1088 if (put_user(crtc->base.id, crtc_id + copied)) {
1089 ret = -EFAULT;
1090 goto out;
1092 copied++;
1094 } else {
1095 for (i = 0; i < mode_group->num_crtcs; i++) {
1096 if (put_user(mode_group->id_list[i],
1097 crtc_id + copied)) {
1098 ret = -EFAULT;
1099 goto out;
1101 copied++;
1105 card_res->count_crtcs = crtc_count;
1107 /* Encoders */
1108 if (card_res->count_encoders >= encoder_count) {
1109 copied = 0;
1110 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1111 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1112 list_for_each_entry(encoder,
1113 &dev->mode_config.encoder_list,
1114 head) {
1115 DRM_DEBUG_KMS("ENCODER ID is %d\n",
1116 encoder->base.id);
1117 if (put_user(encoder->base.id, encoder_id +
1118 copied)) {
1119 ret = -EFAULT;
1120 goto out;
1122 copied++;
1124 } else {
1125 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1126 if (put_user(mode_group->id_list[i],
1127 encoder_id + copied)) {
1128 ret = -EFAULT;
1129 goto out;
1131 copied++;
1136 card_res->count_encoders = encoder_count;
1138 /* Connectors */
1139 if (card_res->count_connectors >= connector_count) {
1140 copied = 0;
1141 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1142 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1143 list_for_each_entry(connector,
1144 &dev->mode_config.connector_list,
1145 head) {
1146 DRM_DEBUG_KMS("CONNECTOR ID is %d\n",
1147 connector->base.id);
1148 if (put_user(connector->base.id,
1149 connector_id + copied)) {
1150 ret = -EFAULT;
1151 goto out;
1153 copied++;
1155 } else {
1156 int start = mode_group->num_crtcs +
1157 mode_group->num_encoders;
1158 for (i = start; i < start + mode_group->num_connectors; i++) {
1159 if (put_user(mode_group->id_list[i],
1160 connector_id + copied)) {
1161 ret = -EFAULT;
1162 goto out;
1164 copied++;
1168 card_res->count_connectors = connector_count;
1170 DRM_DEBUG_KMS("Counted %d %d %d\n", card_res->count_crtcs,
1171 card_res->count_connectors, card_res->count_encoders);
1173 out:
1174 mutex_unlock(&dev->mode_config.mutex);
1175 return ret;
1179 * drm_mode_getcrtc - get CRTC configuration
1180 * @inode: inode from the ioctl
1181 * @filp: file * from the ioctl
1182 * @cmd: cmd from ioctl
1183 * @arg: arg from ioctl
1185 * LOCKING:
1186 * Caller? (FIXME)
1188 * Construct a CRTC configuration structure to return to the user.
1190 * Called by the user via ioctl.
1192 * RETURNS:
1193 * Zero on success, errno on failure.
1195 int drm_mode_getcrtc(struct drm_device *dev,
1196 void *data, struct drm_file *file_priv)
1198 struct drm_mode_crtc *crtc_resp = data;
1199 struct drm_crtc *crtc;
1200 struct drm_mode_object *obj;
1201 int ret = 0;
1203 mutex_lock(&dev->mode_config.mutex);
1205 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1206 DRM_MODE_OBJECT_CRTC);
1207 if (!obj) {
1208 ret = -EINVAL;
1209 goto out;
1211 crtc = obj_to_crtc(obj);
1213 crtc_resp->x = crtc->x;
1214 crtc_resp->y = crtc->y;
1215 crtc_resp->gamma_size = crtc->gamma_size;
1216 if (crtc->fb)
1217 crtc_resp->fb_id = crtc->fb->base.id;
1218 else
1219 crtc_resp->fb_id = 0;
1221 if (crtc->enabled) {
1223 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1224 crtc_resp->mode_valid = 1;
1226 } else {
1227 crtc_resp->mode_valid = 0;
1230 out:
1231 mutex_unlock(&dev->mode_config.mutex);
1232 return ret;
1236 * drm_mode_getconnector - get connector configuration
1237 * @inode: inode from the ioctl
1238 * @filp: file * from the ioctl
1239 * @cmd: cmd from ioctl
1240 * @arg: arg from ioctl
1242 * LOCKING:
1243 * Caller? (FIXME)
1245 * Construct a connector configuration structure to return to the user.
1247 * Called by the user via ioctl.
1249 * RETURNS:
1250 * Zero on success, errno on failure.
1252 int drm_mode_getconnector(struct drm_device *dev, void *data,
1253 struct drm_file *file_priv)
1255 struct drm_mode_get_connector *out_resp = data;
1256 struct drm_mode_object *obj;
1257 struct drm_connector *connector;
1258 struct drm_display_mode *mode;
1259 int mode_count = 0;
1260 int props_count = 0;
1261 int encoders_count = 0;
1262 int ret = 0;
1263 int copied = 0;
1264 int i;
1265 struct drm_mode_modeinfo u_mode;
1266 struct drm_mode_modeinfo __user *mode_ptr;
1267 uint32_t __user *prop_ptr;
1268 uint64_t __user *prop_values;
1269 uint32_t __user *encoder_ptr;
1271 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1273 DRM_DEBUG_KMS("connector id %d:\n", out_resp->connector_id);
1275 mutex_lock(&dev->mode_config.mutex);
1277 obj = drm_mode_object_find(dev, out_resp->connector_id,
1278 DRM_MODE_OBJECT_CONNECTOR);
1279 if (!obj) {
1280 ret = -EINVAL;
1281 goto out;
1283 connector = obj_to_connector(obj);
1285 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1286 if (connector->property_ids[i] != 0) {
1287 props_count++;
1291 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1292 if (connector->encoder_ids[i] != 0) {
1293 encoders_count++;
1297 if (out_resp->count_modes == 0) {
1298 connector->funcs->fill_modes(connector,
1299 dev->mode_config.max_width,
1300 dev->mode_config.max_height);
1303 /* delayed so we get modes regardless of pre-fill_modes state */
1304 list_for_each_entry(mode, &connector->modes, head)
1305 mode_count++;
1307 out_resp->connector_id = connector->base.id;
1308 out_resp->connector_type = connector->connector_type;
1309 out_resp->connector_type_id = connector->connector_type_id;
1310 out_resp->mm_width = connector->display_info.width_mm;
1311 out_resp->mm_height = connector->display_info.height_mm;
1312 out_resp->subpixel = connector->display_info.subpixel_order;
1313 out_resp->connection = connector->status;
1314 if (connector->encoder)
1315 out_resp->encoder_id = connector->encoder->base.id;
1316 else
1317 out_resp->encoder_id = 0;
1320 * This ioctl is called twice, once to determine how much space is
1321 * needed, and the 2nd time to fill it.
1323 if ((out_resp->count_modes >= mode_count) && mode_count) {
1324 copied = 0;
1325 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1326 list_for_each_entry(mode, &connector->modes, head) {
1327 drm_crtc_convert_to_umode(&u_mode, mode);
1328 if (copy_to_user(mode_ptr + copied,
1329 &u_mode, sizeof(u_mode))) {
1330 ret = -EFAULT;
1331 goto out;
1333 copied++;
1336 out_resp->count_modes = mode_count;
1338 if ((out_resp->count_props >= props_count) && props_count) {
1339 copied = 0;
1340 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1341 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1342 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1343 if (connector->property_ids[i] != 0) {
1344 if (put_user(connector->property_ids[i],
1345 prop_ptr + copied)) {
1346 ret = -EFAULT;
1347 goto out;
1350 if (put_user(connector->property_values[i],
1351 prop_values + copied)) {
1352 ret = -EFAULT;
1353 goto out;
1355 copied++;
1359 out_resp->count_props = props_count;
1361 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1362 copied = 0;
1363 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1364 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1365 if (connector->encoder_ids[i] != 0) {
1366 if (put_user(connector->encoder_ids[i],
1367 encoder_ptr + copied)) {
1368 ret = -EFAULT;
1369 goto out;
1371 copied++;
1375 out_resp->count_encoders = encoders_count;
1377 out:
1378 mutex_unlock(&dev->mode_config.mutex);
1379 return ret;
1382 int drm_mode_getencoder(struct drm_device *dev, void *data,
1383 struct drm_file *file_priv)
1385 struct drm_mode_get_encoder *enc_resp = data;
1386 struct drm_mode_object *obj;
1387 struct drm_encoder *encoder;
1388 int ret = 0;
1390 mutex_lock(&dev->mode_config.mutex);
1391 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1392 DRM_MODE_OBJECT_ENCODER);
1393 if (!obj) {
1394 ret = -EINVAL;
1395 goto out;
1397 encoder = obj_to_encoder(obj);
1399 if (encoder->crtc)
1400 enc_resp->crtc_id = encoder->crtc->base.id;
1401 else
1402 enc_resp->crtc_id = 0;
1403 enc_resp->encoder_type = encoder->encoder_type;
1404 enc_resp->encoder_id = encoder->base.id;
1405 enc_resp->possible_crtcs = encoder->possible_crtcs;
1406 enc_resp->possible_clones = encoder->possible_clones;
1408 out:
1409 mutex_unlock(&dev->mode_config.mutex);
1410 return ret;
1414 * drm_mode_setcrtc - set CRTC configuration
1415 * @inode: inode from the ioctl
1416 * @filp: file * from the ioctl
1417 * @cmd: cmd from ioctl
1418 * @arg: arg from ioctl
1420 * LOCKING:
1421 * Caller? (FIXME)
1423 * Build a new CRTC configuration based on user request.
1425 * Called by the user via ioctl.
1427 * RETURNS:
1428 * Zero on success, errno on failure.
1430 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1431 struct drm_file *file_priv)
1433 struct drm_mode_config *config = &dev->mode_config;
1434 struct drm_mode_crtc *crtc_req = data;
1435 struct drm_mode_object *obj;
1436 struct drm_crtc *crtc, *crtcfb;
1437 struct drm_connector **connector_set = NULL, *connector;
1438 struct drm_framebuffer *fb = NULL;
1439 struct drm_display_mode *mode = NULL;
1440 struct drm_mode_set set;
1441 uint32_t __user *set_connectors_ptr;
1442 int ret = 0;
1443 int i;
1445 mutex_lock(&dev->mode_config.mutex);
1446 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1447 DRM_MODE_OBJECT_CRTC);
1448 if (!obj) {
1449 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1450 ret = -EINVAL;
1451 goto out;
1453 crtc = obj_to_crtc(obj);
1455 if (crtc_req->mode_valid) {
1456 /* If we have a mode we need a framebuffer. */
1457 /* If we pass -1, set the mode with the currently bound fb */
1458 if (crtc_req->fb_id == -1) {
1459 list_for_each_entry(crtcfb,
1460 &dev->mode_config.crtc_list, head) {
1461 if (crtcfb == crtc) {
1462 DRM_DEBUG_KMS("Using current fb for "
1463 "setmode\n");
1464 fb = crtc->fb;
1467 } else {
1468 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1469 DRM_MODE_OBJECT_FB);
1470 if (!obj) {
1471 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1472 crtc_req->fb_id);
1473 ret = -EINVAL;
1474 goto out;
1476 fb = obj_to_fb(obj);
1479 mode = drm_mode_create(dev);
1480 drm_crtc_convert_umode(mode, &crtc_req->mode);
1481 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1484 if (crtc_req->count_connectors == 0 && mode) {
1485 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1486 ret = -EINVAL;
1487 goto out;
1490 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1491 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1492 crtc_req->count_connectors);
1493 ret = -EINVAL;
1494 goto out;
1497 if (crtc_req->count_connectors > 0) {
1498 u32 out_id;
1500 /* Avoid unbounded kernel memory allocation */
1501 if (crtc_req->count_connectors > config->num_connector) {
1502 ret = -EINVAL;
1503 goto out;
1506 connector_set = kmalloc(crtc_req->count_connectors *
1507 sizeof(struct drm_connector *),
1508 GFP_KERNEL);
1509 if (!connector_set) {
1510 ret = -ENOMEM;
1511 goto out;
1514 for (i = 0; i < crtc_req->count_connectors; i++) {
1515 set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1516 if (get_user(out_id, &set_connectors_ptr[i])) {
1517 ret = -EFAULT;
1518 goto out;
1521 obj = drm_mode_object_find(dev, out_id,
1522 DRM_MODE_OBJECT_CONNECTOR);
1523 if (!obj) {
1524 DRM_DEBUG_KMS("Connector id %d unknown\n",
1525 out_id);
1526 ret = -EINVAL;
1527 goto out;
1529 connector = obj_to_connector(obj);
1531 connector_set[i] = connector;
1535 set.crtc = crtc;
1536 set.x = crtc_req->x;
1537 set.y = crtc_req->y;
1538 set.mode = mode;
1539 set.connectors = connector_set;
1540 set.num_connectors = crtc_req->count_connectors;
1541 set.fb = fb;
1542 ret = crtc->funcs->set_config(&set);
1544 out:
1545 kfree(connector_set);
1546 mutex_unlock(&dev->mode_config.mutex);
1547 return ret;
1550 int drm_mode_cursor_ioctl(struct drm_device *dev,
1551 void *data, struct drm_file *file_priv)
1553 struct drm_mode_cursor *req = data;
1554 struct drm_mode_object *obj;
1555 struct drm_crtc *crtc;
1556 int ret = 0;
1558 DRM_DEBUG_KMS("\n");
1560 if (!req->flags) {
1561 DRM_ERROR("no operation set\n");
1562 return -EINVAL;
1565 mutex_lock(&dev->mode_config.mutex);
1566 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1567 if (!obj) {
1568 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1569 ret = -EINVAL;
1570 goto out;
1572 crtc = obj_to_crtc(obj);
1574 if (req->flags & DRM_MODE_CURSOR_BO) {
1575 if (!crtc->funcs->cursor_set) {
1576 DRM_ERROR("crtc does not support cursor\n");
1577 ret = -ENXIO;
1578 goto out;
1580 /* Turns off the cursor if handle is 0 */
1581 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1582 req->width, req->height);
1585 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1586 if (crtc->funcs->cursor_move) {
1587 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1588 } else {
1589 DRM_ERROR("crtc does not support cursor\n");
1590 ret = -EFAULT;
1591 goto out;
1594 out:
1595 mutex_unlock(&dev->mode_config.mutex);
1596 return ret;
1600 * drm_mode_addfb - add an FB to the graphics configuration
1601 * @inode: inode from the ioctl
1602 * @filp: file * from the ioctl
1603 * @cmd: cmd from ioctl
1604 * @arg: arg from ioctl
1606 * LOCKING:
1607 * Takes mode config lock.
1609 * Add a new FB to the specified CRTC, given a user request.
1611 * Called by the user via ioctl.
1613 * RETURNS:
1614 * Zero on success, errno on failure.
1616 int drm_mode_addfb(struct drm_device *dev,
1617 void *data, struct drm_file *file_priv)
1619 struct drm_mode_fb_cmd *r = data;
1620 struct drm_mode_config *config = &dev->mode_config;
1621 struct drm_framebuffer *fb;
1622 int ret = 0;
1624 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1625 DRM_ERROR("mode new framebuffer width not within limits\n");
1626 return -EINVAL;
1628 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1629 DRM_ERROR("mode new framebuffer height not within limits\n");
1630 return -EINVAL;
1633 mutex_lock(&dev->mode_config.mutex);
1635 /* TODO check buffer is sufficently large */
1636 /* TODO setup destructor callback */
1638 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1639 if (!fb) {
1640 DRM_ERROR("could not create framebuffer\n");
1641 ret = -EINVAL;
1642 goto out;
1645 r->fb_id = fb->base.id;
1646 list_add(&fb->filp_head, &file_priv->fbs);
1648 out:
1649 mutex_unlock(&dev->mode_config.mutex);
1650 return ret;
1654 * drm_mode_rmfb - remove an FB from the configuration
1655 * @inode: inode from the ioctl
1656 * @filp: file * from the ioctl
1657 * @cmd: cmd from ioctl
1658 * @arg: arg from ioctl
1660 * LOCKING:
1661 * Takes mode config lock.
1663 * Remove the FB specified by the user.
1665 * Called by the user via ioctl.
1667 * RETURNS:
1668 * Zero on success, errno on failure.
1670 int drm_mode_rmfb(struct drm_device *dev,
1671 void *data, struct drm_file *file_priv)
1673 struct drm_mode_object *obj;
1674 struct drm_framebuffer *fb = NULL;
1675 struct drm_framebuffer *fbl = NULL;
1676 uint32_t *id = data;
1677 int ret = 0;
1678 int found = 0;
1680 mutex_lock(&dev->mode_config.mutex);
1681 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1682 /* TODO check that we realy get a framebuffer back. */
1683 if (!obj) {
1684 DRM_ERROR("mode invalid framebuffer id\n");
1685 ret = -EINVAL;
1686 goto out;
1688 fb = obj_to_fb(obj);
1690 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1691 if (fb == fbl)
1692 found = 1;
1694 if (!found) {
1695 DRM_ERROR("tried to remove a fb that we didn't own\n");
1696 ret = -EINVAL;
1697 goto out;
1700 /* TODO release all crtc connected to the framebuffer */
1701 /* TODO unhock the destructor from the buffer object */
1703 list_del(&fb->filp_head);
1704 fb->funcs->destroy(fb);
1706 out:
1707 mutex_unlock(&dev->mode_config.mutex);
1708 return ret;
1712 * drm_mode_getfb - get FB info
1713 * @inode: inode from the ioctl
1714 * @filp: file * from the ioctl
1715 * @cmd: cmd from ioctl
1716 * @arg: arg from ioctl
1718 * LOCKING:
1719 * Caller? (FIXME)
1721 * Lookup the FB given its ID and return info about it.
1723 * Called by the user via ioctl.
1725 * RETURNS:
1726 * Zero on success, errno on failure.
1728 int drm_mode_getfb(struct drm_device *dev,
1729 void *data, struct drm_file *file_priv)
1731 struct drm_mode_fb_cmd *r = data;
1732 struct drm_mode_object *obj;
1733 struct drm_framebuffer *fb;
1734 int ret = 0;
1736 mutex_lock(&dev->mode_config.mutex);
1737 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1738 if (!obj) {
1739 DRM_ERROR("invalid framebuffer id\n");
1740 ret = -EINVAL;
1741 goto out;
1743 fb = obj_to_fb(obj);
1745 r->height = fb->height;
1746 r->width = fb->width;
1747 r->depth = fb->depth;
1748 r->bpp = fb->bits_per_pixel;
1749 r->pitch = fb->pitch;
1750 fb->funcs->create_handle(fb, file_priv, &r->handle);
1752 out:
1753 mutex_unlock(&dev->mode_config.mutex);
1754 return ret;
1758 * drm_fb_release - remove and free the FBs on this file
1759 * @filp: file * from the ioctl
1761 * LOCKING:
1762 * Takes mode config lock.
1764 * Destroy all the FBs associated with @filp.
1766 * Called by the user via ioctl.
1768 * RETURNS:
1769 * Zero on success, errno on failure.
1771 void drm_fb_release(struct drm_file *priv)
1773 struct drm_device *dev = priv->minor->dev;
1774 struct drm_framebuffer *fb, *tfb;
1776 mutex_lock(&dev->mode_config.mutex);
1777 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1778 list_del(&fb->filp_head);
1779 fb->funcs->destroy(fb);
1781 mutex_unlock(&dev->mode_config.mutex);
1785 * drm_mode_attachmode - add a mode to the user mode list
1786 * @dev: DRM device
1787 * @connector: connector to add the mode to
1788 * @mode: mode to add
1790 * Add @mode to @connector's user mode list.
1792 static int drm_mode_attachmode(struct drm_device *dev,
1793 struct drm_connector *connector,
1794 struct drm_display_mode *mode)
1796 int ret = 0;
1798 list_add_tail(&mode->head, &connector->user_modes);
1799 return ret;
1802 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1803 struct drm_display_mode *mode)
1805 struct drm_connector *connector;
1806 int ret = 0;
1807 struct drm_display_mode *dup_mode;
1808 int need_dup = 0;
1809 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1810 if (!connector->encoder)
1811 break;
1812 if (connector->encoder->crtc == crtc) {
1813 if (need_dup)
1814 dup_mode = drm_mode_duplicate(dev, mode);
1815 else
1816 dup_mode = mode;
1817 ret = drm_mode_attachmode(dev, connector, dup_mode);
1818 if (ret)
1819 return ret;
1820 need_dup = 1;
1823 return 0;
1825 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1827 static int drm_mode_detachmode(struct drm_device *dev,
1828 struct drm_connector *connector,
1829 struct drm_display_mode *mode)
1831 int found = 0;
1832 int ret = 0;
1833 struct drm_display_mode *match_mode, *t;
1835 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1836 if (drm_mode_equal(match_mode, mode)) {
1837 list_del(&match_mode->head);
1838 drm_mode_destroy(dev, match_mode);
1839 found = 1;
1840 break;
1844 if (!found)
1845 ret = -EINVAL;
1847 return ret;
1850 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1852 struct drm_connector *connector;
1854 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1855 drm_mode_detachmode(dev, connector, mode);
1857 return 0;
1859 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1862 * drm_fb_attachmode - Attach a user mode to an connector
1863 * @inode: inode from the ioctl
1864 * @filp: file * from the ioctl
1865 * @cmd: cmd from ioctl
1866 * @arg: arg from ioctl
1868 * This attaches a user specified mode to an connector.
1869 * Called by the user via ioctl.
1871 * RETURNS:
1872 * Zero on success, errno on failure.
1874 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1875 void *data, struct drm_file *file_priv)
1877 struct drm_mode_mode_cmd *mode_cmd = data;
1878 struct drm_connector *connector;
1879 struct drm_display_mode *mode;
1880 struct drm_mode_object *obj;
1881 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1882 int ret = 0;
1884 mutex_lock(&dev->mode_config.mutex);
1886 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1887 if (!obj) {
1888 ret = -EINVAL;
1889 goto out;
1891 connector = obj_to_connector(obj);
1893 mode = drm_mode_create(dev);
1894 if (!mode) {
1895 ret = -ENOMEM;
1896 goto out;
1899 drm_crtc_convert_umode(mode, umode);
1901 ret = drm_mode_attachmode(dev, connector, mode);
1902 out:
1903 mutex_unlock(&dev->mode_config.mutex);
1904 return ret;
1909 * drm_fb_detachmode - Detach a user specified mode from an connector
1910 * @inode: inode from the ioctl
1911 * @filp: file * from the ioctl
1912 * @cmd: cmd from ioctl
1913 * @arg: arg from ioctl
1915 * Called by the user via ioctl.
1917 * RETURNS:
1918 * Zero on success, errno on failure.
1920 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1921 void *data, struct drm_file *file_priv)
1923 struct drm_mode_object *obj;
1924 struct drm_mode_mode_cmd *mode_cmd = data;
1925 struct drm_connector *connector;
1926 struct drm_display_mode mode;
1927 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1928 int ret = 0;
1930 mutex_lock(&dev->mode_config.mutex);
1932 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1933 if (!obj) {
1934 ret = -EINVAL;
1935 goto out;
1937 connector = obj_to_connector(obj);
1939 drm_crtc_convert_umode(&mode, umode);
1940 ret = drm_mode_detachmode(dev, connector, &mode);
1941 out:
1942 mutex_unlock(&dev->mode_config.mutex);
1943 return ret;
1946 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1947 const char *name, int num_values)
1949 struct drm_property *property = NULL;
1951 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
1952 if (!property)
1953 return NULL;
1955 if (num_values) {
1956 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1957 if (!property->values)
1958 goto fail;
1961 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
1962 property->flags = flags;
1963 property->num_values = num_values;
1964 INIT_LIST_HEAD(&property->enum_blob_list);
1966 if (name)
1967 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1969 list_add_tail(&property->head, &dev->mode_config.property_list);
1970 return property;
1971 fail:
1972 kfree(property);
1973 return NULL;
1975 EXPORT_SYMBOL(drm_property_create);
1977 int drm_property_add_enum(struct drm_property *property, int index,
1978 uint64_t value, const char *name)
1980 struct drm_property_enum *prop_enum;
1982 if (!(property->flags & DRM_MODE_PROP_ENUM))
1983 return -EINVAL;
1985 if (!list_empty(&property->enum_blob_list)) {
1986 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1987 if (prop_enum->value == value) {
1988 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1989 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1990 return 0;
1995 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1996 if (!prop_enum)
1997 return -ENOMEM;
1999 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2000 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2001 prop_enum->value = value;
2003 property->values[index] = value;
2004 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2005 return 0;
2007 EXPORT_SYMBOL(drm_property_add_enum);
2009 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2011 struct drm_property_enum *prop_enum, *pt;
2013 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2014 list_del(&prop_enum->head);
2015 kfree(prop_enum);
2018 if (property->num_values)
2019 kfree(property->values);
2020 drm_mode_object_put(dev, &property->base);
2021 list_del(&property->head);
2022 kfree(property);
2024 EXPORT_SYMBOL(drm_property_destroy);
2026 int drm_connector_attach_property(struct drm_connector *connector,
2027 struct drm_property *property, uint64_t init_val)
2029 int i;
2031 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2032 if (connector->property_ids[i] == 0) {
2033 connector->property_ids[i] = property->base.id;
2034 connector->property_values[i] = init_val;
2035 break;
2039 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2040 return -EINVAL;
2041 return 0;
2043 EXPORT_SYMBOL(drm_connector_attach_property);
2045 int drm_connector_property_set_value(struct drm_connector *connector,
2046 struct drm_property *property, uint64_t value)
2048 int i;
2050 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2051 if (connector->property_ids[i] == property->base.id) {
2052 connector->property_values[i] = value;
2053 break;
2057 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2058 return -EINVAL;
2059 return 0;
2061 EXPORT_SYMBOL(drm_connector_property_set_value);
2063 int drm_connector_property_get_value(struct drm_connector *connector,
2064 struct drm_property *property, uint64_t *val)
2066 int i;
2068 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2069 if (connector->property_ids[i] == property->base.id) {
2070 *val = connector->property_values[i];
2071 break;
2075 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2076 return -EINVAL;
2077 return 0;
2079 EXPORT_SYMBOL(drm_connector_property_get_value);
2081 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2082 void *data, struct drm_file *file_priv)
2084 struct drm_mode_object *obj;
2085 struct drm_mode_get_property *out_resp = data;
2086 struct drm_property *property;
2087 int enum_count = 0;
2088 int blob_count = 0;
2089 int value_count = 0;
2090 int ret = 0, i;
2091 int copied;
2092 struct drm_property_enum *prop_enum;
2093 struct drm_mode_property_enum __user *enum_ptr;
2094 struct drm_property_blob *prop_blob;
2095 uint32_t *blob_id_ptr;
2096 uint64_t __user *values_ptr;
2097 uint32_t __user *blob_length_ptr;
2099 mutex_lock(&dev->mode_config.mutex);
2100 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2101 if (!obj) {
2102 ret = -EINVAL;
2103 goto done;
2105 property = obj_to_property(obj);
2107 if (property->flags & DRM_MODE_PROP_ENUM) {
2108 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2109 enum_count++;
2110 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2111 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2112 blob_count++;
2115 value_count = property->num_values;
2117 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2118 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2119 out_resp->flags = property->flags;
2121 if ((out_resp->count_values >= value_count) && value_count) {
2122 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2123 for (i = 0; i < value_count; i++) {
2124 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2125 ret = -EFAULT;
2126 goto done;
2130 out_resp->count_values = value_count;
2132 if (property->flags & DRM_MODE_PROP_ENUM) {
2133 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2134 copied = 0;
2135 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2136 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2138 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2139 ret = -EFAULT;
2140 goto done;
2143 if (copy_to_user(&enum_ptr[copied].name,
2144 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2145 ret = -EFAULT;
2146 goto done;
2148 copied++;
2151 out_resp->count_enum_blobs = enum_count;
2154 if (property->flags & DRM_MODE_PROP_BLOB) {
2155 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2156 copied = 0;
2157 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2158 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2160 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2161 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2162 ret = -EFAULT;
2163 goto done;
2166 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2167 ret = -EFAULT;
2168 goto done;
2171 copied++;
2174 out_resp->count_enum_blobs = blob_count;
2176 done:
2177 mutex_unlock(&dev->mode_config.mutex);
2178 return ret;
2181 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2182 void *data)
2184 struct drm_property_blob *blob;
2186 if (!length || !data)
2187 return NULL;
2189 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2190 if (!blob)
2191 return NULL;
2193 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2194 blob->length = length;
2196 memcpy(blob->data, data, length);
2198 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2200 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2201 return blob;
2204 static void drm_property_destroy_blob(struct drm_device *dev,
2205 struct drm_property_blob *blob)
2207 drm_mode_object_put(dev, &blob->base);
2208 list_del(&blob->head);
2209 kfree(blob);
2212 int drm_mode_getblob_ioctl(struct drm_device *dev,
2213 void *data, struct drm_file *file_priv)
2215 struct drm_mode_object *obj;
2216 struct drm_mode_get_blob *out_resp = data;
2217 struct drm_property_blob *blob;
2218 int ret = 0;
2219 void *blob_ptr;
2221 mutex_lock(&dev->mode_config.mutex);
2222 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2223 if (!obj) {
2224 ret = -EINVAL;
2225 goto done;
2227 blob = obj_to_blob(obj);
2229 if (out_resp->length == blob->length) {
2230 blob_ptr = (void *)(unsigned long)out_resp->data;
2231 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2232 ret = -EFAULT;
2233 goto done;
2236 out_resp->length = blob->length;
2238 done:
2239 mutex_unlock(&dev->mode_config.mutex);
2240 return ret;
2243 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2244 struct edid *edid)
2246 struct drm_device *dev = connector->dev;
2247 int ret = 0;
2249 if (connector->edid_blob_ptr)
2250 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2252 /* Delete edid, when there is none. */
2253 if (!edid) {
2254 connector->edid_blob_ptr = NULL;
2255 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2256 return ret;
2259 connector->edid_blob_ptr = drm_property_create_blob(connector->dev, 128, edid);
2261 ret = drm_connector_property_set_value(connector,
2262 dev->mode_config.edid_property,
2263 connector->edid_blob_ptr->base.id);
2265 return ret;
2267 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2269 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2270 void *data, struct drm_file *file_priv)
2272 struct drm_mode_connector_set_property *out_resp = data;
2273 struct drm_mode_object *obj;
2274 struct drm_property *property;
2275 struct drm_connector *connector;
2276 int ret = -EINVAL;
2277 int i;
2279 mutex_lock(&dev->mode_config.mutex);
2281 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2282 if (!obj) {
2283 goto out;
2285 connector = obj_to_connector(obj);
2287 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2288 if (connector->property_ids[i] == out_resp->prop_id)
2289 break;
2292 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2293 goto out;
2296 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2297 if (!obj) {
2298 goto out;
2300 property = obj_to_property(obj);
2302 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2303 goto out;
2305 if (property->flags & DRM_MODE_PROP_RANGE) {
2306 if (out_resp->value < property->values[0])
2307 goto out;
2309 if (out_resp->value > property->values[1])
2310 goto out;
2311 } else {
2312 int found = 0;
2313 for (i = 0; i < property->num_values; i++) {
2314 if (property->values[i] == out_resp->value) {
2315 found = 1;
2316 break;
2319 if (!found) {
2320 goto out;
2324 /* Do DPMS ourselves */
2325 if (property == connector->dev->mode_config.dpms_property) {
2326 if (connector->funcs->dpms)
2327 (*connector->funcs->dpms)(connector, (int) out_resp->value);
2328 ret = 0;
2329 } else if (connector->funcs->set_property)
2330 ret = connector->funcs->set_property(connector, property, out_resp->value);
2332 /* store the property value if succesful */
2333 if (!ret)
2334 drm_connector_property_set_value(connector, property, out_resp->value);
2335 out:
2336 mutex_unlock(&dev->mode_config.mutex);
2337 return ret;
2340 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2341 struct drm_encoder *encoder)
2343 int i;
2345 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2346 if (connector->encoder_ids[i] == 0) {
2347 connector->encoder_ids[i] = encoder->base.id;
2348 return 0;
2351 return -ENOMEM;
2353 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2355 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2356 struct drm_encoder *encoder)
2358 int i;
2359 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2360 if (connector->encoder_ids[i] == encoder->base.id) {
2361 connector->encoder_ids[i] = 0;
2362 if (connector->encoder == encoder)
2363 connector->encoder = NULL;
2364 break;
2368 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2370 bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2371 int gamma_size)
2373 crtc->gamma_size = gamma_size;
2375 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2376 if (!crtc->gamma_store) {
2377 crtc->gamma_size = 0;
2378 return false;
2381 return true;
2383 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2385 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2386 void *data, struct drm_file *file_priv)
2388 struct drm_mode_crtc_lut *crtc_lut = data;
2389 struct drm_mode_object *obj;
2390 struct drm_crtc *crtc;
2391 void *r_base, *g_base, *b_base;
2392 int size;
2393 int ret = 0;
2395 mutex_lock(&dev->mode_config.mutex);
2396 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2397 if (!obj) {
2398 ret = -EINVAL;
2399 goto out;
2401 crtc = obj_to_crtc(obj);
2403 /* memcpy into gamma store */
2404 if (crtc_lut->gamma_size != crtc->gamma_size) {
2405 ret = -EINVAL;
2406 goto out;
2409 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2410 r_base = crtc->gamma_store;
2411 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2412 ret = -EFAULT;
2413 goto out;
2416 g_base = r_base + size;
2417 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2418 ret = -EFAULT;
2419 goto out;
2422 b_base = g_base + size;
2423 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2424 ret = -EFAULT;
2425 goto out;
2428 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
2430 out:
2431 mutex_unlock(&dev->mode_config.mutex);
2432 return ret;
2436 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2437 void *data, struct drm_file *file_priv)
2439 struct drm_mode_crtc_lut *crtc_lut = data;
2440 struct drm_mode_object *obj;
2441 struct drm_crtc *crtc;
2442 void *r_base, *g_base, *b_base;
2443 int size;
2444 int ret = 0;
2446 mutex_lock(&dev->mode_config.mutex);
2447 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2448 if (!obj) {
2449 ret = -EINVAL;
2450 goto out;
2452 crtc = obj_to_crtc(obj);
2454 /* memcpy into gamma store */
2455 if (crtc_lut->gamma_size != crtc->gamma_size) {
2456 ret = -EINVAL;
2457 goto out;
2460 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2461 r_base = crtc->gamma_store;
2462 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2463 ret = -EFAULT;
2464 goto out;
2467 g_base = r_base + size;
2468 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2469 ret = -EFAULT;
2470 goto out;
2473 b_base = g_base + size;
2474 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2475 ret = -EFAULT;
2476 goto out;
2478 out:
2479 mutex_unlock(&dev->mode_config.mutex);
2480 return ret;