drm: Define DRM_MODE_SUBCONNECTOR_SCART
[deliverable/linux.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 return 0;
722 }
723 EXPORT_SYMBOL(drm_mode_create_tv_properties);
724
725 /**
726 * drm_mode_create_scaling_mode_property - create scaling mode property
727 * @dev: DRM device
728 *
729 * Called by a driver the first time it's needed, must be attached to desired
730 * connectors.
731 */
732 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
733 {
734 struct drm_property *scaling_mode;
735 int i;
736
737 if (dev->mode_config.scaling_mode_property)
738 return 0;
739
740 scaling_mode =
741 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
742 ARRAY_SIZE(drm_scaling_mode_enum_list));
743 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
744 drm_property_add_enum(scaling_mode, i,
745 drm_scaling_mode_enum_list[i].type,
746 drm_scaling_mode_enum_list[i].name);
747
748 dev->mode_config.scaling_mode_property = scaling_mode;
749
750 return 0;
751 }
752 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
753
754 /**
755 * drm_mode_create_dithering_property - create dithering property
756 * @dev: DRM device
757 *
758 * Called by a driver the first time it's needed, must be attached to desired
759 * connectors.
760 */
761 int drm_mode_create_dithering_property(struct drm_device *dev)
762 {
763 struct drm_property *dithering_mode;
764 int i;
765
766 if (dev->mode_config.dithering_mode_property)
767 return 0;
768
769 dithering_mode =
770 drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
771 ARRAY_SIZE(drm_dithering_mode_enum_list));
772 for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
773 drm_property_add_enum(dithering_mode, i,
774 drm_dithering_mode_enum_list[i].type,
775 drm_dithering_mode_enum_list[i].name);
776 dev->mode_config.dithering_mode_property = dithering_mode;
777
778 return 0;
779 }
780 EXPORT_SYMBOL(drm_mode_create_dithering_property);
781
782 /**
783 * drm_mode_config_init - initialize DRM mode_configuration structure
784 * @dev: DRM device
785 *
786 * LOCKING:
787 * None, should happen single threaded at init time.
788 *
789 * Initialize @dev's mode_config structure, used for tracking the graphics
790 * configuration of @dev.
791 */
792 void drm_mode_config_init(struct drm_device *dev)
793 {
794 mutex_init(&dev->mode_config.mutex);
795 mutex_init(&dev->mode_config.idr_mutex);
796 INIT_LIST_HEAD(&dev->mode_config.fb_list);
797 INIT_LIST_HEAD(&dev->mode_config.fb_kernel_list);
798 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
799 INIT_LIST_HEAD(&dev->mode_config.connector_list);
800 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
801 INIT_LIST_HEAD(&dev->mode_config.property_list);
802 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
803 idr_init(&dev->mode_config.crtc_idr);
804
805 mutex_lock(&dev->mode_config.mutex);
806 drm_mode_create_standard_connector_properties(dev);
807 mutex_unlock(&dev->mode_config.mutex);
808
809 /* Just to be sure */
810 dev->mode_config.num_fb = 0;
811 dev->mode_config.num_connector = 0;
812 dev->mode_config.num_crtc = 0;
813 dev->mode_config.num_encoder = 0;
814 }
815 EXPORT_SYMBOL(drm_mode_config_init);
816
817 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
818 {
819 uint32_t total_objects = 0;
820
821 total_objects += dev->mode_config.num_crtc;
822 total_objects += dev->mode_config.num_connector;
823 total_objects += dev->mode_config.num_encoder;
824
825 if (total_objects == 0)
826 return -EINVAL;
827
828 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
829 if (!group->id_list)
830 return -ENOMEM;
831
832 group->num_crtcs = 0;
833 group->num_connectors = 0;
834 group->num_encoders = 0;
835 return 0;
836 }
837
838 int drm_mode_group_init_legacy_group(struct drm_device *dev,
839 struct drm_mode_group *group)
840 {
841 struct drm_crtc *crtc;
842 struct drm_encoder *encoder;
843 struct drm_connector *connector;
844 int ret;
845
846 if ((ret = drm_mode_group_init(dev, group)))
847 return ret;
848
849 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
850 group->id_list[group->num_crtcs++] = crtc->base.id;
851
852 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
853 group->id_list[group->num_crtcs + group->num_encoders++] =
854 encoder->base.id;
855
856 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
857 group->id_list[group->num_crtcs + group->num_encoders +
858 group->num_connectors++] = connector->base.id;
859
860 return 0;
861 }
862
863 /**
864 * drm_mode_config_cleanup - free up DRM mode_config info
865 * @dev: DRM device
866 *
867 * LOCKING:
868 * Caller must hold mode config lock.
869 *
870 * Free up all the connectors and CRTCs associated with this DRM device, then
871 * free up the framebuffers and associated buffer objects.
872 *
873 * FIXME: cleanup any dangling user buffer objects too
874 */
875 void drm_mode_config_cleanup(struct drm_device *dev)
876 {
877 struct drm_connector *connector, *ot;
878 struct drm_crtc *crtc, *ct;
879 struct drm_encoder *encoder, *enct;
880 struct drm_framebuffer *fb, *fbt;
881 struct drm_property *property, *pt;
882
883 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
884 head) {
885 encoder->funcs->destroy(encoder);
886 }
887
888 list_for_each_entry_safe(connector, ot,
889 &dev->mode_config.connector_list, head) {
890 connector->funcs->destroy(connector);
891 }
892
893 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
894 head) {
895 drm_property_destroy(dev, property);
896 }
897
898 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
899 fb->funcs->destroy(fb);
900 }
901
902 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
903 crtc->funcs->destroy(crtc);
904 }
905
906 }
907 EXPORT_SYMBOL(drm_mode_config_cleanup);
908
909 /**
910 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
911 * @out: drm_mode_modeinfo struct to return to the user
912 * @in: drm_display_mode to use
913 *
914 * LOCKING:
915 * None.
916 *
917 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
918 * the user.
919 */
920 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
921 struct drm_display_mode *in)
922 {
923 out->clock = in->clock;
924 out->hdisplay = in->hdisplay;
925 out->hsync_start = in->hsync_start;
926 out->hsync_end = in->hsync_end;
927 out->htotal = in->htotal;
928 out->hskew = in->hskew;
929 out->vdisplay = in->vdisplay;
930 out->vsync_start = in->vsync_start;
931 out->vsync_end = in->vsync_end;
932 out->vtotal = in->vtotal;
933 out->vscan = in->vscan;
934 out->vrefresh = in->vrefresh;
935 out->flags = in->flags;
936 out->type = in->type;
937 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
938 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
939 }
940
941 /**
942 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
943 * @out: drm_display_mode to return to the user
944 * @in: drm_mode_modeinfo to use
945 *
946 * LOCKING:
947 * None.
948 *
949 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
950 * the caller.
951 */
952 void drm_crtc_convert_umode(struct drm_display_mode *out,
953 struct drm_mode_modeinfo *in)
954 {
955 out->clock = in->clock;
956 out->hdisplay = in->hdisplay;
957 out->hsync_start = in->hsync_start;
958 out->hsync_end = in->hsync_end;
959 out->htotal = in->htotal;
960 out->hskew = in->hskew;
961 out->vdisplay = in->vdisplay;
962 out->vsync_start = in->vsync_start;
963 out->vsync_end = in->vsync_end;
964 out->vtotal = in->vtotal;
965 out->vscan = in->vscan;
966 out->vrefresh = in->vrefresh;
967 out->flags = in->flags;
968 out->type = in->type;
969 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
970 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
971 }
972
973 /**
974 * drm_mode_getresources - get graphics configuration
975 * @inode: inode from the ioctl
976 * @filp: file * from the ioctl
977 * @cmd: cmd from ioctl
978 * @arg: arg from ioctl
979 *
980 * LOCKING:
981 * Takes mode config lock.
982 *
983 * Construct a set of configuration description structures and return
984 * them to the user, including CRTC, connector and framebuffer configuration.
985 *
986 * Called by the user via ioctl.
987 *
988 * RETURNS:
989 * Zero on success, errno on failure.
990 */
991 int drm_mode_getresources(struct drm_device *dev, void *data,
992 struct drm_file *file_priv)
993 {
994 struct drm_mode_card_res *card_res = data;
995 struct list_head *lh;
996 struct drm_framebuffer *fb;
997 struct drm_connector *connector;
998 struct drm_crtc *crtc;
999 struct drm_encoder *encoder;
1000 int ret = 0;
1001 int connector_count = 0;
1002 int crtc_count = 0;
1003 int fb_count = 0;
1004 int encoder_count = 0;
1005 int copied = 0, i;
1006 uint32_t __user *fb_id;
1007 uint32_t __user *crtc_id;
1008 uint32_t __user *connector_id;
1009 uint32_t __user *encoder_id;
1010 struct drm_mode_group *mode_group;
1011
1012 mutex_lock(&dev->mode_config.mutex);
1013
1014 /*
1015 * For the non-control nodes we need to limit the list of resources
1016 * by IDs in the group list for this node
1017 */
1018 list_for_each(lh, &file_priv->fbs)
1019 fb_count++;
1020
1021 mode_group = &file_priv->master->minor->mode_group;
1022 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1023
1024 list_for_each(lh, &dev->mode_config.crtc_list)
1025 crtc_count++;
1026
1027 list_for_each(lh, &dev->mode_config.connector_list)
1028 connector_count++;
1029
1030 list_for_each(lh, &dev->mode_config.encoder_list)
1031 encoder_count++;
1032 } else {
1033
1034 crtc_count = mode_group->num_crtcs;
1035 connector_count = mode_group->num_connectors;
1036 encoder_count = mode_group->num_encoders;
1037 }
1038
1039 card_res->max_height = dev->mode_config.max_height;
1040 card_res->min_height = dev->mode_config.min_height;
1041 card_res->max_width = dev->mode_config.max_width;
1042 card_res->min_width = dev->mode_config.min_width;
1043
1044 /* handle this in 4 parts */
1045 /* FBs */
1046 if (card_res->count_fbs >= fb_count) {
1047 copied = 0;
1048 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1049 list_for_each_entry(fb, &file_priv->fbs, head) {
1050 if (put_user(fb->base.id, fb_id + copied)) {
1051 ret = -EFAULT;
1052 goto out;
1053 }
1054 copied++;
1055 }
1056 }
1057 card_res->count_fbs = fb_count;
1058
1059 /* CRTCs */
1060 if (card_res->count_crtcs >= crtc_count) {
1061 copied = 0;
1062 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1063 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1064 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1065 head) {
1066 DRM_DEBUG_KMS("CRTC ID is %d\n", crtc->base.id);
1067 if (put_user(crtc->base.id, crtc_id + copied)) {
1068 ret = -EFAULT;
1069 goto out;
1070 }
1071 copied++;
1072 }
1073 } else {
1074 for (i = 0; i < mode_group->num_crtcs; i++) {
1075 if (put_user(mode_group->id_list[i],
1076 crtc_id + copied)) {
1077 ret = -EFAULT;
1078 goto out;
1079 }
1080 copied++;
1081 }
1082 }
1083 }
1084 card_res->count_crtcs = crtc_count;
1085
1086 /* Encoders */
1087 if (card_res->count_encoders >= encoder_count) {
1088 copied = 0;
1089 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1090 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1091 list_for_each_entry(encoder,
1092 &dev->mode_config.encoder_list,
1093 head) {
1094 DRM_DEBUG_KMS("ENCODER ID is %d\n",
1095 encoder->base.id);
1096 if (put_user(encoder->base.id, encoder_id +
1097 copied)) {
1098 ret = -EFAULT;
1099 goto out;
1100 }
1101 copied++;
1102 }
1103 } else {
1104 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1105 if (put_user(mode_group->id_list[i],
1106 encoder_id + copied)) {
1107 ret = -EFAULT;
1108 goto out;
1109 }
1110 copied++;
1111 }
1112
1113 }
1114 }
1115 card_res->count_encoders = encoder_count;
1116
1117 /* Connectors */
1118 if (card_res->count_connectors >= connector_count) {
1119 copied = 0;
1120 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1121 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1122 list_for_each_entry(connector,
1123 &dev->mode_config.connector_list,
1124 head) {
1125 DRM_DEBUG_KMS("CONNECTOR ID is %d\n",
1126 connector->base.id);
1127 if (put_user(connector->base.id,
1128 connector_id + copied)) {
1129 ret = -EFAULT;
1130 goto out;
1131 }
1132 copied++;
1133 }
1134 } else {
1135 int start = mode_group->num_crtcs +
1136 mode_group->num_encoders;
1137 for (i = start; i < start + mode_group->num_connectors; i++) {
1138 if (put_user(mode_group->id_list[i],
1139 connector_id + copied)) {
1140 ret = -EFAULT;
1141 goto out;
1142 }
1143 copied++;
1144 }
1145 }
1146 }
1147 card_res->count_connectors = connector_count;
1148
1149 DRM_DEBUG_KMS("Counted %d %d %d\n", card_res->count_crtcs,
1150 card_res->count_connectors, card_res->count_encoders);
1151
1152 out:
1153 mutex_unlock(&dev->mode_config.mutex);
1154 return ret;
1155 }
1156
1157 /**
1158 * drm_mode_getcrtc - get CRTC configuration
1159 * @inode: inode from the ioctl
1160 * @filp: file * from the ioctl
1161 * @cmd: cmd from ioctl
1162 * @arg: arg from ioctl
1163 *
1164 * LOCKING:
1165 * Caller? (FIXME)
1166 *
1167 * Construct a CRTC configuration structure to return to the user.
1168 *
1169 * Called by the user via ioctl.
1170 *
1171 * RETURNS:
1172 * Zero on success, errno on failure.
1173 */
1174 int drm_mode_getcrtc(struct drm_device *dev,
1175 void *data, struct drm_file *file_priv)
1176 {
1177 struct drm_mode_crtc *crtc_resp = data;
1178 struct drm_crtc *crtc;
1179 struct drm_mode_object *obj;
1180 int ret = 0;
1181
1182 mutex_lock(&dev->mode_config.mutex);
1183
1184 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1185 DRM_MODE_OBJECT_CRTC);
1186 if (!obj) {
1187 ret = -EINVAL;
1188 goto out;
1189 }
1190 crtc = obj_to_crtc(obj);
1191
1192 crtc_resp->x = crtc->x;
1193 crtc_resp->y = crtc->y;
1194 crtc_resp->gamma_size = crtc->gamma_size;
1195 if (crtc->fb)
1196 crtc_resp->fb_id = crtc->fb->base.id;
1197 else
1198 crtc_resp->fb_id = 0;
1199
1200 if (crtc->enabled) {
1201
1202 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1203 crtc_resp->mode_valid = 1;
1204
1205 } else {
1206 crtc_resp->mode_valid = 0;
1207 }
1208
1209 out:
1210 mutex_unlock(&dev->mode_config.mutex);
1211 return ret;
1212 }
1213
1214 /**
1215 * drm_mode_getconnector - get connector configuration
1216 * @inode: inode from the ioctl
1217 * @filp: file * from the ioctl
1218 * @cmd: cmd from ioctl
1219 * @arg: arg from ioctl
1220 *
1221 * LOCKING:
1222 * Caller? (FIXME)
1223 *
1224 * Construct a connector configuration structure to return to the user.
1225 *
1226 * Called by the user via ioctl.
1227 *
1228 * RETURNS:
1229 * Zero on success, errno on failure.
1230 */
1231 int drm_mode_getconnector(struct drm_device *dev, void *data,
1232 struct drm_file *file_priv)
1233 {
1234 struct drm_mode_get_connector *out_resp = data;
1235 struct drm_mode_object *obj;
1236 struct drm_connector *connector;
1237 struct drm_display_mode *mode;
1238 int mode_count = 0;
1239 int props_count = 0;
1240 int encoders_count = 0;
1241 int ret = 0;
1242 int copied = 0;
1243 int i;
1244 struct drm_mode_modeinfo u_mode;
1245 struct drm_mode_modeinfo __user *mode_ptr;
1246 uint32_t __user *prop_ptr;
1247 uint64_t __user *prop_values;
1248 uint32_t __user *encoder_ptr;
1249
1250 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1251
1252 DRM_DEBUG_KMS("connector id %d:\n", out_resp->connector_id);
1253
1254 mutex_lock(&dev->mode_config.mutex);
1255
1256 obj = drm_mode_object_find(dev, out_resp->connector_id,
1257 DRM_MODE_OBJECT_CONNECTOR);
1258 if (!obj) {
1259 ret = -EINVAL;
1260 goto out;
1261 }
1262 connector = obj_to_connector(obj);
1263
1264 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1265 if (connector->property_ids[i] != 0) {
1266 props_count++;
1267 }
1268 }
1269
1270 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1271 if (connector->encoder_ids[i] != 0) {
1272 encoders_count++;
1273 }
1274 }
1275
1276 if (out_resp->count_modes == 0) {
1277 connector->funcs->fill_modes(connector,
1278 dev->mode_config.max_width,
1279 dev->mode_config.max_height);
1280 }
1281
1282 /* delayed so we get modes regardless of pre-fill_modes state */
1283 list_for_each_entry(mode, &connector->modes, head)
1284 mode_count++;
1285
1286 out_resp->connector_id = connector->base.id;
1287 out_resp->connector_type = connector->connector_type;
1288 out_resp->connector_type_id = connector->connector_type_id;
1289 out_resp->mm_width = connector->display_info.width_mm;
1290 out_resp->mm_height = connector->display_info.height_mm;
1291 out_resp->subpixel = connector->display_info.subpixel_order;
1292 out_resp->connection = connector->status;
1293 if (connector->encoder)
1294 out_resp->encoder_id = connector->encoder->base.id;
1295 else
1296 out_resp->encoder_id = 0;
1297
1298 /*
1299 * This ioctl is called twice, once to determine how much space is
1300 * needed, and the 2nd time to fill it.
1301 */
1302 if ((out_resp->count_modes >= mode_count) && mode_count) {
1303 copied = 0;
1304 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1305 list_for_each_entry(mode, &connector->modes, head) {
1306 drm_crtc_convert_to_umode(&u_mode, mode);
1307 if (copy_to_user(mode_ptr + copied,
1308 &u_mode, sizeof(u_mode))) {
1309 ret = -EFAULT;
1310 goto out;
1311 }
1312 copied++;
1313 }
1314 }
1315 out_resp->count_modes = mode_count;
1316
1317 if ((out_resp->count_props >= props_count) && props_count) {
1318 copied = 0;
1319 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1320 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1321 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1322 if (connector->property_ids[i] != 0) {
1323 if (put_user(connector->property_ids[i],
1324 prop_ptr + copied)) {
1325 ret = -EFAULT;
1326 goto out;
1327 }
1328
1329 if (put_user(connector->property_values[i],
1330 prop_values + copied)) {
1331 ret = -EFAULT;
1332 goto out;
1333 }
1334 copied++;
1335 }
1336 }
1337 }
1338 out_resp->count_props = props_count;
1339
1340 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1341 copied = 0;
1342 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1343 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1344 if (connector->encoder_ids[i] != 0) {
1345 if (put_user(connector->encoder_ids[i],
1346 encoder_ptr + copied)) {
1347 ret = -EFAULT;
1348 goto out;
1349 }
1350 copied++;
1351 }
1352 }
1353 }
1354 out_resp->count_encoders = encoders_count;
1355
1356 out:
1357 mutex_unlock(&dev->mode_config.mutex);
1358 return ret;
1359 }
1360
1361 int drm_mode_getencoder(struct drm_device *dev, void *data,
1362 struct drm_file *file_priv)
1363 {
1364 struct drm_mode_get_encoder *enc_resp = data;
1365 struct drm_mode_object *obj;
1366 struct drm_encoder *encoder;
1367 int ret = 0;
1368
1369 mutex_lock(&dev->mode_config.mutex);
1370 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1371 DRM_MODE_OBJECT_ENCODER);
1372 if (!obj) {
1373 ret = -EINVAL;
1374 goto out;
1375 }
1376 encoder = obj_to_encoder(obj);
1377
1378 if (encoder->crtc)
1379 enc_resp->crtc_id = encoder->crtc->base.id;
1380 else
1381 enc_resp->crtc_id = 0;
1382 enc_resp->encoder_type = encoder->encoder_type;
1383 enc_resp->encoder_id = encoder->base.id;
1384 enc_resp->possible_crtcs = encoder->possible_crtcs;
1385 enc_resp->possible_clones = encoder->possible_clones;
1386
1387 out:
1388 mutex_unlock(&dev->mode_config.mutex);
1389 return ret;
1390 }
1391
1392 /**
1393 * drm_mode_setcrtc - set CRTC configuration
1394 * @inode: inode from the ioctl
1395 * @filp: file * from the ioctl
1396 * @cmd: cmd from ioctl
1397 * @arg: arg from ioctl
1398 *
1399 * LOCKING:
1400 * Caller? (FIXME)
1401 *
1402 * Build a new CRTC configuration based on user request.
1403 *
1404 * Called by the user via ioctl.
1405 *
1406 * RETURNS:
1407 * Zero on success, errno on failure.
1408 */
1409 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1410 struct drm_file *file_priv)
1411 {
1412 struct drm_mode_config *config = &dev->mode_config;
1413 struct drm_mode_crtc *crtc_req = data;
1414 struct drm_mode_object *obj;
1415 struct drm_crtc *crtc, *crtcfb;
1416 struct drm_connector **connector_set = NULL, *connector;
1417 struct drm_framebuffer *fb = NULL;
1418 struct drm_display_mode *mode = NULL;
1419 struct drm_mode_set set;
1420 uint32_t __user *set_connectors_ptr;
1421 int ret = 0;
1422 int i;
1423
1424 mutex_lock(&dev->mode_config.mutex);
1425 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1426 DRM_MODE_OBJECT_CRTC);
1427 if (!obj) {
1428 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1429 ret = -EINVAL;
1430 goto out;
1431 }
1432 crtc = obj_to_crtc(obj);
1433
1434 if (crtc_req->mode_valid) {
1435 /* If we have a mode we need a framebuffer. */
1436 /* If we pass -1, set the mode with the currently bound fb */
1437 if (crtc_req->fb_id == -1) {
1438 list_for_each_entry(crtcfb,
1439 &dev->mode_config.crtc_list, head) {
1440 if (crtcfb == crtc) {
1441 DRM_DEBUG_KMS("Using current fb for "
1442 "setmode\n");
1443 fb = crtc->fb;
1444 }
1445 }
1446 } else {
1447 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1448 DRM_MODE_OBJECT_FB);
1449 if (!obj) {
1450 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1451 crtc_req->fb_id);
1452 ret = -EINVAL;
1453 goto out;
1454 }
1455 fb = obj_to_fb(obj);
1456 }
1457
1458 mode = drm_mode_create(dev);
1459 drm_crtc_convert_umode(mode, &crtc_req->mode);
1460 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1461 }
1462
1463 if (crtc_req->count_connectors == 0 && mode) {
1464 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1465 ret = -EINVAL;
1466 goto out;
1467 }
1468
1469 if (crtc_req->count_connectors > 0 && !mode && !fb) {
1470 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1471 crtc_req->count_connectors);
1472 ret = -EINVAL;
1473 goto out;
1474 }
1475
1476 if (crtc_req->count_connectors > 0) {
1477 u32 out_id;
1478
1479 /* Avoid unbounded kernel memory allocation */
1480 if (crtc_req->count_connectors > config->num_connector) {
1481 ret = -EINVAL;
1482 goto out;
1483 }
1484
1485 connector_set = kmalloc(crtc_req->count_connectors *
1486 sizeof(struct drm_connector *),
1487 GFP_KERNEL);
1488 if (!connector_set) {
1489 ret = -ENOMEM;
1490 goto out;
1491 }
1492
1493 for (i = 0; i < crtc_req->count_connectors; i++) {
1494 set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1495 if (get_user(out_id, &set_connectors_ptr[i])) {
1496 ret = -EFAULT;
1497 goto out;
1498 }
1499
1500 obj = drm_mode_object_find(dev, out_id,
1501 DRM_MODE_OBJECT_CONNECTOR);
1502 if (!obj) {
1503 DRM_DEBUG_KMS("Connector id %d unknown\n",
1504 out_id);
1505 ret = -EINVAL;
1506 goto out;
1507 }
1508 connector = obj_to_connector(obj);
1509
1510 connector_set[i] = connector;
1511 }
1512 }
1513
1514 set.crtc = crtc;
1515 set.x = crtc_req->x;
1516 set.y = crtc_req->y;
1517 set.mode = mode;
1518 set.connectors = connector_set;
1519 set.num_connectors = crtc_req->count_connectors;
1520 set.fb =fb;
1521 ret = crtc->funcs->set_config(&set);
1522
1523 out:
1524 kfree(connector_set);
1525 mutex_unlock(&dev->mode_config.mutex);
1526 return ret;
1527 }
1528
1529 int drm_mode_cursor_ioctl(struct drm_device *dev,
1530 void *data, struct drm_file *file_priv)
1531 {
1532 struct drm_mode_cursor *req = data;
1533 struct drm_mode_object *obj;
1534 struct drm_crtc *crtc;
1535 int ret = 0;
1536
1537 DRM_DEBUG_KMS("\n");
1538
1539 if (!req->flags) {
1540 DRM_ERROR("no operation set\n");
1541 return -EINVAL;
1542 }
1543
1544 mutex_lock(&dev->mode_config.mutex);
1545 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1546 if (!obj) {
1547 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1548 ret = -EINVAL;
1549 goto out;
1550 }
1551 crtc = obj_to_crtc(obj);
1552
1553 if (req->flags & DRM_MODE_CURSOR_BO) {
1554 if (!crtc->funcs->cursor_set) {
1555 DRM_ERROR("crtc does not support cursor\n");
1556 ret = -ENXIO;
1557 goto out;
1558 }
1559 /* Turns off the cursor if handle is 0 */
1560 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1561 req->width, req->height);
1562 }
1563
1564 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1565 if (crtc->funcs->cursor_move) {
1566 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1567 } else {
1568 DRM_ERROR("crtc does not support cursor\n");
1569 ret = -EFAULT;
1570 goto out;
1571 }
1572 }
1573 out:
1574 mutex_unlock(&dev->mode_config.mutex);
1575 return ret;
1576 }
1577
1578 /**
1579 * drm_mode_addfb - add an FB to the graphics configuration
1580 * @inode: inode from the ioctl
1581 * @filp: file * from the ioctl
1582 * @cmd: cmd from ioctl
1583 * @arg: arg from ioctl
1584 *
1585 * LOCKING:
1586 * Takes mode config lock.
1587 *
1588 * Add a new FB to the specified CRTC, given a user request.
1589 *
1590 * Called by the user via ioctl.
1591 *
1592 * RETURNS:
1593 * Zero on success, errno on failure.
1594 */
1595 int drm_mode_addfb(struct drm_device *dev,
1596 void *data, struct drm_file *file_priv)
1597 {
1598 struct drm_mode_fb_cmd *r = data;
1599 struct drm_mode_config *config = &dev->mode_config;
1600 struct drm_framebuffer *fb;
1601 int ret = 0;
1602
1603 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1604 DRM_ERROR("mode new framebuffer width not within limits\n");
1605 return -EINVAL;
1606 }
1607 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1608 DRM_ERROR("mode new framebuffer height not within limits\n");
1609 return -EINVAL;
1610 }
1611
1612 mutex_lock(&dev->mode_config.mutex);
1613
1614 /* TODO check buffer is sufficently large */
1615 /* TODO setup destructor callback */
1616
1617 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1618 if (!fb) {
1619 DRM_ERROR("could not create framebuffer\n");
1620 ret = -EINVAL;
1621 goto out;
1622 }
1623
1624 r->fb_id = fb->base.id;
1625 list_add(&fb->filp_head, &file_priv->fbs);
1626
1627 out:
1628 mutex_unlock(&dev->mode_config.mutex);
1629 return ret;
1630 }
1631
1632 /**
1633 * drm_mode_rmfb - remove an FB from the configuration
1634 * @inode: inode from the ioctl
1635 * @filp: file * from the ioctl
1636 * @cmd: cmd from ioctl
1637 * @arg: arg from ioctl
1638 *
1639 * LOCKING:
1640 * Takes mode config lock.
1641 *
1642 * Remove the FB specified by the user.
1643 *
1644 * Called by the user via ioctl.
1645 *
1646 * RETURNS:
1647 * Zero on success, errno on failure.
1648 */
1649 int drm_mode_rmfb(struct drm_device *dev,
1650 void *data, struct drm_file *file_priv)
1651 {
1652 struct drm_mode_object *obj;
1653 struct drm_framebuffer *fb = NULL;
1654 struct drm_framebuffer *fbl = NULL;
1655 uint32_t *id = data;
1656 int ret = 0;
1657 int found = 0;
1658
1659 mutex_lock(&dev->mode_config.mutex);
1660 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1661 /* TODO check that we realy get a framebuffer back. */
1662 if (!obj) {
1663 DRM_ERROR("mode invalid framebuffer id\n");
1664 ret = -EINVAL;
1665 goto out;
1666 }
1667 fb = obj_to_fb(obj);
1668
1669 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1670 if (fb == fbl)
1671 found = 1;
1672
1673 if (!found) {
1674 DRM_ERROR("tried to remove a fb that we didn't own\n");
1675 ret = -EINVAL;
1676 goto out;
1677 }
1678
1679 /* TODO release all crtc connected to the framebuffer */
1680 /* TODO unhock the destructor from the buffer object */
1681
1682 list_del(&fb->filp_head);
1683 fb->funcs->destroy(fb);
1684
1685 out:
1686 mutex_unlock(&dev->mode_config.mutex);
1687 return ret;
1688 }
1689
1690 /**
1691 * drm_mode_getfb - get FB info
1692 * @inode: inode from the ioctl
1693 * @filp: file * from the ioctl
1694 * @cmd: cmd from ioctl
1695 * @arg: arg from ioctl
1696 *
1697 * LOCKING:
1698 * Caller? (FIXME)
1699 *
1700 * Lookup the FB given its ID and return info about it.
1701 *
1702 * Called by the user via ioctl.
1703 *
1704 * RETURNS:
1705 * Zero on success, errno on failure.
1706 */
1707 int drm_mode_getfb(struct drm_device *dev,
1708 void *data, struct drm_file *file_priv)
1709 {
1710 struct drm_mode_fb_cmd *r = data;
1711 struct drm_mode_object *obj;
1712 struct drm_framebuffer *fb;
1713 int ret = 0;
1714
1715 mutex_lock(&dev->mode_config.mutex);
1716 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1717 if (!obj) {
1718 DRM_ERROR("invalid framebuffer id\n");
1719 ret = -EINVAL;
1720 goto out;
1721 }
1722 fb = obj_to_fb(obj);
1723
1724 r->height = fb->height;
1725 r->width = fb->width;
1726 r->depth = fb->depth;
1727 r->bpp = fb->bits_per_pixel;
1728 r->pitch = fb->pitch;
1729 fb->funcs->create_handle(fb, file_priv, &r->handle);
1730
1731 out:
1732 mutex_unlock(&dev->mode_config.mutex);
1733 return ret;
1734 }
1735
1736 /**
1737 * drm_fb_release - remove and free the FBs on this file
1738 * @filp: file * from the ioctl
1739 *
1740 * LOCKING:
1741 * Takes mode config lock.
1742 *
1743 * Destroy all the FBs associated with @filp.
1744 *
1745 * Called by the user via ioctl.
1746 *
1747 * RETURNS:
1748 * Zero on success, errno on failure.
1749 */
1750 void drm_fb_release(struct drm_file *priv)
1751 {
1752 struct drm_device *dev = priv->minor->dev;
1753 struct drm_framebuffer *fb, *tfb;
1754
1755 mutex_lock(&dev->mode_config.mutex);
1756 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1757 list_del(&fb->filp_head);
1758 fb->funcs->destroy(fb);
1759 }
1760 mutex_unlock(&dev->mode_config.mutex);
1761 }
1762
1763 /**
1764 * drm_mode_attachmode - add a mode to the user mode list
1765 * @dev: DRM device
1766 * @connector: connector to add the mode to
1767 * @mode: mode to add
1768 *
1769 * Add @mode to @connector's user mode list.
1770 */
1771 static int drm_mode_attachmode(struct drm_device *dev,
1772 struct drm_connector *connector,
1773 struct drm_display_mode *mode)
1774 {
1775 int ret = 0;
1776
1777 list_add_tail(&mode->head, &connector->user_modes);
1778 return ret;
1779 }
1780
1781 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1782 struct drm_display_mode *mode)
1783 {
1784 struct drm_connector *connector;
1785 int ret = 0;
1786 struct drm_display_mode *dup_mode;
1787 int need_dup = 0;
1788 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1789 if (!connector->encoder)
1790 break;
1791 if (connector->encoder->crtc == crtc) {
1792 if (need_dup)
1793 dup_mode = drm_mode_duplicate(dev, mode);
1794 else
1795 dup_mode = mode;
1796 ret = drm_mode_attachmode(dev, connector, dup_mode);
1797 if (ret)
1798 return ret;
1799 need_dup = 1;
1800 }
1801 }
1802 return 0;
1803 }
1804 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1805
1806 static int drm_mode_detachmode(struct drm_device *dev,
1807 struct drm_connector *connector,
1808 struct drm_display_mode *mode)
1809 {
1810 int found = 0;
1811 int ret = 0;
1812 struct drm_display_mode *match_mode, *t;
1813
1814 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1815 if (drm_mode_equal(match_mode, mode)) {
1816 list_del(&match_mode->head);
1817 drm_mode_destroy(dev, match_mode);
1818 found = 1;
1819 break;
1820 }
1821 }
1822
1823 if (!found)
1824 ret = -EINVAL;
1825
1826 return ret;
1827 }
1828
1829 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1830 {
1831 struct drm_connector *connector;
1832
1833 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1834 drm_mode_detachmode(dev, connector, mode);
1835 }
1836 return 0;
1837 }
1838 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1839
1840 /**
1841 * drm_fb_attachmode - Attach a user mode to an connector
1842 * @inode: inode from the ioctl
1843 * @filp: file * from the ioctl
1844 * @cmd: cmd from ioctl
1845 * @arg: arg from ioctl
1846 *
1847 * This attaches a user specified mode to an connector.
1848 * Called by the user via ioctl.
1849 *
1850 * RETURNS:
1851 * Zero on success, errno on failure.
1852 */
1853 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1854 void *data, struct drm_file *file_priv)
1855 {
1856 struct drm_mode_mode_cmd *mode_cmd = data;
1857 struct drm_connector *connector;
1858 struct drm_display_mode *mode;
1859 struct drm_mode_object *obj;
1860 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1861 int ret = 0;
1862
1863 mutex_lock(&dev->mode_config.mutex);
1864
1865 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1866 if (!obj) {
1867 ret = -EINVAL;
1868 goto out;
1869 }
1870 connector = obj_to_connector(obj);
1871
1872 mode = drm_mode_create(dev);
1873 if (!mode) {
1874 ret = -ENOMEM;
1875 goto out;
1876 }
1877
1878 drm_crtc_convert_umode(mode, umode);
1879
1880 ret = drm_mode_attachmode(dev, connector, mode);
1881 out:
1882 mutex_unlock(&dev->mode_config.mutex);
1883 return ret;
1884 }
1885
1886
1887 /**
1888 * drm_fb_detachmode - Detach a user specified mode from an connector
1889 * @inode: inode from the ioctl
1890 * @filp: file * from the ioctl
1891 * @cmd: cmd from ioctl
1892 * @arg: arg from ioctl
1893 *
1894 * Called by the user via ioctl.
1895 *
1896 * RETURNS:
1897 * Zero on success, errno on failure.
1898 */
1899 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1900 void *data, struct drm_file *file_priv)
1901 {
1902 struct drm_mode_object *obj;
1903 struct drm_mode_mode_cmd *mode_cmd = data;
1904 struct drm_connector *connector;
1905 struct drm_display_mode mode;
1906 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1907 int ret = 0;
1908
1909 mutex_lock(&dev->mode_config.mutex);
1910
1911 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1912 if (!obj) {
1913 ret = -EINVAL;
1914 goto out;
1915 }
1916 connector = obj_to_connector(obj);
1917
1918 drm_crtc_convert_umode(&mode, umode);
1919 ret = drm_mode_detachmode(dev, connector, &mode);
1920 out:
1921 mutex_unlock(&dev->mode_config.mutex);
1922 return ret;
1923 }
1924
1925 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1926 const char *name, int num_values)
1927 {
1928 struct drm_property *property = NULL;
1929
1930 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
1931 if (!property)
1932 return NULL;
1933
1934 if (num_values) {
1935 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1936 if (!property->values)
1937 goto fail;
1938 }
1939
1940 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
1941 property->flags = flags;
1942 property->num_values = num_values;
1943 INIT_LIST_HEAD(&property->enum_blob_list);
1944
1945 if (name)
1946 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1947
1948 list_add_tail(&property->head, &dev->mode_config.property_list);
1949 return property;
1950 fail:
1951 kfree(property);
1952 return NULL;
1953 }
1954 EXPORT_SYMBOL(drm_property_create);
1955
1956 int drm_property_add_enum(struct drm_property *property, int index,
1957 uint64_t value, const char *name)
1958 {
1959 struct drm_property_enum *prop_enum;
1960
1961 if (!(property->flags & DRM_MODE_PROP_ENUM))
1962 return -EINVAL;
1963
1964 if (!list_empty(&property->enum_blob_list)) {
1965 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1966 if (prop_enum->value == value) {
1967 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1968 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1969 return 0;
1970 }
1971 }
1972 }
1973
1974 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1975 if (!prop_enum)
1976 return -ENOMEM;
1977
1978 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1979 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1980 prop_enum->value = value;
1981
1982 property->values[index] = value;
1983 list_add_tail(&prop_enum->head, &property->enum_blob_list);
1984 return 0;
1985 }
1986 EXPORT_SYMBOL(drm_property_add_enum);
1987
1988 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1989 {
1990 struct drm_property_enum *prop_enum, *pt;
1991
1992 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
1993 list_del(&prop_enum->head);
1994 kfree(prop_enum);
1995 }
1996
1997 if (property->num_values)
1998 kfree(property->values);
1999 drm_mode_object_put(dev, &property->base);
2000 list_del(&property->head);
2001 kfree(property);
2002 }
2003 EXPORT_SYMBOL(drm_property_destroy);
2004
2005 int drm_connector_attach_property(struct drm_connector *connector,
2006 struct drm_property *property, uint64_t init_val)
2007 {
2008 int i;
2009
2010 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2011 if (connector->property_ids[i] == 0) {
2012 connector->property_ids[i] = property->base.id;
2013 connector->property_values[i] = init_val;
2014 break;
2015 }
2016 }
2017
2018 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2019 return -EINVAL;
2020 return 0;
2021 }
2022 EXPORT_SYMBOL(drm_connector_attach_property);
2023
2024 int drm_connector_property_set_value(struct drm_connector *connector,
2025 struct drm_property *property, uint64_t value)
2026 {
2027 int i;
2028
2029 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2030 if (connector->property_ids[i] == property->base.id) {
2031 connector->property_values[i] = value;
2032 break;
2033 }
2034 }
2035
2036 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2037 return -EINVAL;
2038 return 0;
2039 }
2040 EXPORT_SYMBOL(drm_connector_property_set_value);
2041
2042 int drm_connector_property_get_value(struct drm_connector *connector,
2043 struct drm_property *property, uint64_t *val)
2044 {
2045 int i;
2046
2047 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2048 if (connector->property_ids[i] == property->base.id) {
2049 *val = connector->property_values[i];
2050 break;
2051 }
2052 }
2053
2054 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2055 return -EINVAL;
2056 return 0;
2057 }
2058 EXPORT_SYMBOL(drm_connector_property_get_value);
2059
2060 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2061 void *data, struct drm_file *file_priv)
2062 {
2063 struct drm_mode_object *obj;
2064 struct drm_mode_get_property *out_resp = data;
2065 struct drm_property *property;
2066 int enum_count = 0;
2067 int blob_count = 0;
2068 int value_count = 0;
2069 int ret = 0, i;
2070 int copied;
2071 struct drm_property_enum *prop_enum;
2072 struct drm_mode_property_enum __user *enum_ptr;
2073 struct drm_property_blob *prop_blob;
2074 uint32_t *blob_id_ptr;
2075 uint64_t __user *values_ptr;
2076 uint32_t __user *blob_length_ptr;
2077
2078 mutex_lock(&dev->mode_config.mutex);
2079 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2080 if (!obj) {
2081 ret = -EINVAL;
2082 goto done;
2083 }
2084 property = obj_to_property(obj);
2085
2086 if (property->flags & DRM_MODE_PROP_ENUM) {
2087 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2088 enum_count++;
2089 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2090 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2091 blob_count++;
2092 }
2093
2094 value_count = property->num_values;
2095
2096 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2097 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2098 out_resp->flags = property->flags;
2099
2100 if ((out_resp->count_values >= value_count) && value_count) {
2101 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2102 for (i = 0; i < value_count; i++) {
2103 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2104 ret = -EFAULT;
2105 goto done;
2106 }
2107 }
2108 }
2109 out_resp->count_values = value_count;
2110
2111 if (property->flags & DRM_MODE_PROP_ENUM) {
2112 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2113 copied = 0;
2114 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2115 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2116
2117 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2118 ret = -EFAULT;
2119 goto done;
2120 }
2121
2122 if (copy_to_user(&enum_ptr[copied].name,
2123 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2124 ret = -EFAULT;
2125 goto done;
2126 }
2127 copied++;
2128 }
2129 }
2130 out_resp->count_enum_blobs = enum_count;
2131 }
2132
2133 if (property->flags & DRM_MODE_PROP_BLOB) {
2134 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2135 copied = 0;
2136 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2137 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2138
2139 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2140 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2141 ret = -EFAULT;
2142 goto done;
2143 }
2144
2145 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2146 ret = -EFAULT;
2147 goto done;
2148 }
2149
2150 copied++;
2151 }
2152 }
2153 out_resp->count_enum_blobs = blob_count;
2154 }
2155 done:
2156 mutex_unlock(&dev->mode_config.mutex);
2157 return ret;
2158 }
2159
2160 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2161 void *data)
2162 {
2163 struct drm_property_blob *blob;
2164
2165 if (!length || !data)
2166 return NULL;
2167
2168 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2169 if (!blob)
2170 return NULL;
2171
2172 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2173 blob->length = length;
2174
2175 memcpy(blob->data, data, length);
2176
2177 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2178
2179 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2180 return blob;
2181 }
2182
2183 static void drm_property_destroy_blob(struct drm_device *dev,
2184 struct drm_property_blob *blob)
2185 {
2186 drm_mode_object_put(dev, &blob->base);
2187 list_del(&blob->head);
2188 kfree(blob);
2189 }
2190
2191 int drm_mode_getblob_ioctl(struct drm_device *dev,
2192 void *data, struct drm_file *file_priv)
2193 {
2194 struct drm_mode_object *obj;
2195 struct drm_mode_get_blob *out_resp = data;
2196 struct drm_property_blob *blob;
2197 int ret = 0;
2198 void *blob_ptr;
2199
2200 mutex_lock(&dev->mode_config.mutex);
2201 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2202 if (!obj) {
2203 ret = -EINVAL;
2204 goto done;
2205 }
2206 blob = obj_to_blob(obj);
2207
2208 if (out_resp->length == blob->length) {
2209 blob_ptr = (void *)(unsigned long)out_resp->data;
2210 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2211 ret = -EFAULT;
2212 goto done;
2213 }
2214 }
2215 out_resp->length = blob->length;
2216
2217 done:
2218 mutex_unlock(&dev->mode_config.mutex);
2219 return ret;
2220 }
2221
2222 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2223 struct edid *edid)
2224 {
2225 struct drm_device *dev = connector->dev;
2226 int ret = 0;
2227
2228 if (connector->edid_blob_ptr)
2229 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2230
2231 /* Delete edid, when there is none. */
2232 if (!edid) {
2233 connector->edid_blob_ptr = NULL;
2234 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2235 return ret;
2236 }
2237
2238 connector->edid_blob_ptr = drm_property_create_blob(connector->dev, 128, edid);
2239
2240 ret = drm_connector_property_set_value(connector,
2241 dev->mode_config.edid_property,
2242 connector->edid_blob_ptr->base.id);
2243
2244 return ret;
2245 }
2246 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2247
2248 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2249 void *data, struct drm_file *file_priv)
2250 {
2251 struct drm_mode_connector_set_property *out_resp = data;
2252 struct drm_mode_object *obj;
2253 struct drm_property *property;
2254 struct drm_connector *connector;
2255 int ret = -EINVAL;
2256 int i;
2257
2258 mutex_lock(&dev->mode_config.mutex);
2259
2260 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2261 if (!obj) {
2262 goto out;
2263 }
2264 connector = obj_to_connector(obj);
2265
2266 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2267 if (connector->property_ids[i] == out_resp->prop_id)
2268 break;
2269 }
2270
2271 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2272 goto out;
2273 }
2274
2275 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2276 if (!obj) {
2277 goto out;
2278 }
2279 property = obj_to_property(obj);
2280
2281 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2282 goto out;
2283
2284 if (property->flags & DRM_MODE_PROP_RANGE) {
2285 if (out_resp->value < property->values[0])
2286 goto out;
2287
2288 if (out_resp->value > property->values[1])
2289 goto out;
2290 } else {
2291 int found = 0;
2292 for (i = 0; i < property->num_values; i++) {
2293 if (property->values[i] == out_resp->value) {
2294 found = 1;
2295 break;
2296 }
2297 }
2298 if (!found) {
2299 goto out;
2300 }
2301 }
2302
2303 /* Do DPMS ourselves */
2304 if (property == connector->dev->mode_config.dpms_property) {
2305 if (connector->funcs->dpms)
2306 (*connector->funcs->dpms)(connector, (int) out_resp->value);
2307 ret = 0;
2308 } else if (connector->funcs->set_property)
2309 ret = connector->funcs->set_property(connector, property, out_resp->value);
2310
2311 /* store the property value if succesful */
2312 if (!ret)
2313 drm_connector_property_set_value(connector, property, out_resp->value);
2314 out:
2315 mutex_unlock(&dev->mode_config.mutex);
2316 return ret;
2317 }
2318
2319 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2320 struct drm_encoder *encoder)
2321 {
2322 int i;
2323
2324 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2325 if (connector->encoder_ids[i] == 0) {
2326 connector->encoder_ids[i] = encoder->base.id;
2327 return 0;
2328 }
2329 }
2330 return -ENOMEM;
2331 }
2332 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2333
2334 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2335 struct drm_encoder *encoder)
2336 {
2337 int i;
2338 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2339 if (connector->encoder_ids[i] == encoder->base.id) {
2340 connector->encoder_ids[i] = 0;
2341 if (connector->encoder == encoder)
2342 connector->encoder = NULL;
2343 break;
2344 }
2345 }
2346 }
2347 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2348
2349 bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2350 int gamma_size)
2351 {
2352 crtc->gamma_size = gamma_size;
2353
2354 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2355 if (!crtc->gamma_store) {
2356 crtc->gamma_size = 0;
2357 return false;
2358 }
2359
2360 return true;
2361 }
2362 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2363
2364 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2365 void *data, struct drm_file *file_priv)
2366 {
2367 struct drm_mode_crtc_lut *crtc_lut = data;
2368 struct drm_mode_object *obj;
2369 struct drm_crtc *crtc;
2370 void *r_base, *g_base, *b_base;
2371 int size;
2372 int ret = 0;
2373
2374 mutex_lock(&dev->mode_config.mutex);
2375 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2376 if (!obj) {
2377 ret = -EINVAL;
2378 goto out;
2379 }
2380 crtc = obj_to_crtc(obj);
2381
2382 /* memcpy into gamma store */
2383 if (crtc_lut->gamma_size != crtc->gamma_size) {
2384 ret = -EINVAL;
2385 goto out;
2386 }
2387
2388 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2389 r_base = crtc->gamma_store;
2390 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2391 ret = -EFAULT;
2392 goto out;
2393 }
2394
2395 g_base = r_base + size;
2396 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2397 ret = -EFAULT;
2398 goto out;
2399 }
2400
2401 b_base = g_base + size;
2402 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2403 ret = -EFAULT;
2404 goto out;
2405 }
2406
2407 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
2408
2409 out:
2410 mutex_unlock(&dev->mode_config.mutex);
2411 return ret;
2412
2413 }
2414
2415 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2416 void *data, struct drm_file *file_priv)
2417 {
2418 struct drm_mode_crtc_lut *crtc_lut = data;
2419 struct drm_mode_object *obj;
2420 struct drm_crtc *crtc;
2421 void *r_base, *g_base, *b_base;
2422 int size;
2423 int ret = 0;
2424
2425 mutex_lock(&dev->mode_config.mutex);
2426 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2427 if (!obj) {
2428 ret = -EINVAL;
2429 goto out;
2430 }
2431 crtc = obj_to_crtc(obj);
2432
2433 /* memcpy into gamma store */
2434 if (crtc_lut->gamma_size != crtc->gamma_size) {
2435 ret = -EINVAL;
2436 goto out;
2437 }
2438
2439 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2440 r_base = crtc->gamma_store;
2441 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2442 ret = -EFAULT;
2443 goto out;
2444 }
2445
2446 g_base = r_base + size;
2447 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2448 ret = -EFAULT;
2449 goto out;
2450 }
2451
2452 b_base = g_base + size;
2453 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2454 ret = -EFAULT;
2455 goto out;
2456 }
2457 out:
2458 mutex_unlock(&dev->mode_config.mutex);
2459 return ret;
2460 }
This page took 0.147037 seconds and 6 git commands to generate.