]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/gpu/drm/drm_crtc.c
362a538cdedc053d4a6cea08dc6eaba5c61b5eba
[mirror_ubuntu-jammy-kernel.git] / drivers / gpu / drm / drm_crtc.c
1 /*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
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.
17 *
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.
25 *
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
31 */
32 #include <linux/list.h>
33 #include "drm.h"
34 #include "drmP.h"
35 #include "drm_crtc.h"
36
37 struct drm_prop_enum_list {
38 int type;
39 char *name;
40 };
41
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)"; \
52 }
53
54 /*
55 * Global properties
56 */
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" }
62 };
63
64 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
65
66 /*
67 * Optional properties
68 */
69 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
70 {
71 { DRM_MODE_SCALE_NON_GPU, "Non-GPU" },
72 { DRM_MODE_SCALE_FULLSCREEN, "Fullscreen" },
73 { DRM_MODE_SCALE_NO_SCALE, "No scale" },
74 { DRM_MODE_SCALE_ASPECT, "Aspect" },
75 };
76
77 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
78 {
79 { DRM_MODE_DITHERING_OFF, "Off" },
80 { DRM_MODE_DITHERING_ON, "On" },
81 };
82
83 /*
84 * Non-global properties, but "required" for certain connectors.
85 */
86 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
87 {
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 */
91 };
92
93 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
94
95 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
96 {
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 */
100 };
101
102 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
103 drm_dvi_i_subconnector_enum_list)
104
105 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
106 {
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 */
112 };
113
114 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
115
116 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
117 {
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 */
123 };
124
125 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
126 drm_tv_subconnector_enum_list)
127
128 struct drm_conn_prop_enum_list {
129 int type;
130 char *name;
131 int count;
132 };
133
134 /*
135 * Connector and encoder types.
136 */
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 },
152 };
153
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" },
160 };
161
162 char *drm_get_encoder_name(struct drm_encoder *encoder)
163 {
164 static char buf[32];
165
166 snprintf(buf, 32, "%s-%d",
167 drm_encoder_enum_list[encoder->encoder_type].name,
168 encoder->base.id);
169 return buf;
170 }
171
172 char *drm_get_connector_name(struct drm_connector *connector)
173 {
174 static char buf[32];
175
176 snprintf(buf, 32, "%s-%d",
177 drm_connector_enum_list[connector->connector_type].name,
178 connector->connector_type_id);
179 return buf;
180 }
181 EXPORT_SYMBOL(drm_get_connector_name);
182
183 char *drm_get_connector_status_name(enum drm_connector_status status)
184 {
185 if (status == connector_status_connected)
186 return "connected";
187 else if (status == connector_status_disconnected)
188 return "disconnected";
189 else
190 return "unknown";
191 }
192
193 /**
194 * drm_mode_object_get - allocate a new identifier
195 * @dev: DRM device
196 * @ptr: object pointer, used to generate unique ID
197 * @type: object type
198 *
199 * LOCKING:
200 *
201 * Create a unique identifier based on @ptr in @dev's identifier space. Used
202 * for tracking modes, CRTCs and connectors.
203 *
204 * RETURNS:
205 * New unique (relative to other objects in @dev) integer identifier for the
206 * object.
207 */
208 static int drm_mode_object_get(struct drm_device *dev,
209 struct drm_mode_object *obj, uint32_t obj_type)
210 {
211 int new_id = 0;
212 int ret;
213
214 again:
215 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
216 DRM_ERROR("Ran out memory getting a mode number\n");
217 return -EINVAL;
218 }
219
220 mutex_lock(&dev->mode_config.idr_mutex);
221 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
222 mutex_unlock(&dev->mode_config.idr_mutex);
223 if (ret == -EAGAIN)
224 goto again;
225
226 obj->id = new_id;
227 obj->type = obj_type;
228 return 0;
229 }
230
231 /**
232 * drm_mode_object_put - free an identifer
233 * @dev: DRM device
234 * @id: ID to free
235 *
236 * LOCKING:
237 * Caller must hold DRM mode_config lock.
238 *
239 * Free @id from @dev's unique identifier pool.
240 */
241 static void drm_mode_object_put(struct drm_device *dev,
242 struct drm_mode_object *object)
243 {
244 mutex_lock(&dev->mode_config.idr_mutex);
245 idr_remove(&dev->mode_config.crtc_idr, object->id);
246 mutex_unlock(&dev->mode_config.idr_mutex);
247 }
248
249 void *drm_mode_object_find(struct drm_device *dev, uint32_t id, uint32_t type)
250 {
251 struct drm_mode_object *obj = NULL;
252
253 mutex_lock(&dev->mode_config.idr_mutex);
254 obj = idr_find(&dev->mode_config.crtc_idr, id);
255 if (!obj || (obj->type != type) || (obj->id != id))
256 obj = NULL;
257 mutex_unlock(&dev->mode_config.idr_mutex);
258
259 return obj;
260 }
261 EXPORT_SYMBOL(drm_mode_object_find);
262
263 /**
264 * drm_crtc_from_fb - find the CRTC structure associated with an fb
265 * @dev: DRM device
266 * @fb: framebuffer in question
267 *
268 * LOCKING:
269 * Caller must hold mode_config lock.
270 *
271 * Find CRTC in the mode_config structure that matches @fb.
272 *
273 * RETURNS:
274 * Pointer to the CRTC or NULL if it wasn't found.
275 */
276 struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
277 struct drm_framebuffer *fb)
278 {
279 struct drm_crtc *crtc;
280
281 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
282 if (crtc->fb == fb)
283 return crtc;
284 }
285 return NULL;
286 }
287
288 /**
289 * drm_framebuffer_init - initialize a framebuffer
290 * @dev: DRM device
291 *
292 * LOCKING:
293 * Caller must hold mode config lock.
294 *
295 * Allocates an ID for the framebuffer's parent mode object, sets its mode
296 * functions & device file and adds it to the master fd list.
297 *
298 * RETURNS:
299 * Zero on success, error code on falure.
300 */
301 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
302 const struct drm_framebuffer_funcs *funcs)
303 {
304 int ret;
305
306 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
307 if (ret) {
308 return ret;
309 }
310
311 fb->dev = dev;
312 fb->funcs = funcs;
313 dev->mode_config.num_fb++;
314 list_add(&fb->head, &dev->mode_config.fb_list);
315
316 return 0;
317 }
318 EXPORT_SYMBOL(drm_framebuffer_init);
319
320 /**
321 * drm_framebuffer_cleanup - remove a framebuffer object
322 * @fb: framebuffer to remove
323 *
324 * LOCKING:
325 * Caller must hold mode config lock.
326 *
327 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
328 * it, setting it to NULL.
329 */
330 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
331 {
332 struct drm_device *dev = fb->dev;
333 struct drm_crtc *crtc;
334
335 /* remove from any CRTC */
336 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
337 if (crtc->fb == fb)
338 crtc->fb = NULL;
339 }
340
341 drm_mode_object_put(dev, &fb->base);
342 list_del(&fb->head);
343 dev->mode_config.num_fb--;
344 }
345 EXPORT_SYMBOL(drm_framebuffer_cleanup);
346
347 /**
348 * drm_crtc_init - Initialise a new CRTC object
349 * @dev: DRM device
350 * @crtc: CRTC object to init
351 * @funcs: callbacks for the new CRTC
352 *
353 * LOCKING:
354 * Caller must hold mode config lock.
355 *
356 * Inits a new object created as base part of an driver crtc object.
357 */
358 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
359 const struct drm_crtc_funcs *funcs)
360 {
361 crtc->dev = dev;
362 crtc->funcs = funcs;
363
364 mutex_lock(&dev->mode_config.mutex);
365 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
366
367 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
368 dev->mode_config.num_crtc++;
369 mutex_unlock(&dev->mode_config.mutex);
370 }
371 EXPORT_SYMBOL(drm_crtc_init);
372
373 /**
374 * drm_crtc_cleanup - Cleans up the core crtc usage.
375 * @crtc: CRTC to cleanup
376 *
377 * LOCKING:
378 * Caller must hold mode config lock.
379 *
380 * Cleanup @crtc. Removes from drm modesetting space
381 * does NOT free object, caller does that.
382 */
383 void drm_crtc_cleanup(struct drm_crtc *crtc)
384 {
385 struct drm_device *dev = crtc->dev;
386
387 if (crtc->gamma_store) {
388 kfree(crtc->gamma_store);
389 crtc->gamma_store = NULL;
390 }
391
392 drm_mode_object_put(dev, &crtc->base);
393 list_del(&crtc->head);
394 dev->mode_config.num_crtc--;
395 }
396 EXPORT_SYMBOL(drm_crtc_cleanup);
397
398 /**
399 * drm_mode_probed_add - add a mode to a connector's probed mode list
400 * @connector: connector the new mode
401 * @mode: mode data
402 *
403 * LOCKING:
404 * Caller must hold mode config lock.
405 *
406 * Add @mode to @connector's mode list for later use.
407 */
408 void drm_mode_probed_add(struct drm_connector *connector,
409 struct drm_display_mode *mode)
410 {
411 list_add(&mode->head, &connector->probed_modes);
412 }
413 EXPORT_SYMBOL(drm_mode_probed_add);
414
415 /**
416 * drm_mode_remove - remove and free a mode
417 * @connector: connector list to modify
418 * @mode: mode to remove
419 *
420 * LOCKING:
421 * Caller must hold mode config lock.
422 *
423 * Remove @mode from @connector's mode list, then free it.
424 */
425 void drm_mode_remove(struct drm_connector *connector,
426 struct drm_display_mode *mode)
427 {
428 list_del(&mode->head);
429 kfree(mode);
430 }
431 EXPORT_SYMBOL(drm_mode_remove);
432
433 /**
434 * drm_connector_init - Init a preallocated connector
435 * @dev: DRM device
436 * @connector: the connector to init
437 * @funcs: callbacks for this connector
438 * @name: user visible name of the connector
439 *
440 * LOCKING:
441 * Caller must hold @dev's mode_config lock.
442 *
443 * Initialises a preallocated connector. Connectors should be
444 * subclassed as part of driver connector objects.
445 */
446 void drm_connector_init(struct drm_device *dev,
447 struct drm_connector *connector,
448 const struct drm_connector_funcs *funcs,
449 int connector_type)
450 {
451 mutex_lock(&dev->mode_config.mutex);
452
453 connector->dev = dev;
454 connector->funcs = funcs;
455 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
456 connector->connector_type = connector_type;
457 connector->connector_type_id =
458 ++drm_connector_enum_list[connector_type].count; /* TODO */
459 INIT_LIST_HEAD(&connector->user_modes);
460 INIT_LIST_HEAD(&connector->probed_modes);
461 INIT_LIST_HEAD(&connector->modes);
462 connector->edid_blob_ptr = NULL;
463
464 list_add_tail(&connector->head, &dev->mode_config.connector_list);
465 dev->mode_config.num_connector++;
466
467 drm_connector_attach_property(connector,
468 dev->mode_config.edid_property, 0);
469
470 drm_connector_attach_property(connector,
471 dev->mode_config.dpms_property, 0);
472
473 mutex_unlock(&dev->mode_config.mutex);
474 }
475 EXPORT_SYMBOL(drm_connector_init);
476
477 /**
478 * drm_connector_cleanup - cleans up an initialised connector
479 * @connector: connector to cleanup
480 *
481 * LOCKING:
482 * Caller must hold @dev's mode_config lock.
483 *
484 * Cleans up the connector but doesn't free the object.
485 */
486 void drm_connector_cleanup(struct drm_connector *connector)
487 {
488 struct drm_device *dev = connector->dev;
489 struct drm_display_mode *mode, *t;
490
491 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
492 drm_mode_remove(connector, mode);
493
494 list_for_each_entry_safe(mode, t, &connector->modes, head)
495 drm_mode_remove(connector, mode);
496
497 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
498 drm_mode_remove(connector, mode);
499
500 mutex_lock(&dev->mode_config.mutex);
501 drm_mode_object_put(dev, &connector->base);
502 list_del(&connector->head);
503 mutex_unlock(&dev->mode_config.mutex);
504 }
505 EXPORT_SYMBOL(drm_connector_cleanup);
506
507 void drm_encoder_init(struct drm_device *dev,
508 struct drm_encoder *encoder,
509 const struct drm_encoder_funcs *funcs,
510 int encoder_type)
511 {
512 mutex_lock(&dev->mode_config.mutex);
513
514 encoder->dev = dev;
515
516 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
517 encoder->encoder_type = encoder_type;
518 encoder->funcs = funcs;
519
520 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
521 dev->mode_config.num_encoder++;
522
523 mutex_unlock(&dev->mode_config.mutex);
524 }
525 EXPORT_SYMBOL(drm_encoder_init);
526
527 void drm_encoder_cleanup(struct drm_encoder *encoder)
528 {
529 struct drm_device *dev = encoder->dev;
530 mutex_lock(&dev->mode_config.mutex);
531 drm_mode_object_put(dev, &encoder->base);
532 list_del(&encoder->head);
533 mutex_unlock(&dev->mode_config.mutex);
534 }
535 EXPORT_SYMBOL(drm_encoder_cleanup);
536
537 /**
538 * drm_mode_create - create a new display mode
539 * @dev: DRM device
540 *
541 * LOCKING:
542 * Caller must hold DRM mode_config lock.
543 *
544 * Create a new drm_display_mode, give it an ID, and return it.
545 *
546 * RETURNS:
547 * Pointer to new mode on success, NULL on error.
548 */
549 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
550 {
551 struct drm_display_mode *nmode;
552
553 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
554 if (!nmode)
555 return NULL;
556
557 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
558 return nmode;
559 }
560 EXPORT_SYMBOL(drm_mode_create);
561
562 /**
563 * drm_mode_destroy - remove a mode
564 * @dev: DRM device
565 * @mode: mode to remove
566 *
567 * LOCKING:
568 * Caller must hold mode config lock.
569 *
570 * Free @mode's unique identifier, then free it.
571 */
572 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
573 {
574 drm_mode_object_put(dev, &mode->base);
575
576 kfree(mode);
577 }
578 EXPORT_SYMBOL(drm_mode_destroy);
579
580 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
581 {
582 struct drm_property *edid;
583 struct drm_property *dpms;
584 int i;
585
586 /*
587 * Standard properties (apply to all connectors)
588 */
589 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
590 DRM_MODE_PROP_IMMUTABLE,
591 "EDID", 0);
592 dev->mode_config.edid_property = edid;
593
594 dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
595 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
596 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
597 drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
598 drm_dpms_enum_list[i].name);
599 dev->mode_config.dpms_property = dpms;
600
601 return 0;
602 }
603
604 /**
605 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
606 * @dev: DRM device
607 *
608 * Called by a driver the first time a DVI-I connector is made.
609 */
610 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
611 {
612 struct drm_property *dvi_i_selector;
613 struct drm_property *dvi_i_subconnector;
614 int i;
615
616 if (dev->mode_config.dvi_i_select_subconnector_property)
617 return 0;
618
619 dvi_i_selector =
620 drm_property_create(dev, DRM_MODE_PROP_ENUM,
621 "select subconnector",
622 ARRAY_SIZE(drm_dvi_i_select_enum_list));
623 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
624 drm_property_add_enum(dvi_i_selector, i,
625 drm_dvi_i_select_enum_list[i].type,
626 drm_dvi_i_select_enum_list[i].name);
627 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
628
629 dvi_i_subconnector =
630 drm_property_create(dev, DRM_MODE_PROP_ENUM |
631 DRM_MODE_PROP_IMMUTABLE,
632 "subconnector",
633 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
634 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
635 drm_property_add_enum(dvi_i_subconnector, i,
636 drm_dvi_i_subconnector_enum_list[i].type,
637 drm_dvi_i_subconnector_enum_list[i].name);
638 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
639
640 return 0;
641 }
642 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
643
644 /**
645 * drm_create_tv_properties - create TV specific connector properties
646 * @dev: DRM device
647 * @num_modes: number of different TV formats (modes) supported
648 * @modes: array of pointers to strings containing name of each format
649 *
650 * Called by a driver's TV initialization routine, this function creates
651 * the TV specific connector properties for a given device. Caller is
652 * responsible for allocating a list of format names and passing them to
653 * this routine.
654 */
655 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
656 char *modes[])
657 {
658 struct drm_property *tv_selector;
659 struct drm_property *tv_subconnector;
660 int i;
661
662 if (dev->mode_config.tv_select_subconnector_property)
663 return 0;
664
665 /*
666 * Basic connector properties
667 */
668 tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
669 "select subconnector",
670 ARRAY_SIZE(drm_tv_select_enum_list));
671 for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
672 drm_property_add_enum(tv_selector, i,
673 drm_tv_select_enum_list[i].type,
674 drm_tv_select_enum_list[i].name);
675 dev->mode_config.tv_select_subconnector_property = tv_selector;
676
677 tv_subconnector =
678 drm_property_create(dev, DRM_MODE_PROP_ENUM |
679 DRM_MODE_PROP_IMMUTABLE, "subconnector",
680 ARRAY_SIZE(drm_tv_subconnector_enum_list));
681 for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
682 drm_property_add_enum(tv_subconnector, i,
683 drm_tv_subconnector_enum_list[i].type,
684 drm_tv_subconnector_enum_list[i].name);
685 dev->mode_config.tv_subconnector_property = tv_subconnector;
686
687 /*
688 * Other, TV specific properties: margins & TV modes.
689 */
690 dev->mode_config.tv_left_margin_property =
691 drm_property_create(dev, DRM_MODE_PROP_RANGE,
692 "left margin", 2);
693 dev->mode_config.tv_left_margin_property->values[0] = 0;
694 dev->mode_config.tv_left_margin_property->values[1] = 100;
695
696 dev->mode_config.tv_right_margin_property =
697 drm_property_create(dev, DRM_MODE_PROP_RANGE,
698 "right margin", 2);
699 dev->mode_config.tv_right_margin_property->values[0] = 0;
700 dev->mode_config.tv_right_margin_property->values[1] = 100;
701
702 dev->mode_config.tv_top_margin_property =
703 drm_property_create(dev, DRM_MODE_PROP_RANGE,
704 "top margin", 2);
705 dev->mode_config.tv_top_margin_property->values[0] = 0;
706 dev->mode_config.tv_top_margin_property->values[1] = 100;
707
708 dev->mode_config.tv_bottom_margin_property =
709 drm_property_create(dev, DRM_MODE_PROP_RANGE,
710 "bottom margin", 2);
711 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
712 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
713
714 dev->mode_config.tv_mode_property =
715 drm_property_create(dev, DRM_MODE_PROP_ENUM,
716 "mode", num_modes);
717 for (i = 0; i < num_modes; i++)
718 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
719 i, modes[i]);
720
721 dev->mode_config.tv_brightness_property =
722 drm_property_create(dev, DRM_MODE_PROP_RANGE,
723 "brightness", 2);
724 dev->mode_config.tv_brightness_property->values[0] = 0;
725 dev->mode_config.tv_brightness_property->values[1] = 100;
726
727 dev->mode_config.tv_contrast_property =
728 drm_property_create(dev, DRM_MODE_PROP_RANGE,
729 "contrast", 2);
730 dev->mode_config.tv_contrast_property->values[0] = 0;
731 dev->mode_config.tv_contrast_property->values[1] = 100;
732
733 dev->mode_config.tv_flicker_reduction_property =
734 drm_property_create(dev, DRM_MODE_PROP_RANGE,
735 "flicker reduction", 2);
736 dev->mode_config.tv_flicker_reduction_property->values[0] = 0;
737 dev->mode_config.tv_flicker_reduction_property->values[1] = 100;
738
739 dev->mode_config.tv_overscan_property =
740 drm_property_create(dev, DRM_MODE_PROP_RANGE,
741 "overscan", 2);
742 dev->mode_config.tv_overscan_property->values[0] = 0;
743 dev->mode_config.tv_overscan_property->values[1] = 100;
744
745 dev->mode_config.tv_saturation_property =
746 drm_property_create(dev, DRM_MODE_PROP_RANGE,
747 "saturation", 2);
748 dev->mode_config.tv_saturation_property->values[0] = 0;
749 dev->mode_config.tv_saturation_property->values[1] = 100;
750
751 dev->mode_config.tv_hue_property =
752 drm_property_create(dev, DRM_MODE_PROP_RANGE,
753 "hue", 2);
754 dev->mode_config.tv_hue_property->values[0] = 0;
755 dev->mode_config.tv_hue_property->values[1] = 100;
756
757 return 0;
758 }
759 EXPORT_SYMBOL(drm_mode_create_tv_properties);
760
761 /**
762 * drm_mode_create_scaling_mode_property - create scaling mode property
763 * @dev: DRM device
764 *
765 * Called by a driver the first time it's needed, must be attached to desired
766 * connectors.
767 */
768 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
769 {
770 struct drm_property *scaling_mode;
771 int i;
772
773 if (dev->mode_config.scaling_mode_property)
774 return 0;
775
776 scaling_mode =
777 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
778 ARRAY_SIZE(drm_scaling_mode_enum_list));
779 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
780 drm_property_add_enum(scaling_mode, i,
781 drm_scaling_mode_enum_list[i].type,
782 drm_scaling_mode_enum_list[i].name);
783
784 dev->mode_config.scaling_mode_property = scaling_mode;
785
786 return 0;
787 }
788 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
789
790 /**
791 * drm_mode_create_dithering_property - create dithering property
792 * @dev: DRM device
793 *
794 * Called by a driver the first time it's needed, must be attached to desired
795 * connectors.
796 */
797 int drm_mode_create_dithering_property(struct drm_device *dev)
798 {
799 struct drm_property *dithering_mode;
800 int i;
801
802 if (dev->mode_config.dithering_mode_property)
803 return 0;
804
805 dithering_mode =
806 drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
807 ARRAY_SIZE(drm_dithering_mode_enum_list));
808 for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
809 drm_property_add_enum(dithering_mode, i,
810 drm_dithering_mode_enum_list[i].type,
811 drm_dithering_mode_enum_list[i].name);
812 dev->mode_config.dithering_mode_property = dithering_mode;
813
814 return 0;
815 }
816 EXPORT_SYMBOL(drm_mode_create_dithering_property);
817
818 /**
819 * drm_mode_config_init - initialize DRM mode_configuration structure
820 * @dev: DRM device
821 *
822 * LOCKING:
823 * None, should happen single threaded at init time.
824 *
825 * Initialize @dev's mode_config structure, used for tracking the graphics
826 * configuration of @dev.
827 */
828 void drm_mode_config_init(struct drm_device *dev)
829 {
830 mutex_init(&dev->mode_config.mutex);
831 mutex_init(&dev->mode_config.idr_mutex);
832 INIT_LIST_HEAD(&dev->mode_config.fb_list);
833 INIT_LIST_HEAD(&dev->mode_config.fb_kernel_list);
834 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
835 INIT_LIST_HEAD(&dev->mode_config.connector_list);
836 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
837 INIT_LIST_HEAD(&dev->mode_config.property_list);
838 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
839 idr_init(&dev->mode_config.crtc_idr);
840
841 mutex_lock(&dev->mode_config.mutex);
842 drm_mode_create_standard_connector_properties(dev);
843 mutex_unlock(&dev->mode_config.mutex);
844
845 /* Just to be sure */
846 dev->mode_config.num_fb = 0;
847 dev->mode_config.num_connector = 0;
848 dev->mode_config.num_crtc = 0;
849 dev->mode_config.num_encoder = 0;
850 }
851 EXPORT_SYMBOL(drm_mode_config_init);
852
853 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
854 {
855 uint32_t total_objects = 0;
856
857 total_objects += dev->mode_config.num_crtc;
858 total_objects += dev->mode_config.num_connector;
859 total_objects += dev->mode_config.num_encoder;
860
861 if (total_objects == 0)
862 return -EINVAL;
863
864 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
865 if (!group->id_list)
866 return -ENOMEM;
867
868 group->num_crtcs = 0;
869 group->num_connectors = 0;
870 group->num_encoders = 0;
871 return 0;
872 }
873
874 int drm_mode_group_init_legacy_group(struct drm_device *dev,
875 struct drm_mode_group *group)
876 {
877 struct drm_crtc *crtc;
878 struct drm_encoder *encoder;
879 struct drm_connector *connector;
880 int ret;
881
882 if ((ret = drm_mode_group_init(dev, group)))
883 return ret;
884
885 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
886 group->id_list[group->num_crtcs++] = crtc->base.id;
887
888 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
889 group->id_list[group->num_crtcs + group->num_encoders++] =
890 encoder->base.id;
891
892 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
893 group->id_list[group->num_crtcs + group->num_encoders +
894 group->num_connectors++] = connector->base.id;
895
896 return 0;
897 }
898
899 /**
900 * drm_mode_config_cleanup - free up DRM mode_config info
901 * @dev: DRM device
902 *
903 * LOCKING:
904 * Caller must hold mode config lock.
905 *
906 * Free up all the connectors and CRTCs associated with this DRM device, then
907 * free up the framebuffers and associated buffer objects.
908 *
909 * FIXME: cleanup any dangling user buffer objects too
910 */
911 void drm_mode_config_cleanup(struct drm_device *dev)
912 {
913 struct drm_connector *connector, *ot;
914 struct drm_crtc *crtc, *ct;
915 struct drm_encoder *encoder, *enct;
916 struct drm_framebuffer *fb, *fbt;
917 struct drm_property *property, *pt;
918
919 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
920 head) {
921 encoder->funcs->destroy(encoder);
922 }
923
924 list_for_each_entry_safe(connector, ot,
925 &dev->mode_config.connector_list, head) {
926 connector->funcs->destroy(connector);
927 }
928
929 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
930 head) {
931 drm_property_destroy(dev, property);
932 }
933
934 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
935 fb->funcs->destroy(fb);
936 }
937
938 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
939 crtc->funcs->destroy(crtc);
940 }
941
942 }
943 EXPORT_SYMBOL(drm_mode_config_cleanup);
944
945 /**
946 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
947 * @out: drm_mode_modeinfo struct to return to the user
948 * @in: drm_display_mode to use
949 *
950 * LOCKING:
951 * None.
952 *
953 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
954 * the user.
955 */
956 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
957 struct drm_display_mode *in)
958 {
959 out->clock = in->clock;
960 out->hdisplay = in->hdisplay;
961 out->hsync_start = in->hsync_start;
962 out->hsync_end = in->hsync_end;
963 out->htotal = in->htotal;
964 out->hskew = in->hskew;
965 out->vdisplay = in->vdisplay;
966 out->vsync_start = in->vsync_start;
967 out->vsync_end = in->vsync_end;
968 out->vtotal = in->vtotal;
969 out->vscan = in->vscan;
970 out->vrefresh = in->vrefresh;
971 out->flags = in->flags;
972 out->type = in->type;
973 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
974 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
975 }
976
977 /**
978 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
979 * @out: drm_display_mode to return to the user
980 * @in: drm_mode_modeinfo to use
981 *
982 * LOCKING:
983 * None.
984 *
985 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
986 * the caller.
987 */
988 void drm_crtc_convert_umode(struct drm_display_mode *out,
989 struct drm_mode_modeinfo *in)
990 {
991 out->clock = in->clock;
992 out->hdisplay = in->hdisplay;
993 out->hsync_start = in->hsync_start;
994 out->hsync_end = in->hsync_end;
995 out->htotal = in->htotal;
996 out->hskew = in->hskew;
997 out->vdisplay = in->vdisplay;
998 out->vsync_start = in->vsync_start;
999 out->vsync_end = in->vsync_end;
1000 out->vtotal = in->vtotal;
1001 out->vscan = in->vscan;
1002 out->vrefresh = in->vrefresh;
1003 out->flags = in->flags;
1004 out->type = in->type;
1005 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1006 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1007 }
1008
1009 /**
1010 * drm_mode_getresources - get graphics configuration
1011 * @inode: inode from the ioctl
1012 * @filp: file * from the ioctl
1013 * @cmd: cmd from ioctl
1014 * @arg: arg from ioctl
1015 *
1016 * LOCKING:
1017 * Takes mode config lock.
1018 *
1019 * Construct a set of configuration description structures and return
1020 * them to the user, including CRTC, connector and framebuffer configuration.
1021 *
1022 * Called by the user via ioctl.
1023 *
1024 * RETURNS:
1025 * Zero on success, errno on failure.
1026 */
1027 int drm_mode_getresources(struct drm_device *dev, void *data,
1028 struct drm_file *file_priv)
1029 {
1030 struct drm_mode_card_res *card_res = data;
1031 struct list_head *lh;
1032 struct drm_framebuffer *fb;
1033 struct drm_connector *connector;
1034 struct drm_crtc *crtc;
1035 struct drm_encoder *encoder;
1036 int ret = 0;
1037 int connector_count = 0;
1038 int crtc_count = 0;
1039 int fb_count = 0;
1040 int encoder_count = 0;
1041 int copied = 0, i;
1042 uint32_t __user *fb_id;
1043 uint32_t __user *crtc_id;
1044 uint32_t __user *connector_id;
1045 uint32_t __user *encoder_id;
1046 struct drm_mode_group *mode_group;
1047
1048 mutex_lock(&dev->mode_config.mutex);
1049
1050 /*
1051 * For the non-control nodes we need to limit the list of resources
1052 * by IDs in the group list for this node
1053 */
1054 list_for_each(lh, &file_priv->fbs)
1055 fb_count++;
1056
1057 mode_group = &file_priv->master->minor->mode_group;
1058 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1059
1060 list_for_each(lh, &dev->mode_config.crtc_list)
1061 crtc_count++;
1062
1063 list_for_each(lh, &dev->mode_config.connector_list)
1064 connector_count++;
1065
1066 list_for_each(lh, &dev->mode_config.encoder_list)
1067 encoder_count++;
1068 } else {
1069
1070 crtc_count = mode_group->num_crtcs;
1071 connector_count = mode_group->num_connectors;
1072 encoder_count = mode_group->num_encoders;
1073 }
1074
1075 card_res->max_height = dev->mode_config.max_height;
1076 card_res->min_height = dev->mode_config.min_height;
1077 card_res->max_width = dev->mode_config.max_width;
1078 card_res->min_width = dev->mode_config.min_width;
1079
1080 /* handle this in 4 parts */
1081 /* FBs */
1082 if (card_res->count_fbs >= fb_count) {
1083 copied = 0;
1084 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1085 list_for_each_entry(fb, &file_priv->fbs, head) {
1086 if (put_user(fb->base.id, fb_id + copied)) {
1087 ret = -EFAULT;
1088 goto out;
1089 }
1090 copied++;
1091 }
1092 }
1093 card_res->count_fbs = fb_count;
1094
1095 /* CRTCs */
1096 if (card_res->count_crtcs >= crtc_count) {
1097 copied = 0;
1098 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1099 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1100 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1101 head) {
1102 DRM_DEBUG_KMS("CRTC ID is %d\n", crtc->base.id);
1103 if (put_user(crtc->base.id, crtc_id + copied)) {
1104 ret = -EFAULT;
1105 goto out;
1106 }
1107 copied++;
1108 }
1109 } else {
1110 for (i = 0; i < mode_group->num_crtcs; i++) {
1111 if (put_user(mode_group->id_list[i],
1112 crtc_id + copied)) {
1113 ret = -EFAULT;
1114 goto out;
1115 }
1116 copied++;
1117 }
1118 }
1119 }
1120 card_res->count_crtcs = crtc_count;
1121
1122 /* Encoders */
1123 if (card_res->count_encoders >= encoder_count) {
1124 copied = 0;
1125 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1126 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1127 list_for_each_entry(encoder,
1128 &dev->mode_config.encoder_list,
1129 head) {
1130 DRM_DEBUG_KMS("ENCODER ID is %d\n",
1131 encoder->base.id);
1132 if (put_user(encoder->base.id, encoder_id +
1133 copied)) {
1134 ret = -EFAULT;
1135 goto out;
1136 }
1137 copied++;
1138 }
1139 } else {
1140 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1141 if (put_user(mode_group->id_list[i],
1142 encoder_id + copied)) {
1143 ret = -EFAULT;
1144 goto out;
1145 }
1146 copied++;
1147 }
1148
1149 }
1150 }
1151 card_res->count_encoders = encoder_count;
1152
1153 /* Connectors */
1154 if (card_res->count_connectors >= connector_count) {
1155 copied = 0;
1156 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1157 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1158 list_for_each_entry(connector,
1159 &dev->mode_config.connector_list,
1160 head) {
1161 DRM_DEBUG_KMS("CONNECTOR ID is %d\n",
1162 connector->base.id);
1163 if (put_user(connector->base.id,
1164 connector_id + copied)) {
1165 ret = -EFAULT;
1166 goto out;
1167 }
1168 copied++;
1169 }
1170 } else {
1171 int start = mode_group->num_crtcs +
1172 mode_group->num_encoders;
1173 for (i = start; i < start + mode_group->num_connectors; i++) {
1174 if (put_user(mode_group->id_list[i],
1175 connector_id + copied)) {
1176 ret = -EFAULT;
1177 goto out;
1178 }
1179 copied++;
1180 }
1181 }
1182 }
1183 card_res->count_connectors = connector_count;
1184
1185 DRM_DEBUG_KMS("Counted %d %d %d\n", card_res->count_crtcs,
1186 card_res->count_connectors, card_res->count_encoders);
1187
1188 out:
1189 mutex_unlock(&dev->mode_config.mutex);
1190 return ret;
1191 }
1192
1193 /**
1194 * drm_mode_getcrtc - get CRTC configuration
1195 * @inode: inode from the ioctl
1196 * @filp: file * from the ioctl
1197 * @cmd: cmd from ioctl
1198 * @arg: arg from ioctl
1199 *
1200 * LOCKING:
1201 * Caller? (FIXME)
1202 *
1203 * Construct a CRTC configuration structure to return to the user.
1204 *
1205 * Called by the user via ioctl.
1206 *
1207 * RETURNS:
1208 * Zero on success, errno on failure.
1209 */
1210 int drm_mode_getcrtc(struct drm_device *dev,
1211 void *data, struct drm_file *file_priv)
1212 {
1213 struct drm_mode_crtc *crtc_resp = data;
1214 struct drm_crtc *crtc;
1215 struct drm_mode_object *obj;
1216 int ret = 0;
1217
1218 mutex_lock(&dev->mode_config.mutex);
1219
1220 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1221 DRM_MODE_OBJECT_CRTC);
1222 if (!obj) {
1223 ret = -EINVAL;
1224 goto out;
1225 }
1226 crtc = obj_to_crtc(obj);
1227
1228 crtc_resp->x = crtc->x;
1229 crtc_resp->y = crtc->y;
1230 crtc_resp->gamma_size = crtc->gamma_size;
1231 if (crtc->fb)
1232 crtc_resp->fb_id = crtc->fb->base.id;
1233 else
1234 crtc_resp->fb_id = 0;
1235
1236 if (crtc->enabled) {
1237
1238 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1239 crtc_resp->mode_valid = 1;
1240
1241 } else {
1242 crtc_resp->mode_valid = 0;
1243 }
1244
1245 out:
1246 mutex_unlock(&dev->mode_config.mutex);
1247 return ret;
1248 }
1249
1250 /**
1251 * drm_mode_getconnector - get connector configuration
1252 * @inode: inode from the ioctl
1253 * @filp: file * from the ioctl
1254 * @cmd: cmd from ioctl
1255 * @arg: arg from ioctl
1256 *
1257 * LOCKING:
1258 * Caller? (FIXME)
1259 *
1260 * Construct a connector configuration structure to return to the user.
1261 *
1262 * Called by the user via ioctl.
1263 *
1264 * RETURNS:
1265 * Zero on success, errno on failure.
1266 */
1267 int drm_mode_getconnector(struct drm_device *dev, void *data,
1268 struct drm_file *file_priv)
1269 {
1270 struct drm_mode_get_connector *out_resp = data;
1271 struct drm_mode_object *obj;
1272 struct drm_connector *connector;
1273 struct drm_display_mode *mode;
1274 int mode_count = 0;
1275 int props_count = 0;
1276 int encoders_count = 0;
1277 int ret = 0;
1278 int copied = 0;
1279 int i;
1280 struct drm_mode_modeinfo u_mode;
1281 struct drm_mode_modeinfo __user *mode_ptr;
1282 uint32_t __user *prop_ptr;
1283 uint64_t __user *prop_values;
1284 uint32_t __user *encoder_ptr;
1285
1286 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1287
1288 DRM_DEBUG_KMS("connector id %d:\n", out_resp->connector_id);
1289
1290 mutex_lock(&dev->mode_config.mutex);
1291
1292 obj = drm_mode_object_find(dev, out_resp->connector_id,
1293 DRM_MODE_OBJECT_CONNECTOR);
1294 if (!obj) {
1295 ret = -EINVAL;
1296 goto out;
1297 }
1298 connector = obj_to_connector(obj);
1299
1300 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1301 if (connector->property_ids[i] != 0) {
1302 props_count++;
1303 }
1304 }
1305
1306 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1307 if (connector->encoder_ids[i] != 0) {
1308 encoders_count++;
1309 }
1310 }
1311
1312 if (out_resp->count_modes == 0) {
1313 connector->funcs->fill_modes(connector,
1314 dev->mode_config.max_width,
1315 dev->mode_config.max_height);
1316 }
1317
1318 /* delayed so we get modes regardless of pre-fill_modes state */
1319 list_for_each_entry(mode, &connector->modes, head)
1320 mode_count++;
1321
1322 out_resp->connector_id = connector->base.id;
1323 out_resp->connector_type = connector->connector_type;
1324 out_resp->connector_type_id = connector->connector_type_id;
1325 out_resp->mm_width = connector->display_info.width_mm;
1326 out_resp->mm_height = connector->display_info.height_mm;
1327 out_resp->subpixel = connector->display_info.subpixel_order;
1328 out_resp->connection = connector->status;
1329 if (connector->encoder)
1330 out_resp->encoder_id = connector->encoder->base.id;
1331 else
1332 out_resp->encoder_id = 0;
1333
1334 /*
1335 * This ioctl is called twice, once to determine how much space is
1336 * needed, and the 2nd time to fill it.
1337 */
1338 if ((out_resp->count_modes >= mode_count) && mode_count) {
1339 copied = 0;
1340 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1341 list_for_each_entry(mode, &connector->modes, head) {
1342 drm_crtc_convert_to_umode(&u_mode, mode);
1343 if (copy_to_user(mode_ptr + copied,
1344 &u_mode, sizeof(u_mode))) {
1345 ret = -EFAULT;
1346 goto out;
1347 }
1348 copied++;
1349 }
1350 }
1351 out_resp->count_modes = mode_count;
1352
1353 if ((out_resp->count_props >= props_count) && props_count) {
1354 copied = 0;
1355 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1356 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1357 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1358 if (connector->property_ids[i] != 0) {
1359 if (put_user(connector->property_ids[i],
1360 prop_ptr + copied)) {
1361 ret = -EFAULT;
1362 goto out;
1363 }
1364
1365 if (put_user(connector->property_values[i],
1366 prop_values + copied)) {
1367 ret = -EFAULT;
1368 goto out;
1369 }
1370 copied++;
1371 }
1372 }
1373 }
1374 out_resp->count_props = props_count;
1375
1376 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1377 copied = 0;
1378 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1379 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1380 if (connector->encoder_ids[i] != 0) {
1381 if (put_user(connector->encoder_ids[i],
1382 encoder_ptr + copied)) {
1383 ret = -EFAULT;
1384 goto out;
1385 }
1386 copied++;
1387 }
1388 }
1389 }
1390 out_resp->count_encoders = encoders_count;
1391
1392 out:
1393 mutex_unlock(&dev->mode_config.mutex);
1394 return ret;
1395 }
1396
1397 int drm_mode_getencoder(struct drm_device *dev, void *data,
1398 struct drm_file *file_priv)
1399 {
1400 struct drm_mode_get_encoder *enc_resp = data;
1401 struct drm_mode_object *obj;
1402 struct drm_encoder *encoder;
1403 int ret = 0;
1404
1405 mutex_lock(&dev->mode_config.mutex);
1406 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1407 DRM_MODE_OBJECT_ENCODER);
1408 if (!obj) {
1409 ret = -EINVAL;
1410 goto out;
1411 }
1412 encoder = obj_to_encoder(obj);
1413
1414 if (encoder->crtc)
1415 enc_resp->crtc_id = encoder->crtc->base.id;
1416 else
1417 enc_resp->crtc_id = 0;
1418 enc_resp->encoder_type = encoder->encoder_type;
1419 enc_resp->encoder_id = encoder->base.id;
1420 enc_resp->possible_crtcs = encoder->possible_crtcs;
1421 enc_resp->possible_clones = encoder->possible_clones;
1422
1423 out:
1424 mutex_unlock(&dev->mode_config.mutex);
1425 return ret;
1426 }
1427
1428 /**
1429 * drm_mode_setcrtc - set CRTC configuration
1430 * @inode: inode from the ioctl
1431 * @filp: file * from the ioctl
1432 * @cmd: cmd from ioctl
1433 * @arg: arg from ioctl
1434 *
1435 * LOCKING:
1436 * Caller? (FIXME)
1437 *
1438 * Build a new CRTC configuration based on user request.
1439 *
1440 * Called by the user via ioctl.
1441 *
1442 * RETURNS:
1443 * Zero on success, errno on failure.
1444 */
1445 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1446 struct drm_file *file_priv)
1447 {
1448 struct drm_mode_config *config = &dev->mode_config;
1449 struct drm_mode_crtc *crtc_req = data;
1450 struct drm_mode_object *obj;
1451 struct drm_crtc *crtc, *crtcfb;
1452 struct drm_connector **connector_set = NULL, *connector;
1453 struct drm_framebuffer *fb = NULL;
1454 struct drm_display_mode *mode = NULL;
1455 struct drm_mode_set set;
1456 uint32_t __user *set_connectors_ptr;
1457 int ret = 0;
1458 int i;
1459
1460 mutex_lock(&dev->mode_config.mutex);
1461 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1462 DRM_MODE_OBJECT_CRTC);
1463 if (!obj) {
1464 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1465 ret = -EINVAL;
1466 goto out;
1467 }
1468 crtc = obj_to_crtc(obj);
1469
1470 if (crtc_req->mode_valid) {
1471 /* If we have a mode we need a framebuffer. */
1472 /* If we pass -1, set the mode with the currently bound fb */
1473 if (crtc_req->fb_id == -1) {
1474 list_for_each_entry(crtcfb,
1475 &dev->mode_config.crtc_list, head) {
1476 if (crtcfb == crtc) {
1477 DRM_DEBUG_KMS("Using current fb for "
1478 "setmode\n");
1479 fb = crtc->fb;
1480 }
1481 }
1482 } else {
1483 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1484 DRM_MODE_OBJECT_FB);
1485 if (!obj) {
1486 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1487 crtc_req->fb_id);
1488 ret = -EINVAL;
1489 goto out;
1490 }
1491 fb = obj_to_fb(obj);
1492 }
1493
1494 mode = drm_mode_create(dev);
1495 drm_crtc_convert_umode(mode, &crtc_req->mode);
1496 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1497 }
1498
1499 if (crtc_req->count_connectors == 0 && mode) {
1500 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1501 ret = -EINVAL;
1502 goto out;
1503 }
1504
1505 if (crtc_req->count_connectors > 0 && !mode && !fb) {
1506 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1507 crtc_req->count_connectors);
1508 ret = -EINVAL;
1509 goto out;
1510 }
1511
1512 if (crtc_req->count_connectors > 0) {
1513 u32 out_id;
1514
1515 /* Avoid unbounded kernel memory allocation */
1516 if (crtc_req->count_connectors > config->num_connector) {
1517 ret = -EINVAL;
1518 goto out;
1519 }
1520
1521 connector_set = kmalloc(crtc_req->count_connectors *
1522 sizeof(struct drm_connector *),
1523 GFP_KERNEL);
1524 if (!connector_set) {
1525 ret = -ENOMEM;
1526 goto out;
1527 }
1528
1529 for (i = 0; i < crtc_req->count_connectors; i++) {
1530 set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1531 if (get_user(out_id, &set_connectors_ptr[i])) {
1532 ret = -EFAULT;
1533 goto out;
1534 }
1535
1536 obj = drm_mode_object_find(dev, out_id,
1537 DRM_MODE_OBJECT_CONNECTOR);
1538 if (!obj) {
1539 DRM_DEBUG_KMS("Connector id %d unknown\n",
1540 out_id);
1541 ret = -EINVAL;
1542 goto out;
1543 }
1544 connector = obj_to_connector(obj);
1545
1546 connector_set[i] = connector;
1547 }
1548 }
1549
1550 set.crtc = crtc;
1551 set.x = crtc_req->x;
1552 set.y = crtc_req->y;
1553 set.mode = mode;
1554 set.connectors = connector_set;
1555 set.num_connectors = crtc_req->count_connectors;
1556 set.fb =fb;
1557 ret = crtc->funcs->set_config(&set);
1558
1559 out:
1560 kfree(connector_set);
1561 mutex_unlock(&dev->mode_config.mutex);
1562 return ret;
1563 }
1564
1565 int drm_mode_cursor_ioctl(struct drm_device *dev,
1566 void *data, struct drm_file *file_priv)
1567 {
1568 struct drm_mode_cursor *req = data;
1569 struct drm_mode_object *obj;
1570 struct drm_crtc *crtc;
1571 int ret = 0;
1572
1573 DRM_DEBUG_KMS("\n");
1574
1575 if (!req->flags) {
1576 DRM_ERROR("no operation set\n");
1577 return -EINVAL;
1578 }
1579
1580 mutex_lock(&dev->mode_config.mutex);
1581 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1582 if (!obj) {
1583 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1584 ret = -EINVAL;
1585 goto out;
1586 }
1587 crtc = obj_to_crtc(obj);
1588
1589 if (req->flags & DRM_MODE_CURSOR_BO) {
1590 if (!crtc->funcs->cursor_set) {
1591 DRM_ERROR("crtc does not support cursor\n");
1592 ret = -ENXIO;
1593 goto out;
1594 }
1595 /* Turns off the cursor if handle is 0 */
1596 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1597 req->width, req->height);
1598 }
1599
1600 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1601 if (crtc->funcs->cursor_move) {
1602 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1603 } else {
1604 DRM_ERROR("crtc does not support cursor\n");
1605 ret = -EFAULT;
1606 goto out;
1607 }
1608 }
1609 out:
1610 mutex_unlock(&dev->mode_config.mutex);
1611 return ret;
1612 }
1613
1614 /**
1615 * drm_mode_addfb - add an FB to the graphics configuration
1616 * @inode: inode from the ioctl
1617 * @filp: file * from the ioctl
1618 * @cmd: cmd from ioctl
1619 * @arg: arg from ioctl
1620 *
1621 * LOCKING:
1622 * Takes mode config lock.
1623 *
1624 * Add a new FB to the specified CRTC, given a user request.
1625 *
1626 * Called by the user via ioctl.
1627 *
1628 * RETURNS:
1629 * Zero on success, errno on failure.
1630 */
1631 int drm_mode_addfb(struct drm_device *dev,
1632 void *data, struct drm_file *file_priv)
1633 {
1634 struct drm_mode_fb_cmd *r = data;
1635 struct drm_mode_config *config = &dev->mode_config;
1636 struct drm_framebuffer *fb;
1637 int ret = 0;
1638
1639 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1640 DRM_ERROR("mode new framebuffer width not within limits\n");
1641 return -EINVAL;
1642 }
1643 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1644 DRM_ERROR("mode new framebuffer height not within limits\n");
1645 return -EINVAL;
1646 }
1647
1648 mutex_lock(&dev->mode_config.mutex);
1649
1650 /* TODO check buffer is sufficently large */
1651 /* TODO setup destructor callback */
1652
1653 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1654 if (!fb) {
1655 DRM_ERROR("could not create framebuffer\n");
1656 ret = -EINVAL;
1657 goto out;
1658 }
1659
1660 r->fb_id = fb->base.id;
1661 list_add(&fb->filp_head, &file_priv->fbs);
1662
1663 out:
1664 mutex_unlock(&dev->mode_config.mutex);
1665 return ret;
1666 }
1667
1668 /**
1669 * drm_mode_rmfb - remove an FB from the configuration
1670 * @inode: inode from the ioctl
1671 * @filp: file * from the ioctl
1672 * @cmd: cmd from ioctl
1673 * @arg: arg from ioctl
1674 *
1675 * LOCKING:
1676 * Takes mode config lock.
1677 *
1678 * Remove the FB specified by the user.
1679 *
1680 * Called by the user via ioctl.
1681 *
1682 * RETURNS:
1683 * Zero on success, errno on failure.
1684 */
1685 int drm_mode_rmfb(struct drm_device *dev,
1686 void *data, struct drm_file *file_priv)
1687 {
1688 struct drm_mode_object *obj;
1689 struct drm_framebuffer *fb = NULL;
1690 struct drm_framebuffer *fbl = NULL;
1691 uint32_t *id = data;
1692 int ret = 0;
1693 int found = 0;
1694
1695 mutex_lock(&dev->mode_config.mutex);
1696 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1697 /* TODO check that we realy get a framebuffer back. */
1698 if (!obj) {
1699 DRM_ERROR("mode invalid framebuffer id\n");
1700 ret = -EINVAL;
1701 goto out;
1702 }
1703 fb = obj_to_fb(obj);
1704
1705 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1706 if (fb == fbl)
1707 found = 1;
1708
1709 if (!found) {
1710 DRM_ERROR("tried to remove a fb that we didn't own\n");
1711 ret = -EINVAL;
1712 goto out;
1713 }
1714
1715 /* TODO release all crtc connected to the framebuffer */
1716 /* TODO unhock the destructor from the buffer object */
1717
1718 list_del(&fb->filp_head);
1719 fb->funcs->destroy(fb);
1720
1721 out:
1722 mutex_unlock(&dev->mode_config.mutex);
1723 return ret;
1724 }
1725
1726 /**
1727 * drm_mode_getfb - get FB info
1728 * @inode: inode from the ioctl
1729 * @filp: file * from the ioctl
1730 * @cmd: cmd from ioctl
1731 * @arg: arg from ioctl
1732 *
1733 * LOCKING:
1734 * Caller? (FIXME)
1735 *
1736 * Lookup the FB given its ID and return info about it.
1737 *
1738 * Called by the user via ioctl.
1739 *
1740 * RETURNS:
1741 * Zero on success, errno on failure.
1742 */
1743 int drm_mode_getfb(struct drm_device *dev,
1744 void *data, struct drm_file *file_priv)
1745 {
1746 struct drm_mode_fb_cmd *r = data;
1747 struct drm_mode_object *obj;
1748 struct drm_framebuffer *fb;
1749 int ret = 0;
1750
1751 mutex_lock(&dev->mode_config.mutex);
1752 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1753 if (!obj) {
1754 DRM_ERROR("invalid framebuffer id\n");
1755 ret = -EINVAL;
1756 goto out;
1757 }
1758 fb = obj_to_fb(obj);
1759
1760 r->height = fb->height;
1761 r->width = fb->width;
1762 r->depth = fb->depth;
1763 r->bpp = fb->bits_per_pixel;
1764 r->pitch = fb->pitch;
1765 fb->funcs->create_handle(fb, file_priv, &r->handle);
1766
1767 out:
1768 mutex_unlock(&dev->mode_config.mutex);
1769 return ret;
1770 }
1771
1772 /**
1773 * drm_fb_release - remove and free the FBs on this file
1774 * @filp: file * from the ioctl
1775 *
1776 * LOCKING:
1777 * Takes mode config lock.
1778 *
1779 * Destroy all the FBs associated with @filp.
1780 *
1781 * Called by the user via ioctl.
1782 *
1783 * RETURNS:
1784 * Zero on success, errno on failure.
1785 */
1786 void drm_fb_release(struct drm_file *priv)
1787 {
1788 struct drm_device *dev = priv->minor->dev;
1789 struct drm_framebuffer *fb, *tfb;
1790
1791 mutex_lock(&dev->mode_config.mutex);
1792 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1793 list_del(&fb->filp_head);
1794 fb->funcs->destroy(fb);
1795 }
1796 mutex_unlock(&dev->mode_config.mutex);
1797 }
1798
1799 /**
1800 * drm_mode_attachmode - add a mode to the user mode list
1801 * @dev: DRM device
1802 * @connector: connector to add the mode to
1803 * @mode: mode to add
1804 *
1805 * Add @mode to @connector's user mode list.
1806 */
1807 static int drm_mode_attachmode(struct drm_device *dev,
1808 struct drm_connector *connector,
1809 struct drm_display_mode *mode)
1810 {
1811 int ret = 0;
1812
1813 list_add_tail(&mode->head, &connector->user_modes);
1814 return ret;
1815 }
1816
1817 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1818 struct drm_display_mode *mode)
1819 {
1820 struct drm_connector *connector;
1821 int ret = 0;
1822 struct drm_display_mode *dup_mode;
1823 int need_dup = 0;
1824 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1825 if (!connector->encoder)
1826 break;
1827 if (connector->encoder->crtc == crtc) {
1828 if (need_dup)
1829 dup_mode = drm_mode_duplicate(dev, mode);
1830 else
1831 dup_mode = mode;
1832 ret = drm_mode_attachmode(dev, connector, dup_mode);
1833 if (ret)
1834 return ret;
1835 need_dup = 1;
1836 }
1837 }
1838 return 0;
1839 }
1840 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1841
1842 static int drm_mode_detachmode(struct drm_device *dev,
1843 struct drm_connector *connector,
1844 struct drm_display_mode *mode)
1845 {
1846 int found = 0;
1847 int ret = 0;
1848 struct drm_display_mode *match_mode, *t;
1849
1850 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1851 if (drm_mode_equal(match_mode, mode)) {
1852 list_del(&match_mode->head);
1853 drm_mode_destroy(dev, match_mode);
1854 found = 1;
1855 break;
1856 }
1857 }
1858
1859 if (!found)
1860 ret = -EINVAL;
1861
1862 return ret;
1863 }
1864
1865 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1866 {
1867 struct drm_connector *connector;
1868
1869 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1870 drm_mode_detachmode(dev, connector, mode);
1871 }
1872 return 0;
1873 }
1874 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1875
1876 /**
1877 * drm_fb_attachmode - Attach a user mode to an connector
1878 * @inode: inode from the ioctl
1879 * @filp: file * from the ioctl
1880 * @cmd: cmd from ioctl
1881 * @arg: arg from ioctl
1882 *
1883 * This attaches a user specified mode to an connector.
1884 * Called by the user via ioctl.
1885 *
1886 * RETURNS:
1887 * Zero on success, errno on failure.
1888 */
1889 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1890 void *data, struct drm_file *file_priv)
1891 {
1892 struct drm_mode_mode_cmd *mode_cmd = data;
1893 struct drm_connector *connector;
1894 struct drm_display_mode *mode;
1895 struct drm_mode_object *obj;
1896 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1897 int ret = 0;
1898
1899 mutex_lock(&dev->mode_config.mutex);
1900
1901 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1902 if (!obj) {
1903 ret = -EINVAL;
1904 goto out;
1905 }
1906 connector = obj_to_connector(obj);
1907
1908 mode = drm_mode_create(dev);
1909 if (!mode) {
1910 ret = -ENOMEM;
1911 goto out;
1912 }
1913
1914 drm_crtc_convert_umode(mode, umode);
1915
1916 ret = drm_mode_attachmode(dev, connector, mode);
1917 out:
1918 mutex_unlock(&dev->mode_config.mutex);
1919 return ret;
1920 }
1921
1922
1923 /**
1924 * drm_fb_detachmode - Detach a user specified mode from an connector
1925 * @inode: inode from the ioctl
1926 * @filp: file * from the ioctl
1927 * @cmd: cmd from ioctl
1928 * @arg: arg from ioctl
1929 *
1930 * Called by the user via ioctl.
1931 *
1932 * RETURNS:
1933 * Zero on success, errno on failure.
1934 */
1935 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1936 void *data, struct drm_file *file_priv)
1937 {
1938 struct drm_mode_object *obj;
1939 struct drm_mode_mode_cmd *mode_cmd = data;
1940 struct drm_connector *connector;
1941 struct drm_display_mode mode;
1942 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1943 int ret = 0;
1944
1945 mutex_lock(&dev->mode_config.mutex);
1946
1947 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1948 if (!obj) {
1949 ret = -EINVAL;
1950 goto out;
1951 }
1952 connector = obj_to_connector(obj);
1953
1954 drm_crtc_convert_umode(&mode, umode);
1955 ret = drm_mode_detachmode(dev, connector, &mode);
1956 out:
1957 mutex_unlock(&dev->mode_config.mutex);
1958 return ret;
1959 }
1960
1961 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1962 const char *name, int num_values)
1963 {
1964 struct drm_property *property = NULL;
1965
1966 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
1967 if (!property)
1968 return NULL;
1969
1970 if (num_values) {
1971 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1972 if (!property->values)
1973 goto fail;
1974 }
1975
1976 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
1977 property->flags = flags;
1978 property->num_values = num_values;
1979 INIT_LIST_HEAD(&property->enum_blob_list);
1980
1981 if (name)
1982 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1983
1984 list_add_tail(&property->head, &dev->mode_config.property_list);
1985 return property;
1986 fail:
1987 kfree(property);
1988 return NULL;
1989 }
1990 EXPORT_SYMBOL(drm_property_create);
1991
1992 int drm_property_add_enum(struct drm_property *property, int index,
1993 uint64_t value, const char *name)
1994 {
1995 struct drm_property_enum *prop_enum;
1996
1997 if (!(property->flags & DRM_MODE_PROP_ENUM))
1998 return -EINVAL;
1999
2000 if (!list_empty(&property->enum_blob_list)) {
2001 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2002 if (prop_enum->value == value) {
2003 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2004 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2005 return 0;
2006 }
2007 }
2008 }
2009
2010 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2011 if (!prop_enum)
2012 return -ENOMEM;
2013
2014 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2015 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2016 prop_enum->value = value;
2017
2018 property->values[index] = value;
2019 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2020 return 0;
2021 }
2022 EXPORT_SYMBOL(drm_property_add_enum);
2023
2024 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2025 {
2026 struct drm_property_enum *prop_enum, *pt;
2027
2028 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2029 list_del(&prop_enum->head);
2030 kfree(prop_enum);
2031 }
2032
2033 if (property->num_values)
2034 kfree(property->values);
2035 drm_mode_object_put(dev, &property->base);
2036 list_del(&property->head);
2037 kfree(property);
2038 }
2039 EXPORT_SYMBOL(drm_property_destroy);
2040
2041 int drm_connector_attach_property(struct drm_connector *connector,
2042 struct drm_property *property, uint64_t init_val)
2043 {
2044 int i;
2045
2046 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2047 if (connector->property_ids[i] == 0) {
2048 connector->property_ids[i] = property->base.id;
2049 connector->property_values[i] = init_val;
2050 break;
2051 }
2052 }
2053
2054 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2055 return -EINVAL;
2056 return 0;
2057 }
2058 EXPORT_SYMBOL(drm_connector_attach_property);
2059
2060 int drm_connector_property_set_value(struct drm_connector *connector,
2061 struct drm_property *property, uint64_t value)
2062 {
2063 int i;
2064
2065 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2066 if (connector->property_ids[i] == property->base.id) {
2067 connector->property_values[i] = value;
2068 break;
2069 }
2070 }
2071
2072 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2073 return -EINVAL;
2074 return 0;
2075 }
2076 EXPORT_SYMBOL(drm_connector_property_set_value);
2077
2078 int drm_connector_property_get_value(struct drm_connector *connector,
2079 struct drm_property *property, uint64_t *val)
2080 {
2081 int i;
2082
2083 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2084 if (connector->property_ids[i] == property->base.id) {
2085 *val = connector->property_values[i];
2086 break;
2087 }
2088 }
2089
2090 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2091 return -EINVAL;
2092 return 0;
2093 }
2094 EXPORT_SYMBOL(drm_connector_property_get_value);
2095
2096 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2097 void *data, struct drm_file *file_priv)
2098 {
2099 struct drm_mode_object *obj;
2100 struct drm_mode_get_property *out_resp = data;
2101 struct drm_property *property;
2102 int enum_count = 0;
2103 int blob_count = 0;
2104 int value_count = 0;
2105 int ret = 0, i;
2106 int copied;
2107 struct drm_property_enum *prop_enum;
2108 struct drm_mode_property_enum __user *enum_ptr;
2109 struct drm_property_blob *prop_blob;
2110 uint32_t *blob_id_ptr;
2111 uint64_t __user *values_ptr;
2112 uint32_t __user *blob_length_ptr;
2113
2114 mutex_lock(&dev->mode_config.mutex);
2115 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2116 if (!obj) {
2117 ret = -EINVAL;
2118 goto done;
2119 }
2120 property = obj_to_property(obj);
2121
2122 if (property->flags & DRM_MODE_PROP_ENUM) {
2123 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2124 enum_count++;
2125 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2126 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2127 blob_count++;
2128 }
2129
2130 value_count = property->num_values;
2131
2132 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2133 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2134 out_resp->flags = property->flags;
2135
2136 if ((out_resp->count_values >= value_count) && value_count) {
2137 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2138 for (i = 0; i < value_count; i++) {
2139 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2140 ret = -EFAULT;
2141 goto done;
2142 }
2143 }
2144 }
2145 out_resp->count_values = value_count;
2146
2147 if (property->flags & DRM_MODE_PROP_ENUM) {
2148 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2149 copied = 0;
2150 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2151 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2152
2153 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2154 ret = -EFAULT;
2155 goto done;
2156 }
2157
2158 if (copy_to_user(&enum_ptr[copied].name,
2159 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2160 ret = -EFAULT;
2161 goto done;
2162 }
2163 copied++;
2164 }
2165 }
2166 out_resp->count_enum_blobs = enum_count;
2167 }
2168
2169 if (property->flags & DRM_MODE_PROP_BLOB) {
2170 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2171 copied = 0;
2172 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2173 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2174
2175 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2176 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2177 ret = -EFAULT;
2178 goto done;
2179 }
2180
2181 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2182 ret = -EFAULT;
2183 goto done;
2184 }
2185
2186 copied++;
2187 }
2188 }
2189 out_resp->count_enum_blobs = blob_count;
2190 }
2191 done:
2192 mutex_unlock(&dev->mode_config.mutex);
2193 return ret;
2194 }
2195
2196 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2197 void *data)
2198 {
2199 struct drm_property_blob *blob;
2200
2201 if (!length || !data)
2202 return NULL;
2203
2204 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2205 if (!blob)
2206 return NULL;
2207
2208 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2209 blob->length = length;
2210
2211 memcpy(blob->data, data, length);
2212
2213 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2214
2215 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2216 return blob;
2217 }
2218
2219 static void drm_property_destroy_blob(struct drm_device *dev,
2220 struct drm_property_blob *blob)
2221 {
2222 drm_mode_object_put(dev, &blob->base);
2223 list_del(&blob->head);
2224 kfree(blob);
2225 }
2226
2227 int drm_mode_getblob_ioctl(struct drm_device *dev,
2228 void *data, struct drm_file *file_priv)
2229 {
2230 struct drm_mode_object *obj;
2231 struct drm_mode_get_blob *out_resp = data;
2232 struct drm_property_blob *blob;
2233 int ret = 0;
2234 void *blob_ptr;
2235
2236 mutex_lock(&dev->mode_config.mutex);
2237 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2238 if (!obj) {
2239 ret = -EINVAL;
2240 goto done;
2241 }
2242 blob = obj_to_blob(obj);
2243
2244 if (out_resp->length == blob->length) {
2245 blob_ptr = (void *)(unsigned long)out_resp->data;
2246 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2247 ret = -EFAULT;
2248 goto done;
2249 }
2250 }
2251 out_resp->length = blob->length;
2252
2253 done:
2254 mutex_unlock(&dev->mode_config.mutex);
2255 return ret;
2256 }
2257
2258 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2259 struct edid *edid)
2260 {
2261 struct drm_device *dev = connector->dev;
2262 int ret = 0;
2263
2264 if (connector->edid_blob_ptr)
2265 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2266
2267 /* Delete edid, when there is none. */
2268 if (!edid) {
2269 connector->edid_blob_ptr = NULL;
2270 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2271 return ret;
2272 }
2273
2274 connector->edid_blob_ptr = drm_property_create_blob(connector->dev, 128, edid);
2275
2276 ret = drm_connector_property_set_value(connector,
2277 dev->mode_config.edid_property,
2278 connector->edid_blob_ptr->base.id);
2279
2280 return ret;
2281 }
2282 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2283
2284 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2285 void *data, struct drm_file *file_priv)
2286 {
2287 struct drm_mode_connector_set_property *out_resp = data;
2288 struct drm_mode_object *obj;
2289 struct drm_property *property;
2290 struct drm_connector *connector;
2291 int ret = -EINVAL;
2292 int i;
2293
2294 mutex_lock(&dev->mode_config.mutex);
2295
2296 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2297 if (!obj) {
2298 goto out;
2299 }
2300 connector = obj_to_connector(obj);
2301
2302 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2303 if (connector->property_ids[i] == out_resp->prop_id)
2304 break;
2305 }
2306
2307 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2308 goto out;
2309 }
2310
2311 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2312 if (!obj) {
2313 goto out;
2314 }
2315 property = obj_to_property(obj);
2316
2317 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2318 goto out;
2319
2320 if (property->flags & DRM_MODE_PROP_RANGE) {
2321 if (out_resp->value < property->values[0])
2322 goto out;
2323
2324 if (out_resp->value > property->values[1])
2325 goto out;
2326 } else {
2327 int found = 0;
2328 for (i = 0; i < property->num_values; i++) {
2329 if (property->values[i] == out_resp->value) {
2330 found = 1;
2331 break;
2332 }
2333 }
2334 if (!found) {
2335 goto out;
2336 }
2337 }
2338
2339 /* Do DPMS ourselves */
2340 if (property == connector->dev->mode_config.dpms_property) {
2341 if (connector->funcs->dpms)
2342 (*connector->funcs->dpms)(connector, (int) out_resp->value);
2343 ret = 0;
2344 } else if (connector->funcs->set_property)
2345 ret = connector->funcs->set_property(connector, property, out_resp->value);
2346
2347 /* store the property value if succesful */
2348 if (!ret)
2349 drm_connector_property_set_value(connector, property, out_resp->value);
2350 out:
2351 mutex_unlock(&dev->mode_config.mutex);
2352 return ret;
2353 }
2354
2355 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2356 struct drm_encoder *encoder)
2357 {
2358 int i;
2359
2360 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2361 if (connector->encoder_ids[i] == 0) {
2362 connector->encoder_ids[i] = encoder->base.id;
2363 return 0;
2364 }
2365 }
2366 return -ENOMEM;
2367 }
2368 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2369
2370 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2371 struct drm_encoder *encoder)
2372 {
2373 int i;
2374 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2375 if (connector->encoder_ids[i] == encoder->base.id) {
2376 connector->encoder_ids[i] = 0;
2377 if (connector->encoder == encoder)
2378 connector->encoder = NULL;
2379 break;
2380 }
2381 }
2382 }
2383 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2384
2385 bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2386 int gamma_size)
2387 {
2388 crtc->gamma_size = gamma_size;
2389
2390 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2391 if (!crtc->gamma_store) {
2392 crtc->gamma_size = 0;
2393 return false;
2394 }
2395
2396 return true;
2397 }
2398 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2399
2400 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2401 void *data, struct drm_file *file_priv)
2402 {
2403 struct drm_mode_crtc_lut *crtc_lut = data;
2404 struct drm_mode_object *obj;
2405 struct drm_crtc *crtc;
2406 void *r_base, *g_base, *b_base;
2407 int size;
2408 int ret = 0;
2409
2410 mutex_lock(&dev->mode_config.mutex);
2411 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2412 if (!obj) {
2413 ret = -EINVAL;
2414 goto out;
2415 }
2416 crtc = obj_to_crtc(obj);
2417
2418 /* memcpy into gamma store */
2419 if (crtc_lut->gamma_size != crtc->gamma_size) {
2420 ret = -EINVAL;
2421 goto out;
2422 }
2423
2424 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2425 r_base = crtc->gamma_store;
2426 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2427 ret = -EFAULT;
2428 goto out;
2429 }
2430
2431 g_base = r_base + size;
2432 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2433 ret = -EFAULT;
2434 goto out;
2435 }
2436
2437 b_base = g_base + size;
2438 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2439 ret = -EFAULT;
2440 goto out;
2441 }
2442
2443 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
2444
2445 out:
2446 mutex_unlock(&dev->mode_config.mutex);
2447 return ret;
2448
2449 }
2450
2451 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2452 void *data, struct drm_file *file_priv)
2453 {
2454 struct drm_mode_crtc_lut *crtc_lut = data;
2455 struct drm_mode_object *obj;
2456 struct drm_crtc *crtc;
2457 void *r_base, *g_base, *b_base;
2458 int size;
2459 int ret = 0;
2460
2461 mutex_lock(&dev->mode_config.mutex);
2462 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2463 if (!obj) {
2464 ret = -EINVAL;
2465 goto out;
2466 }
2467 crtc = obj_to_crtc(obj);
2468
2469 /* memcpy into gamma store */
2470 if (crtc_lut->gamma_size != crtc->gamma_size) {
2471 ret = -EINVAL;
2472 goto out;
2473 }
2474
2475 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2476 r_base = crtc->gamma_store;
2477 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2478 ret = -EFAULT;
2479 goto out;
2480 }
2481
2482 g_base = r_base + size;
2483 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2484 ret = -EFAULT;
2485 goto out;
2486 }
2487
2488 b_base = g_base + size;
2489 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2490 ret = -EFAULT;
2491 goto out;
2492 }
2493 out:
2494 mutex_unlock(&dev->mode_config.mutex);
2495 return ret;
2496 }