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