d11763f7211abe242386fb8208273d033d03586b
[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 <linux/slab.h>
34 #include <linux/export.h>
35 #include "drm.h"
36 #include "drmP.h"
37 #include "drm_crtc.h"
38 #include "drm_edid.h"
39 #include "drm_fourcc.h"
40
41 /* Avoid boilerplate. I'm tired of typing. */
42 #define DRM_ENUM_NAME_FN(fnname, list) \
43 char *fnname(int val) \
44 { \
45 int i; \
46 for (i = 0; i < ARRAY_SIZE(list); i++) { \
47 if (list[i].type == val) \
48 return list[i].name; \
49 } \
50 return "(unknown)"; \
51 }
52
53 /*
54 * Global properties
55 */
56 static struct drm_prop_enum_list drm_dpms_enum_list[] =
57 { { DRM_MODE_DPMS_ON, "On" },
58 { DRM_MODE_DPMS_STANDBY, "Standby" },
59 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
60 { DRM_MODE_DPMS_OFF, "Off" }
61 };
62
63 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
64
65 /*
66 * Optional properties
67 */
68 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
69 {
70 { DRM_MODE_SCALE_NONE, "None" },
71 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
72 { DRM_MODE_SCALE_CENTER, "Center" },
73 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
74 };
75
76 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
77 {
78 { DRM_MODE_DITHERING_OFF, "Off" },
79 { DRM_MODE_DITHERING_ON, "On" },
80 { DRM_MODE_DITHERING_AUTO, "Automatic" },
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 static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
129 { DRM_MODE_DIRTY_OFF, "Off" },
130 { DRM_MODE_DIRTY_ON, "On" },
131 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
132 };
133
134 DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
135 drm_dirty_info_enum_list)
136
137 struct drm_conn_prop_enum_list {
138 int type;
139 char *name;
140 int count;
141 };
142
143 /*
144 * Connector and encoder types.
145 */
146 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
147 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
148 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
149 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
150 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
151 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
152 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
153 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
154 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
155 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
156 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
157 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
158 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
159 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
160 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
161 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
162 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
163 };
164
165 static struct drm_prop_enum_list drm_encoder_enum_list[] =
166 { { DRM_MODE_ENCODER_NONE, "None" },
167 { DRM_MODE_ENCODER_DAC, "DAC" },
168 { DRM_MODE_ENCODER_TMDS, "TMDS" },
169 { DRM_MODE_ENCODER_LVDS, "LVDS" },
170 { DRM_MODE_ENCODER_TVDAC, "TV" },
171 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
172 };
173
174 char *drm_get_encoder_name(struct drm_encoder *encoder)
175 {
176 static char buf[32];
177
178 snprintf(buf, 32, "%s-%d",
179 drm_encoder_enum_list[encoder->encoder_type].name,
180 encoder->base.id);
181 return buf;
182 }
183 EXPORT_SYMBOL(drm_get_encoder_name);
184
185 char *drm_get_connector_name(struct drm_connector *connector)
186 {
187 static char buf[32];
188
189 snprintf(buf, 32, "%s-%d",
190 drm_connector_enum_list[connector->connector_type].name,
191 connector->connector_type_id);
192 return buf;
193 }
194 EXPORT_SYMBOL(drm_get_connector_name);
195
196 char *drm_get_connector_status_name(enum drm_connector_status status)
197 {
198 if (status == connector_status_connected)
199 return "connected";
200 else if (status == connector_status_disconnected)
201 return "disconnected";
202 else
203 return "unknown";
204 }
205
206 /**
207 * drm_mode_object_get - allocate a new identifier
208 * @dev: DRM device
209 * @ptr: object pointer, used to generate unique ID
210 * @type: object type
211 *
212 * LOCKING:
213 *
214 * Create a unique identifier based on @ptr in @dev's identifier space. Used
215 * for tracking modes, CRTCs and connectors.
216 *
217 * RETURNS:
218 * New unique (relative to other objects in @dev) integer identifier for the
219 * object.
220 */
221 static int drm_mode_object_get(struct drm_device *dev,
222 struct drm_mode_object *obj, uint32_t obj_type)
223 {
224 int new_id = 0;
225 int ret;
226
227 again:
228 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
229 DRM_ERROR("Ran out memory getting a mode number\n");
230 return -EINVAL;
231 }
232
233 mutex_lock(&dev->mode_config.idr_mutex);
234 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
235 mutex_unlock(&dev->mode_config.idr_mutex);
236 if (ret == -EAGAIN)
237 goto again;
238
239 obj->id = new_id;
240 obj->type = obj_type;
241 return 0;
242 }
243
244 /**
245 * drm_mode_object_put - free an identifer
246 * @dev: DRM device
247 * @id: ID to free
248 *
249 * LOCKING:
250 * Caller must hold DRM mode_config lock.
251 *
252 * Free @id from @dev's unique identifier pool.
253 */
254 static void drm_mode_object_put(struct drm_device *dev,
255 struct drm_mode_object *object)
256 {
257 mutex_lock(&dev->mode_config.idr_mutex);
258 idr_remove(&dev->mode_config.crtc_idr, object->id);
259 mutex_unlock(&dev->mode_config.idr_mutex);
260 }
261
262 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
263 uint32_t id, uint32_t type)
264 {
265 struct drm_mode_object *obj = NULL;
266
267 mutex_lock(&dev->mode_config.idr_mutex);
268 obj = idr_find(&dev->mode_config.crtc_idr, id);
269 if (!obj || (obj->type != type) || (obj->id != id))
270 obj = NULL;
271 mutex_unlock(&dev->mode_config.idr_mutex);
272
273 return obj;
274 }
275 EXPORT_SYMBOL(drm_mode_object_find);
276
277 /**
278 * drm_framebuffer_init - initialize a framebuffer
279 * @dev: DRM device
280 *
281 * LOCKING:
282 * Caller must hold mode config lock.
283 *
284 * Allocates an ID for the framebuffer's parent mode object, sets its mode
285 * functions & device file and adds it to the master fd list.
286 *
287 * RETURNS:
288 * Zero on success, error code on failure.
289 */
290 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
291 const struct drm_framebuffer_funcs *funcs)
292 {
293 int ret;
294
295 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
296 if (ret) {
297 return ret;
298 }
299
300 fb->dev = dev;
301 fb->funcs = funcs;
302 dev->mode_config.num_fb++;
303 list_add(&fb->head, &dev->mode_config.fb_list);
304
305 return 0;
306 }
307 EXPORT_SYMBOL(drm_framebuffer_init);
308
309 /**
310 * drm_framebuffer_cleanup - remove a framebuffer object
311 * @fb: framebuffer to remove
312 *
313 * LOCKING:
314 * Caller must hold mode config lock.
315 *
316 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
317 * it, setting it to NULL.
318 */
319 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
320 {
321 struct drm_device *dev = fb->dev;
322 struct drm_crtc *crtc;
323 struct drm_plane *plane;
324 struct drm_mode_set set;
325 int ret;
326
327 /* remove from any CRTC */
328 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
329 if (crtc->fb == fb) {
330 /* should turn off the crtc */
331 memset(&set, 0, sizeof(struct drm_mode_set));
332 set.crtc = crtc;
333 set.fb = NULL;
334 ret = crtc->funcs->set_config(&set);
335 if (ret)
336 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
337 }
338 }
339
340 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
341 if (plane->fb == fb) {
342 /* should turn off the crtc */
343 ret = plane->funcs->disable_plane(plane);
344 if (ret)
345 DRM_ERROR("failed to disable plane with busy fb\n");
346 /* disconnect the plane from the fb and crtc: */
347 plane->fb = NULL;
348 plane->crtc = NULL;
349 }
350 }
351
352 drm_mode_object_put(dev, &fb->base);
353 list_del(&fb->head);
354 dev->mode_config.num_fb--;
355 }
356 EXPORT_SYMBOL(drm_framebuffer_cleanup);
357
358 /**
359 * drm_crtc_init - Initialise a new CRTC object
360 * @dev: DRM device
361 * @crtc: CRTC object to init
362 * @funcs: callbacks for the new CRTC
363 *
364 * LOCKING:
365 * Caller must hold mode config lock.
366 *
367 * Inits a new object created as base part of an driver crtc object.
368 */
369 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
370 const struct drm_crtc_funcs *funcs)
371 {
372 crtc->dev = dev;
373 crtc->funcs = funcs;
374
375 mutex_lock(&dev->mode_config.mutex);
376 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
377
378 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
379 dev->mode_config.num_crtc++;
380 mutex_unlock(&dev->mode_config.mutex);
381 }
382 EXPORT_SYMBOL(drm_crtc_init);
383
384 /**
385 * drm_crtc_cleanup - Cleans up the core crtc usage.
386 * @crtc: CRTC to cleanup
387 *
388 * LOCKING:
389 * Caller must hold mode config lock.
390 *
391 * Cleanup @crtc. Removes from drm modesetting space
392 * does NOT free object, caller does that.
393 */
394 void drm_crtc_cleanup(struct drm_crtc *crtc)
395 {
396 struct drm_device *dev = crtc->dev;
397
398 if (crtc->gamma_store) {
399 kfree(crtc->gamma_store);
400 crtc->gamma_store = NULL;
401 }
402
403 drm_mode_object_put(dev, &crtc->base);
404 list_del(&crtc->head);
405 dev->mode_config.num_crtc--;
406 }
407 EXPORT_SYMBOL(drm_crtc_cleanup);
408
409 /**
410 * drm_mode_probed_add - add a mode to a connector's probed mode list
411 * @connector: connector the new mode
412 * @mode: mode data
413 *
414 * LOCKING:
415 * Caller must hold mode config lock.
416 *
417 * Add @mode to @connector's mode list for later use.
418 */
419 void drm_mode_probed_add(struct drm_connector *connector,
420 struct drm_display_mode *mode)
421 {
422 list_add(&mode->head, &connector->probed_modes);
423 }
424 EXPORT_SYMBOL(drm_mode_probed_add);
425
426 /**
427 * drm_mode_remove - remove and free a mode
428 * @connector: connector list to modify
429 * @mode: mode to remove
430 *
431 * LOCKING:
432 * Caller must hold mode config lock.
433 *
434 * Remove @mode from @connector's mode list, then free it.
435 */
436 void drm_mode_remove(struct drm_connector *connector,
437 struct drm_display_mode *mode)
438 {
439 list_del(&mode->head);
440 drm_mode_destroy(connector->dev, mode);
441 }
442 EXPORT_SYMBOL(drm_mode_remove);
443
444 /**
445 * drm_connector_init - Init a preallocated connector
446 * @dev: DRM device
447 * @connector: the connector to init
448 * @funcs: callbacks for this connector
449 * @name: user visible name of the connector
450 *
451 * LOCKING:
452 * Takes mode config lock.
453 *
454 * Initialises a preallocated connector. Connectors should be
455 * subclassed as part of driver connector objects.
456 */
457 void drm_connector_init(struct drm_device *dev,
458 struct drm_connector *connector,
459 const struct drm_connector_funcs *funcs,
460 int connector_type)
461 {
462 mutex_lock(&dev->mode_config.mutex);
463
464 connector->dev = dev;
465 connector->funcs = funcs;
466 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
467 connector->connector_type = connector_type;
468 connector->connector_type_id =
469 ++drm_connector_enum_list[connector_type].count; /* TODO */
470 INIT_LIST_HEAD(&connector->user_modes);
471 INIT_LIST_HEAD(&connector->probed_modes);
472 INIT_LIST_HEAD(&connector->modes);
473 connector->edid_blob_ptr = NULL;
474
475 list_add_tail(&connector->head, &dev->mode_config.connector_list);
476 dev->mode_config.num_connector++;
477
478 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
479 drm_connector_attach_property(connector,
480 dev->mode_config.edid_property,
481 0);
482
483 drm_connector_attach_property(connector,
484 dev->mode_config.dpms_property, 0);
485
486 mutex_unlock(&dev->mode_config.mutex);
487 }
488 EXPORT_SYMBOL(drm_connector_init);
489
490 /**
491 * drm_connector_cleanup - cleans up an initialised connector
492 * @connector: connector to cleanup
493 *
494 * LOCKING:
495 * Takes mode config lock.
496 *
497 * Cleans up the connector but doesn't free the object.
498 */
499 void drm_connector_cleanup(struct drm_connector *connector)
500 {
501 struct drm_device *dev = connector->dev;
502 struct drm_display_mode *mode, *t;
503
504 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
505 drm_mode_remove(connector, mode);
506
507 list_for_each_entry_safe(mode, t, &connector->modes, head)
508 drm_mode_remove(connector, mode);
509
510 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
511 drm_mode_remove(connector, mode);
512
513 mutex_lock(&dev->mode_config.mutex);
514 drm_mode_object_put(dev, &connector->base);
515 list_del(&connector->head);
516 dev->mode_config.num_connector--;
517 mutex_unlock(&dev->mode_config.mutex);
518 }
519 EXPORT_SYMBOL(drm_connector_cleanup);
520
521 void drm_encoder_init(struct drm_device *dev,
522 struct drm_encoder *encoder,
523 const struct drm_encoder_funcs *funcs,
524 int encoder_type)
525 {
526 mutex_lock(&dev->mode_config.mutex);
527
528 encoder->dev = dev;
529
530 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
531 encoder->encoder_type = encoder_type;
532 encoder->funcs = funcs;
533
534 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
535 dev->mode_config.num_encoder++;
536
537 mutex_unlock(&dev->mode_config.mutex);
538 }
539 EXPORT_SYMBOL(drm_encoder_init);
540
541 void drm_encoder_cleanup(struct drm_encoder *encoder)
542 {
543 struct drm_device *dev = encoder->dev;
544 mutex_lock(&dev->mode_config.mutex);
545 drm_mode_object_put(dev, &encoder->base);
546 list_del(&encoder->head);
547 dev->mode_config.num_encoder--;
548 mutex_unlock(&dev->mode_config.mutex);
549 }
550 EXPORT_SYMBOL(drm_encoder_cleanup);
551
552 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
553 unsigned long possible_crtcs,
554 const struct drm_plane_funcs *funcs,
555 const uint32_t *formats, uint32_t format_count,
556 bool priv)
557 {
558 mutex_lock(&dev->mode_config.mutex);
559
560 plane->dev = dev;
561 drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
562 plane->funcs = funcs;
563 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
564 GFP_KERNEL);
565 if (!plane->format_types) {
566 DRM_DEBUG_KMS("out of memory when allocating plane\n");
567 drm_mode_object_put(dev, &plane->base);
568 mutex_unlock(&dev->mode_config.mutex);
569 return -ENOMEM;
570 }
571
572 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
573 plane->format_count = format_count;
574 plane->possible_crtcs = possible_crtcs;
575
576 /* private planes are not exposed to userspace, but depending on
577 * display hardware, might be convenient to allow sharing programming
578 * for the scanout engine with the crtc implementation.
579 */
580 if (!priv) {
581 list_add_tail(&plane->head, &dev->mode_config.plane_list);
582 dev->mode_config.num_plane++;
583 } else {
584 INIT_LIST_HEAD(&plane->head);
585 }
586
587 mutex_unlock(&dev->mode_config.mutex);
588
589 return 0;
590 }
591 EXPORT_SYMBOL(drm_plane_init);
592
593 void drm_plane_cleanup(struct drm_plane *plane)
594 {
595 struct drm_device *dev = plane->dev;
596
597 mutex_lock(&dev->mode_config.mutex);
598 kfree(plane->format_types);
599 drm_mode_object_put(dev, &plane->base);
600 /* if not added to a list, it must be a private plane */
601 if (!list_empty(&plane->head)) {
602 list_del(&plane->head);
603 dev->mode_config.num_plane--;
604 }
605 mutex_unlock(&dev->mode_config.mutex);
606 }
607 EXPORT_SYMBOL(drm_plane_cleanup);
608
609 /**
610 * drm_mode_create - create a new display mode
611 * @dev: DRM device
612 *
613 * LOCKING:
614 * Caller must hold DRM mode_config lock.
615 *
616 * Create a new drm_display_mode, give it an ID, and return it.
617 *
618 * RETURNS:
619 * Pointer to new mode on success, NULL on error.
620 */
621 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
622 {
623 struct drm_display_mode *nmode;
624
625 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
626 if (!nmode)
627 return NULL;
628
629 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
630 return nmode;
631 }
632 EXPORT_SYMBOL(drm_mode_create);
633
634 /**
635 * drm_mode_destroy - remove a mode
636 * @dev: DRM device
637 * @mode: mode to remove
638 *
639 * LOCKING:
640 * Caller must hold mode config lock.
641 *
642 * Free @mode's unique identifier, then free it.
643 */
644 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
645 {
646 drm_mode_object_put(dev, &mode->base);
647
648 kfree(mode);
649 }
650 EXPORT_SYMBOL(drm_mode_destroy);
651
652 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
653 {
654 struct drm_property *edid;
655 struct drm_property *dpms;
656
657 /*
658 * Standard properties (apply to all connectors)
659 */
660 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
661 DRM_MODE_PROP_IMMUTABLE,
662 "EDID", 0);
663 dev->mode_config.edid_property = edid;
664
665 dpms = drm_property_create_enum(dev, 0,
666 "DPMS", drm_dpms_enum_list,
667 ARRAY_SIZE(drm_dpms_enum_list));
668 dev->mode_config.dpms_property = dpms;
669
670 return 0;
671 }
672
673 /**
674 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
675 * @dev: DRM device
676 *
677 * Called by a driver the first time a DVI-I connector is made.
678 */
679 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
680 {
681 struct drm_property *dvi_i_selector;
682 struct drm_property *dvi_i_subconnector;
683
684 if (dev->mode_config.dvi_i_select_subconnector_property)
685 return 0;
686
687 dvi_i_selector =
688 drm_property_create_enum(dev, 0,
689 "select subconnector",
690 drm_dvi_i_select_enum_list,
691 ARRAY_SIZE(drm_dvi_i_select_enum_list));
692 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
693
694 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
695 "subconnector",
696 drm_dvi_i_subconnector_enum_list,
697 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
698 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
699
700 return 0;
701 }
702 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
703
704 /**
705 * drm_create_tv_properties - create TV specific connector properties
706 * @dev: DRM device
707 * @num_modes: number of different TV formats (modes) supported
708 * @modes: array of pointers to strings containing name of each format
709 *
710 * Called by a driver's TV initialization routine, this function creates
711 * the TV specific connector properties for a given device. Caller is
712 * responsible for allocating a list of format names and passing them to
713 * this routine.
714 */
715 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
716 char *modes[])
717 {
718 struct drm_property *tv_selector;
719 struct drm_property *tv_subconnector;
720 int i;
721
722 if (dev->mode_config.tv_select_subconnector_property)
723 return 0;
724
725 /*
726 * Basic connector properties
727 */
728 tv_selector = drm_property_create_enum(dev, 0,
729 "select subconnector",
730 drm_tv_select_enum_list,
731 ARRAY_SIZE(drm_tv_select_enum_list));
732 dev->mode_config.tv_select_subconnector_property = tv_selector;
733
734 tv_subconnector =
735 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
736 "subconnector",
737 drm_tv_subconnector_enum_list,
738 ARRAY_SIZE(drm_tv_subconnector_enum_list));
739 dev->mode_config.tv_subconnector_property = tv_subconnector;
740
741 /*
742 * Other, TV specific properties: margins & TV modes.
743 */
744 dev->mode_config.tv_left_margin_property =
745 drm_property_create_range(dev, 0, "left margin", 0, 100);
746
747 dev->mode_config.tv_right_margin_property =
748 drm_property_create_range(dev, 0, "right margin", 0, 100);
749
750 dev->mode_config.tv_top_margin_property =
751 drm_property_create_range(dev, 0, "top margin", 0, 100);
752
753 dev->mode_config.tv_bottom_margin_property =
754 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
755
756 dev->mode_config.tv_mode_property =
757 drm_property_create(dev, DRM_MODE_PROP_ENUM,
758 "mode", num_modes);
759 for (i = 0; i < num_modes; i++)
760 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
761 i, modes[i]);
762
763 dev->mode_config.tv_brightness_property =
764 drm_property_create_range(dev, 0, "brightness", 0, 100);
765
766 dev->mode_config.tv_contrast_property =
767 drm_property_create_range(dev, 0, "contrast", 0, 100);
768
769 dev->mode_config.tv_flicker_reduction_property =
770 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
771
772 dev->mode_config.tv_overscan_property =
773 drm_property_create_range(dev, 0, "overscan", 0, 100);
774
775 dev->mode_config.tv_saturation_property =
776 drm_property_create_range(dev, 0, "saturation", 0, 100);
777
778 dev->mode_config.tv_hue_property =
779 drm_property_create_range(dev, 0, "hue", 0, 100);
780
781 return 0;
782 }
783 EXPORT_SYMBOL(drm_mode_create_tv_properties);
784
785 /**
786 * drm_mode_create_scaling_mode_property - create scaling mode property
787 * @dev: DRM device
788 *
789 * Called by a driver the first time it's needed, must be attached to desired
790 * connectors.
791 */
792 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
793 {
794 struct drm_property *scaling_mode;
795
796 if (dev->mode_config.scaling_mode_property)
797 return 0;
798
799 scaling_mode =
800 drm_property_create_enum(dev, 0, "scaling mode",
801 drm_scaling_mode_enum_list,
802 ARRAY_SIZE(drm_scaling_mode_enum_list));
803
804 dev->mode_config.scaling_mode_property = scaling_mode;
805
806 return 0;
807 }
808 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
809
810 /**
811 * drm_mode_create_dithering_property - create dithering property
812 * @dev: DRM device
813 *
814 * Called by a driver the first time it's needed, must be attached to desired
815 * connectors.
816 */
817 int drm_mode_create_dithering_property(struct drm_device *dev)
818 {
819 struct drm_property *dithering_mode;
820
821 if (dev->mode_config.dithering_mode_property)
822 return 0;
823
824 dithering_mode =
825 drm_property_create_enum(dev, 0, "dithering",
826 drm_dithering_mode_enum_list,
827 ARRAY_SIZE(drm_dithering_mode_enum_list));
828 dev->mode_config.dithering_mode_property = dithering_mode;
829
830 return 0;
831 }
832 EXPORT_SYMBOL(drm_mode_create_dithering_property);
833
834 /**
835 * drm_mode_create_dirty_property - create dirty property
836 * @dev: DRM device
837 *
838 * Called by a driver the first time it's needed, must be attached to desired
839 * connectors.
840 */
841 int drm_mode_create_dirty_info_property(struct drm_device *dev)
842 {
843 struct drm_property *dirty_info;
844
845 if (dev->mode_config.dirty_info_property)
846 return 0;
847
848 dirty_info =
849 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
850 "dirty",
851 drm_dirty_info_enum_list,
852 ARRAY_SIZE(drm_dirty_info_enum_list));
853 dev->mode_config.dirty_info_property = dirty_info;
854
855 return 0;
856 }
857 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
858
859 /**
860 * drm_mode_config_init - initialize DRM mode_configuration structure
861 * @dev: DRM device
862 *
863 * LOCKING:
864 * None, should happen single threaded at init time.
865 *
866 * Initialize @dev's mode_config structure, used for tracking the graphics
867 * configuration of @dev.
868 */
869 void drm_mode_config_init(struct drm_device *dev)
870 {
871 mutex_init(&dev->mode_config.mutex);
872 mutex_init(&dev->mode_config.idr_mutex);
873 INIT_LIST_HEAD(&dev->mode_config.fb_list);
874 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
875 INIT_LIST_HEAD(&dev->mode_config.connector_list);
876 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
877 INIT_LIST_HEAD(&dev->mode_config.property_list);
878 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
879 INIT_LIST_HEAD(&dev->mode_config.plane_list);
880 idr_init(&dev->mode_config.crtc_idr);
881
882 mutex_lock(&dev->mode_config.mutex);
883 drm_mode_create_standard_connector_properties(dev);
884 mutex_unlock(&dev->mode_config.mutex);
885
886 /* Just to be sure */
887 dev->mode_config.num_fb = 0;
888 dev->mode_config.num_connector = 0;
889 dev->mode_config.num_crtc = 0;
890 dev->mode_config.num_encoder = 0;
891 }
892 EXPORT_SYMBOL(drm_mode_config_init);
893
894 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
895 {
896 uint32_t total_objects = 0;
897
898 total_objects += dev->mode_config.num_crtc;
899 total_objects += dev->mode_config.num_connector;
900 total_objects += dev->mode_config.num_encoder;
901
902 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
903 if (!group->id_list)
904 return -ENOMEM;
905
906 group->num_crtcs = 0;
907 group->num_connectors = 0;
908 group->num_encoders = 0;
909 return 0;
910 }
911
912 int drm_mode_group_init_legacy_group(struct drm_device *dev,
913 struct drm_mode_group *group)
914 {
915 struct drm_crtc *crtc;
916 struct drm_encoder *encoder;
917 struct drm_connector *connector;
918 int ret;
919
920 if ((ret = drm_mode_group_init(dev, group)))
921 return ret;
922
923 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
924 group->id_list[group->num_crtcs++] = crtc->base.id;
925
926 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
927 group->id_list[group->num_crtcs + group->num_encoders++] =
928 encoder->base.id;
929
930 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
931 group->id_list[group->num_crtcs + group->num_encoders +
932 group->num_connectors++] = connector->base.id;
933
934 return 0;
935 }
936
937 /**
938 * drm_mode_config_cleanup - free up DRM mode_config info
939 * @dev: DRM device
940 *
941 * LOCKING:
942 * Caller must hold mode config lock.
943 *
944 * Free up all the connectors and CRTCs associated with this DRM device, then
945 * free up the framebuffers and associated buffer objects.
946 *
947 * FIXME: cleanup any dangling user buffer objects too
948 */
949 void drm_mode_config_cleanup(struct drm_device *dev)
950 {
951 struct drm_connector *connector, *ot;
952 struct drm_crtc *crtc, *ct;
953 struct drm_encoder *encoder, *enct;
954 struct drm_framebuffer *fb, *fbt;
955 struct drm_property *property, *pt;
956 struct drm_plane *plane, *plt;
957
958 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
959 head) {
960 encoder->funcs->destroy(encoder);
961 }
962
963 list_for_each_entry_safe(connector, ot,
964 &dev->mode_config.connector_list, head) {
965 connector->funcs->destroy(connector);
966 }
967
968 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
969 head) {
970 drm_property_destroy(dev, property);
971 }
972
973 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
974 fb->funcs->destroy(fb);
975 }
976
977 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
978 crtc->funcs->destroy(crtc);
979 }
980
981 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
982 head) {
983 plane->funcs->destroy(plane);
984 }
985
986 idr_remove_all(&dev->mode_config.crtc_idr);
987 idr_destroy(&dev->mode_config.crtc_idr);
988 }
989 EXPORT_SYMBOL(drm_mode_config_cleanup);
990
991 /**
992 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
993 * @out: drm_mode_modeinfo struct to return to the user
994 * @in: drm_display_mode to use
995 *
996 * LOCKING:
997 * None.
998 *
999 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1000 * the user.
1001 */
1002 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1003 struct drm_display_mode *in)
1004 {
1005 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1006 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1007 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1008 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1009 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1010 "timing values too large for mode info\n");
1011
1012 out->clock = in->clock;
1013 out->hdisplay = in->hdisplay;
1014 out->hsync_start = in->hsync_start;
1015 out->hsync_end = in->hsync_end;
1016 out->htotal = in->htotal;
1017 out->hskew = in->hskew;
1018 out->vdisplay = in->vdisplay;
1019 out->vsync_start = in->vsync_start;
1020 out->vsync_end = in->vsync_end;
1021 out->vtotal = in->vtotal;
1022 out->vscan = in->vscan;
1023 out->vrefresh = in->vrefresh;
1024 out->flags = in->flags;
1025 out->type = in->type;
1026 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1027 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1028 }
1029
1030 /**
1031 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1032 * @out: drm_display_mode to return to the user
1033 * @in: drm_mode_modeinfo to use
1034 *
1035 * LOCKING:
1036 * None.
1037 *
1038 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1039 * the caller.
1040 */
1041 void drm_crtc_convert_umode(struct drm_display_mode *out,
1042 struct drm_mode_modeinfo *in)
1043 {
1044 out->clock = in->clock;
1045 out->hdisplay = in->hdisplay;
1046 out->hsync_start = in->hsync_start;
1047 out->hsync_end = in->hsync_end;
1048 out->htotal = in->htotal;
1049 out->hskew = in->hskew;
1050 out->vdisplay = in->vdisplay;
1051 out->vsync_start = in->vsync_start;
1052 out->vsync_end = in->vsync_end;
1053 out->vtotal = in->vtotal;
1054 out->vscan = in->vscan;
1055 out->vrefresh = in->vrefresh;
1056 out->flags = in->flags;
1057 out->type = in->type;
1058 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1059 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1060 }
1061
1062 /**
1063 * drm_mode_getresources - get graphics configuration
1064 * @inode: inode from the ioctl
1065 * @filp: file * from the ioctl
1066 * @cmd: cmd from ioctl
1067 * @arg: arg from ioctl
1068 *
1069 * LOCKING:
1070 * Takes mode config lock.
1071 *
1072 * Construct a set of configuration description structures and return
1073 * them to the user, including CRTC, connector and framebuffer configuration.
1074 *
1075 * Called by the user via ioctl.
1076 *
1077 * RETURNS:
1078 * Zero on success, errno on failure.
1079 */
1080 int drm_mode_getresources(struct drm_device *dev, void *data,
1081 struct drm_file *file_priv)
1082 {
1083 struct drm_mode_card_res *card_res = data;
1084 struct list_head *lh;
1085 struct drm_framebuffer *fb;
1086 struct drm_connector *connector;
1087 struct drm_crtc *crtc;
1088 struct drm_encoder *encoder;
1089 int ret = 0;
1090 int connector_count = 0;
1091 int crtc_count = 0;
1092 int fb_count = 0;
1093 int encoder_count = 0;
1094 int copied = 0, i;
1095 uint32_t __user *fb_id;
1096 uint32_t __user *crtc_id;
1097 uint32_t __user *connector_id;
1098 uint32_t __user *encoder_id;
1099 struct drm_mode_group *mode_group;
1100
1101 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1102 return -EINVAL;
1103
1104 mutex_lock(&dev->mode_config.mutex);
1105
1106 /*
1107 * For the non-control nodes we need to limit the list of resources
1108 * by IDs in the group list for this node
1109 */
1110 list_for_each(lh, &file_priv->fbs)
1111 fb_count++;
1112
1113 mode_group = &file_priv->master->minor->mode_group;
1114 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1115
1116 list_for_each(lh, &dev->mode_config.crtc_list)
1117 crtc_count++;
1118
1119 list_for_each(lh, &dev->mode_config.connector_list)
1120 connector_count++;
1121
1122 list_for_each(lh, &dev->mode_config.encoder_list)
1123 encoder_count++;
1124 } else {
1125
1126 crtc_count = mode_group->num_crtcs;
1127 connector_count = mode_group->num_connectors;
1128 encoder_count = mode_group->num_encoders;
1129 }
1130
1131 card_res->max_height = dev->mode_config.max_height;
1132 card_res->min_height = dev->mode_config.min_height;
1133 card_res->max_width = dev->mode_config.max_width;
1134 card_res->min_width = dev->mode_config.min_width;
1135
1136 /* handle this in 4 parts */
1137 /* FBs */
1138 if (card_res->count_fbs >= fb_count) {
1139 copied = 0;
1140 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1141 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1142 if (put_user(fb->base.id, fb_id + copied)) {
1143 ret = -EFAULT;
1144 goto out;
1145 }
1146 copied++;
1147 }
1148 }
1149 card_res->count_fbs = fb_count;
1150
1151 /* CRTCs */
1152 if (card_res->count_crtcs >= crtc_count) {
1153 copied = 0;
1154 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1155 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1156 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1157 head) {
1158 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1159 if (put_user(crtc->base.id, crtc_id + copied)) {
1160 ret = -EFAULT;
1161 goto out;
1162 }
1163 copied++;
1164 }
1165 } else {
1166 for (i = 0; i < mode_group->num_crtcs; i++) {
1167 if (put_user(mode_group->id_list[i],
1168 crtc_id + copied)) {
1169 ret = -EFAULT;
1170 goto out;
1171 }
1172 copied++;
1173 }
1174 }
1175 }
1176 card_res->count_crtcs = crtc_count;
1177
1178 /* Encoders */
1179 if (card_res->count_encoders >= encoder_count) {
1180 copied = 0;
1181 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1182 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1183 list_for_each_entry(encoder,
1184 &dev->mode_config.encoder_list,
1185 head) {
1186 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1187 drm_get_encoder_name(encoder));
1188 if (put_user(encoder->base.id, encoder_id +
1189 copied)) {
1190 ret = -EFAULT;
1191 goto out;
1192 }
1193 copied++;
1194 }
1195 } else {
1196 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1197 if (put_user(mode_group->id_list[i],
1198 encoder_id + copied)) {
1199 ret = -EFAULT;
1200 goto out;
1201 }
1202 copied++;
1203 }
1204
1205 }
1206 }
1207 card_res->count_encoders = encoder_count;
1208
1209 /* Connectors */
1210 if (card_res->count_connectors >= connector_count) {
1211 copied = 0;
1212 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1213 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1214 list_for_each_entry(connector,
1215 &dev->mode_config.connector_list,
1216 head) {
1217 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1218 connector->base.id,
1219 drm_get_connector_name(connector));
1220 if (put_user(connector->base.id,
1221 connector_id + copied)) {
1222 ret = -EFAULT;
1223 goto out;
1224 }
1225 copied++;
1226 }
1227 } else {
1228 int start = mode_group->num_crtcs +
1229 mode_group->num_encoders;
1230 for (i = start; i < start + mode_group->num_connectors; i++) {
1231 if (put_user(mode_group->id_list[i],
1232 connector_id + copied)) {
1233 ret = -EFAULT;
1234 goto out;
1235 }
1236 copied++;
1237 }
1238 }
1239 }
1240 card_res->count_connectors = connector_count;
1241
1242 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1243 card_res->count_connectors, card_res->count_encoders);
1244
1245 out:
1246 mutex_unlock(&dev->mode_config.mutex);
1247 return ret;
1248 }
1249
1250 /**
1251 * drm_mode_getcrtc - get CRTC configuration
1252 * @inode: inode from the ioctl
1253 * @filp: file * from the ioctl
1254 * @cmd: cmd from ioctl
1255 * @arg: arg from ioctl
1256 *
1257 * LOCKING:
1258 * Takes mode config lock.
1259 *
1260 * Construct a CRTC configuration structure to return to the user.
1261 *
1262 * Called by the user via ioctl.
1263 *
1264 * RETURNS:
1265 * Zero on success, errno on failure.
1266 */
1267 int drm_mode_getcrtc(struct drm_device *dev,
1268 void *data, struct drm_file *file_priv)
1269 {
1270 struct drm_mode_crtc *crtc_resp = data;
1271 struct drm_crtc *crtc;
1272 struct drm_mode_object *obj;
1273 int ret = 0;
1274
1275 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1276 return -EINVAL;
1277
1278 mutex_lock(&dev->mode_config.mutex);
1279
1280 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1281 DRM_MODE_OBJECT_CRTC);
1282 if (!obj) {
1283 ret = -EINVAL;
1284 goto out;
1285 }
1286 crtc = obj_to_crtc(obj);
1287
1288 crtc_resp->x = crtc->x;
1289 crtc_resp->y = crtc->y;
1290 crtc_resp->gamma_size = crtc->gamma_size;
1291 if (crtc->fb)
1292 crtc_resp->fb_id = crtc->fb->base.id;
1293 else
1294 crtc_resp->fb_id = 0;
1295
1296 if (crtc->enabled) {
1297
1298 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1299 crtc_resp->mode_valid = 1;
1300
1301 } else {
1302 crtc_resp->mode_valid = 0;
1303 }
1304
1305 out:
1306 mutex_unlock(&dev->mode_config.mutex);
1307 return ret;
1308 }
1309
1310 /**
1311 * drm_mode_getconnector - get connector configuration
1312 * @inode: inode from the ioctl
1313 * @filp: file * from the ioctl
1314 * @cmd: cmd from ioctl
1315 * @arg: arg from ioctl
1316 *
1317 * LOCKING:
1318 * Takes mode config lock.
1319 *
1320 * Construct a connector configuration structure to return to the user.
1321 *
1322 * Called by the user via ioctl.
1323 *
1324 * RETURNS:
1325 * Zero on success, errno on failure.
1326 */
1327 int drm_mode_getconnector(struct drm_device *dev, void *data,
1328 struct drm_file *file_priv)
1329 {
1330 struct drm_mode_get_connector *out_resp = data;
1331 struct drm_mode_object *obj;
1332 struct drm_connector *connector;
1333 struct drm_display_mode *mode;
1334 int mode_count = 0;
1335 int props_count = 0;
1336 int encoders_count = 0;
1337 int ret = 0;
1338 int copied = 0;
1339 int i;
1340 struct drm_mode_modeinfo u_mode;
1341 struct drm_mode_modeinfo __user *mode_ptr;
1342 uint32_t __user *prop_ptr;
1343 uint64_t __user *prop_values;
1344 uint32_t __user *encoder_ptr;
1345
1346 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1347 return -EINVAL;
1348
1349 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1350
1351 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1352
1353 mutex_lock(&dev->mode_config.mutex);
1354
1355 obj = drm_mode_object_find(dev, out_resp->connector_id,
1356 DRM_MODE_OBJECT_CONNECTOR);
1357 if (!obj) {
1358 ret = -EINVAL;
1359 goto out;
1360 }
1361 connector = obj_to_connector(obj);
1362
1363 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1364 if (connector->property_ids[i] != 0) {
1365 props_count++;
1366 }
1367 }
1368
1369 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1370 if (connector->encoder_ids[i] != 0) {
1371 encoders_count++;
1372 }
1373 }
1374
1375 if (out_resp->count_modes == 0) {
1376 connector->funcs->fill_modes(connector,
1377 dev->mode_config.max_width,
1378 dev->mode_config.max_height);
1379 }
1380
1381 /* delayed so we get modes regardless of pre-fill_modes state */
1382 list_for_each_entry(mode, &connector->modes, head)
1383 mode_count++;
1384
1385 out_resp->connector_id = connector->base.id;
1386 out_resp->connector_type = connector->connector_type;
1387 out_resp->connector_type_id = connector->connector_type_id;
1388 out_resp->mm_width = connector->display_info.width_mm;
1389 out_resp->mm_height = connector->display_info.height_mm;
1390 out_resp->subpixel = connector->display_info.subpixel_order;
1391 out_resp->connection = connector->status;
1392 if (connector->encoder)
1393 out_resp->encoder_id = connector->encoder->base.id;
1394 else
1395 out_resp->encoder_id = 0;
1396
1397 /*
1398 * This ioctl is called twice, once to determine how much space is
1399 * needed, and the 2nd time to fill it.
1400 */
1401 if ((out_resp->count_modes >= mode_count) && mode_count) {
1402 copied = 0;
1403 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1404 list_for_each_entry(mode, &connector->modes, head) {
1405 drm_crtc_convert_to_umode(&u_mode, mode);
1406 if (copy_to_user(mode_ptr + copied,
1407 &u_mode, sizeof(u_mode))) {
1408 ret = -EFAULT;
1409 goto out;
1410 }
1411 copied++;
1412 }
1413 }
1414 out_resp->count_modes = mode_count;
1415
1416 if ((out_resp->count_props >= props_count) && props_count) {
1417 copied = 0;
1418 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1419 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1420 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1421 if (connector->property_ids[i] != 0) {
1422 if (put_user(connector->property_ids[i],
1423 prop_ptr + copied)) {
1424 ret = -EFAULT;
1425 goto out;
1426 }
1427
1428 if (put_user(connector->property_values[i],
1429 prop_values + copied)) {
1430 ret = -EFAULT;
1431 goto out;
1432 }
1433 copied++;
1434 }
1435 }
1436 }
1437 out_resp->count_props = props_count;
1438
1439 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1440 copied = 0;
1441 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1442 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1443 if (connector->encoder_ids[i] != 0) {
1444 if (put_user(connector->encoder_ids[i],
1445 encoder_ptr + copied)) {
1446 ret = -EFAULT;
1447 goto out;
1448 }
1449 copied++;
1450 }
1451 }
1452 }
1453 out_resp->count_encoders = encoders_count;
1454
1455 out:
1456 mutex_unlock(&dev->mode_config.mutex);
1457 return ret;
1458 }
1459
1460 int drm_mode_getencoder(struct drm_device *dev, void *data,
1461 struct drm_file *file_priv)
1462 {
1463 struct drm_mode_get_encoder *enc_resp = data;
1464 struct drm_mode_object *obj;
1465 struct drm_encoder *encoder;
1466 int ret = 0;
1467
1468 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1469 return -EINVAL;
1470
1471 mutex_lock(&dev->mode_config.mutex);
1472 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1473 DRM_MODE_OBJECT_ENCODER);
1474 if (!obj) {
1475 ret = -EINVAL;
1476 goto out;
1477 }
1478 encoder = obj_to_encoder(obj);
1479
1480 if (encoder->crtc)
1481 enc_resp->crtc_id = encoder->crtc->base.id;
1482 else
1483 enc_resp->crtc_id = 0;
1484 enc_resp->encoder_type = encoder->encoder_type;
1485 enc_resp->encoder_id = encoder->base.id;
1486 enc_resp->possible_crtcs = encoder->possible_crtcs;
1487 enc_resp->possible_clones = encoder->possible_clones;
1488
1489 out:
1490 mutex_unlock(&dev->mode_config.mutex);
1491 return ret;
1492 }
1493
1494 /**
1495 * drm_mode_getplane_res - get plane info
1496 * @dev: DRM device
1497 * @data: ioctl data
1498 * @file_priv: DRM file info
1499 *
1500 * LOCKING:
1501 * Takes mode config lock.
1502 *
1503 * Return an plane count and set of IDs.
1504 */
1505 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1506 struct drm_file *file_priv)
1507 {
1508 struct drm_mode_get_plane_res *plane_resp = data;
1509 struct drm_mode_config *config;
1510 struct drm_plane *plane;
1511 uint32_t __user *plane_ptr;
1512 int copied = 0, ret = 0;
1513
1514 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1515 return -EINVAL;
1516
1517 mutex_lock(&dev->mode_config.mutex);
1518 config = &dev->mode_config;
1519
1520 /*
1521 * This ioctl is called twice, once to determine how much space is
1522 * needed, and the 2nd time to fill it.
1523 */
1524 if (config->num_plane &&
1525 (plane_resp->count_planes >= config->num_plane)) {
1526 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1527
1528 list_for_each_entry(plane, &config->plane_list, head) {
1529 if (put_user(plane->base.id, plane_ptr + copied)) {
1530 ret = -EFAULT;
1531 goto out;
1532 }
1533 copied++;
1534 }
1535 }
1536 plane_resp->count_planes = config->num_plane;
1537
1538 out:
1539 mutex_unlock(&dev->mode_config.mutex);
1540 return ret;
1541 }
1542
1543 /**
1544 * drm_mode_getplane - get plane info
1545 * @dev: DRM device
1546 * @data: ioctl data
1547 * @file_priv: DRM file info
1548 *
1549 * LOCKING:
1550 * Takes mode config lock.
1551 *
1552 * Return plane info, including formats supported, gamma size, any
1553 * current fb, etc.
1554 */
1555 int drm_mode_getplane(struct drm_device *dev, void *data,
1556 struct drm_file *file_priv)
1557 {
1558 struct drm_mode_get_plane *plane_resp = data;
1559 struct drm_mode_object *obj;
1560 struct drm_plane *plane;
1561 uint32_t __user *format_ptr;
1562 int ret = 0;
1563
1564 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1565 return -EINVAL;
1566
1567 mutex_lock(&dev->mode_config.mutex);
1568 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1569 DRM_MODE_OBJECT_PLANE);
1570 if (!obj) {
1571 ret = -ENOENT;
1572 goto out;
1573 }
1574 plane = obj_to_plane(obj);
1575
1576 if (plane->crtc)
1577 plane_resp->crtc_id = plane->crtc->base.id;
1578 else
1579 plane_resp->crtc_id = 0;
1580
1581 if (plane->fb)
1582 plane_resp->fb_id = plane->fb->base.id;
1583 else
1584 plane_resp->fb_id = 0;
1585
1586 plane_resp->plane_id = plane->base.id;
1587 plane_resp->possible_crtcs = plane->possible_crtcs;
1588 plane_resp->gamma_size = plane->gamma_size;
1589
1590 /*
1591 * This ioctl is called twice, once to determine how much space is
1592 * needed, and the 2nd time to fill it.
1593 */
1594 if (plane->format_count &&
1595 (plane_resp->count_format_types >= plane->format_count)) {
1596 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1597 if (copy_to_user(format_ptr,
1598 plane->format_types,
1599 sizeof(uint32_t) * plane->format_count)) {
1600 ret = -EFAULT;
1601 goto out;
1602 }
1603 }
1604 plane_resp->count_format_types = plane->format_count;
1605
1606 out:
1607 mutex_unlock(&dev->mode_config.mutex);
1608 return ret;
1609 }
1610
1611 /**
1612 * drm_mode_setplane - set up or tear down an plane
1613 * @dev: DRM device
1614 * @data: ioctl data*
1615 * @file_prive: DRM file info
1616 *
1617 * LOCKING:
1618 * Takes mode config lock.
1619 *
1620 * Set plane info, including placement, fb, scaling, and other factors.
1621 * Or pass a NULL fb to disable.
1622 */
1623 int drm_mode_setplane(struct drm_device *dev, void *data,
1624 struct drm_file *file_priv)
1625 {
1626 struct drm_mode_set_plane *plane_req = data;
1627 struct drm_mode_object *obj;
1628 struct drm_plane *plane;
1629 struct drm_crtc *crtc;
1630 struct drm_framebuffer *fb;
1631 int ret = 0;
1632 unsigned int fb_width, fb_height;
1633 int i;
1634
1635 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1636 return -EINVAL;
1637
1638 mutex_lock(&dev->mode_config.mutex);
1639
1640 /*
1641 * First, find the plane, crtc, and fb objects. If not available,
1642 * we don't bother to call the driver.
1643 */
1644 obj = drm_mode_object_find(dev, plane_req->plane_id,
1645 DRM_MODE_OBJECT_PLANE);
1646 if (!obj) {
1647 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1648 plane_req->plane_id);
1649 ret = -ENOENT;
1650 goto out;
1651 }
1652 plane = obj_to_plane(obj);
1653
1654 /* No fb means shut it down */
1655 if (!plane_req->fb_id) {
1656 plane->funcs->disable_plane(plane);
1657 plane->crtc = NULL;
1658 plane->fb = NULL;
1659 goto out;
1660 }
1661
1662 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1663 DRM_MODE_OBJECT_CRTC);
1664 if (!obj) {
1665 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1666 plane_req->crtc_id);
1667 ret = -ENOENT;
1668 goto out;
1669 }
1670 crtc = obj_to_crtc(obj);
1671
1672 obj = drm_mode_object_find(dev, plane_req->fb_id,
1673 DRM_MODE_OBJECT_FB);
1674 if (!obj) {
1675 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1676 plane_req->fb_id);
1677 ret = -ENOENT;
1678 goto out;
1679 }
1680 fb = obj_to_fb(obj);
1681
1682 /* Check whether this plane supports the fb pixel format. */
1683 for (i = 0; i < plane->format_count; i++)
1684 if (fb->pixel_format == plane->format_types[i])
1685 break;
1686 if (i == plane->format_count) {
1687 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1688 ret = -EINVAL;
1689 goto out;
1690 }
1691
1692 fb_width = fb->width << 16;
1693 fb_height = fb->height << 16;
1694
1695 /* Make sure source coordinates are inside the fb. */
1696 if (plane_req->src_w > fb_width ||
1697 plane_req->src_x > fb_width - plane_req->src_w ||
1698 plane_req->src_h > fb_height ||
1699 plane_req->src_y > fb_height - plane_req->src_h) {
1700 DRM_DEBUG_KMS("Invalid source coordinates "
1701 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1702 plane_req->src_w >> 16,
1703 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1704 plane_req->src_h >> 16,
1705 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1706 plane_req->src_x >> 16,
1707 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1708 plane_req->src_y >> 16,
1709 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1710 ret = -ENOSPC;
1711 goto out;
1712 }
1713
1714 /* Give drivers some help against integer overflows */
1715 if (plane_req->crtc_w > INT_MAX ||
1716 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1717 plane_req->crtc_h > INT_MAX ||
1718 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1719 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1720 plane_req->crtc_w, plane_req->crtc_h,
1721 plane_req->crtc_x, plane_req->crtc_y);
1722 ret = -ERANGE;
1723 goto out;
1724 }
1725
1726 ret = plane->funcs->update_plane(plane, crtc, fb,
1727 plane_req->crtc_x, plane_req->crtc_y,
1728 plane_req->crtc_w, plane_req->crtc_h,
1729 plane_req->src_x, plane_req->src_y,
1730 plane_req->src_w, plane_req->src_h);
1731 if (!ret) {
1732 plane->crtc = crtc;
1733 plane->fb = fb;
1734 }
1735
1736 out:
1737 mutex_unlock(&dev->mode_config.mutex);
1738
1739 return ret;
1740 }
1741
1742 /**
1743 * drm_mode_setcrtc - set CRTC configuration
1744 * @inode: inode from the ioctl
1745 * @filp: file * from the ioctl
1746 * @cmd: cmd from ioctl
1747 * @arg: arg from ioctl
1748 *
1749 * LOCKING:
1750 * Takes mode config lock.
1751 *
1752 * Build a new CRTC configuration based on user request.
1753 *
1754 * Called by the user via ioctl.
1755 *
1756 * RETURNS:
1757 * Zero on success, errno on failure.
1758 */
1759 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1760 struct drm_file *file_priv)
1761 {
1762 struct drm_mode_config *config = &dev->mode_config;
1763 struct drm_mode_crtc *crtc_req = data;
1764 struct drm_mode_object *obj;
1765 struct drm_crtc *crtc;
1766 struct drm_connector **connector_set = NULL, *connector;
1767 struct drm_framebuffer *fb = NULL;
1768 struct drm_display_mode *mode = NULL;
1769 struct drm_mode_set set;
1770 uint32_t __user *set_connectors_ptr;
1771 int ret = 0;
1772 int i;
1773
1774 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1775 return -EINVAL;
1776
1777 mutex_lock(&dev->mode_config.mutex);
1778 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1779 DRM_MODE_OBJECT_CRTC);
1780 if (!obj) {
1781 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1782 ret = -EINVAL;
1783 goto out;
1784 }
1785 crtc = obj_to_crtc(obj);
1786 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1787
1788 if (crtc_req->mode_valid) {
1789 /* If we have a mode we need a framebuffer. */
1790 /* If we pass -1, set the mode with the currently bound fb */
1791 if (crtc_req->fb_id == -1) {
1792 if (!crtc->fb) {
1793 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1794 ret = -EINVAL;
1795 goto out;
1796 }
1797 fb = crtc->fb;
1798 } else {
1799 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1800 DRM_MODE_OBJECT_FB);
1801 if (!obj) {
1802 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1803 crtc_req->fb_id);
1804 ret = -EINVAL;
1805 goto out;
1806 }
1807 fb = obj_to_fb(obj);
1808 }
1809
1810 mode = drm_mode_create(dev);
1811 drm_crtc_convert_umode(mode, &crtc_req->mode);
1812 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1813 }
1814
1815 if (crtc_req->count_connectors == 0 && mode) {
1816 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1817 ret = -EINVAL;
1818 goto out;
1819 }
1820
1821 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1822 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1823 crtc_req->count_connectors);
1824 ret = -EINVAL;
1825 goto out;
1826 }
1827
1828 if (crtc_req->count_connectors > 0) {
1829 u32 out_id;
1830
1831 /* Avoid unbounded kernel memory allocation */
1832 if (crtc_req->count_connectors > config->num_connector) {
1833 ret = -EINVAL;
1834 goto out;
1835 }
1836
1837 connector_set = kmalloc(crtc_req->count_connectors *
1838 sizeof(struct drm_connector *),
1839 GFP_KERNEL);
1840 if (!connector_set) {
1841 ret = -ENOMEM;
1842 goto out;
1843 }
1844
1845 for (i = 0; i < crtc_req->count_connectors; i++) {
1846 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
1847 if (get_user(out_id, &set_connectors_ptr[i])) {
1848 ret = -EFAULT;
1849 goto out;
1850 }
1851
1852 obj = drm_mode_object_find(dev, out_id,
1853 DRM_MODE_OBJECT_CONNECTOR);
1854 if (!obj) {
1855 DRM_DEBUG_KMS("Connector id %d unknown\n",
1856 out_id);
1857 ret = -EINVAL;
1858 goto out;
1859 }
1860 connector = obj_to_connector(obj);
1861 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1862 connector->base.id,
1863 drm_get_connector_name(connector));
1864
1865 connector_set[i] = connector;
1866 }
1867 }
1868
1869 set.crtc = crtc;
1870 set.x = crtc_req->x;
1871 set.y = crtc_req->y;
1872 set.mode = mode;
1873 set.connectors = connector_set;
1874 set.num_connectors = crtc_req->count_connectors;
1875 set.fb = fb;
1876 ret = crtc->funcs->set_config(&set);
1877
1878 out:
1879 kfree(connector_set);
1880 mutex_unlock(&dev->mode_config.mutex);
1881 return ret;
1882 }
1883
1884 int drm_mode_cursor_ioctl(struct drm_device *dev,
1885 void *data, struct drm_file *file_priv)
1886 {
1887 struct drm_mode_cursor *req = data;
1888 struct drm_mode_object *obj;
1889 struct drm_crtc *crtc;
1890 int ret = 0;
1891
1892 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1893 return -EINVAL;
1894
1895 if (!req->flags)
1896 return -EINVAL;
1897
1898 mutex_lock(&dev->mode_config.mutex);
1899 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1900 if (!obj) {
1901 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1902 ret = -EINVAL;
1903 goto out;
1904 }
1905 crtc = obj_to_crtc(obj);
1906
1907 if (req->flags & DRM_MODE_CURSOR_BO) {
1908 if (!crtc->funcs->cursor_set) {
1909 ret = -ENXIO;
1910 goto out;
1911 }
1912 /* Turns off the cursor if handle is 0 */
1913 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1914 req->width, req->height);
1915 }
1916
1917 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1918 if (crtc->funcs->cursor_move) {
1919 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1920 } else {
1921 ret = -EFAULT;
1922 goto out;
1923 }
1924 }
1925 out:
1926 mutex_unlock(&dev->mode_config.mutex);
1927 return ret;
1928 }
1929
1930 /* Original addfb only supported RGB formats, so figure out which one */
1931 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
1932 {
1933 uint32_t fmt;
1934
1935 switch (bpp) {
1936 case 8:
1937 fmt = DRM_FORMAT_RGB332;
1938 break;
1939 case 16:
1940 if (depth == 15)
1941 fmt = DRM_FORMAT_XRGB1555;
1942 else
1943 fmt = DRM_FORMAT_RGB565;
1944 break;
1945 case 24:
1946 fmt = DRM_FORMAT_RGB888;
1947 break;
1948 case 32:
1949 if (depth == 24)
1950 fmt = DRM_FORMAT_XRGB8888;
1951 else if (depth == 30)
1952 fmt = DRM_FORMAT_XRGB2101010;
1953 else
1954 fmt = DRM_FORMAT_ARGB8888;
1955 break;
1956 default:
1957 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
1958 fmt = DRM_FORMAT_XRGB8888;
1959 break;
1960 }
1961
1962 return fmt;
1963 }
1964 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
1965
1966 /**
1967 * drm_mode_addfb - add an FB to the graphics configuration
1968 * @inode: inode from the ioctl
1969 * @filp: file * from the ioctl
1970 * @cmd: cmd from ioctl
1971 * @arg: arg from ioctl
1972 *
1973 * LOCKING:
1974 * Takes mode config lock.
1975 *
1976 * Add a new FB to the specified CRTC, given a user request.
1977 *
1978 * Called by the user via ioctl.
1979 *
1980 * RETURNS:
1981 * Zero on success, errno on failure.
1982 */
1983 int drm_mode_addfb(struct drm_device *dev,
1984 void *data, struct drm_file *file_priv)
1985 {
1986 struct drm_mode_fb_cmd *or = data;
1987 struct drm_mode_fb_cmd2 r = {};
1988 struct drm_mode_config *config = &dev->mode_config;
1989 struct drm_framebuffer *fb;
1990 int ret = 0;
1991
1992 /* Use new struct with format internally */
1993 r.fb_id = or->fb_id;
1994 r.width = or->width;
1995 r.height = or->height;
1996 r.pitches[0] = or->pitch;
1997 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
1998 r.handles[0] = or->handle;
1999
2000 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2001 return -EINVAL;
2002
2003 if ((config->min_width > r.width) || (r.width > config->max_width))
2004 return -EINVAL;
2005
2006 if ((config->min_height > r.height) || (r.height > config->max_height))
2007 return -EINVAL;
2008
2009 mutex_lock(&dev->mode_config.mutex);
2010
2011 /* TODO check buffer is sufficiently large */
2012 /* TODO setup destructor callback */
2013
2014 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2015 if (IS_ERR(fb)) {
2016 DRM_ERROR("could not create framebuffer\n");
2017 ret = PTR_ERR(fb);
2018 goto out;
2019 }
2020
2021 or->fb_id = fb->base.id;
2022 list_add(&fb->filp_head, &file_priv->fbs);
2023 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2024
2025 out:
2026 mutex_unlock(&dev->mode_config.mutex);
2027 return ret;
2028 }
2029
2030 static int format_check(struct drm_mode_fb_cmd2 *r)
2031 {
2032 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2033
2034 switch (format) {
2035 case DRM_FORMAT_C8:
2036 case DRM_FORMAT_RGB332:
2037 case DRM_FORMAT_BGR233:
2038 case DRM_FORMAT_XRGB4444:
2039 case DRM_FORMAT_XBGR4444:
2040 case DRM_FORMAT_RGBX4444:
2041 case DRM_FORMAT_BGRX4444:
2042 case DRM_FORMAT_ARGB4444:
2043 case DRM_FORMAT_ABGR4444:
2044 case DRM_FORMAT_RGBA4444:
2045 case DRM_FORMAT_BGRA4444:
2046 case DRM_FORMAT_XRGB1555:
2047 case DRM_FORMAT_XBGR1555:
2048 case DRM_FORMAT_RGBX5551:
2049 case DRM_FORMAT_BGRX5551:
2050 case DRM_FORMAT_ARGB1555:
2051 case DRM_FORMAT_ABGR1555:
2052 case DRM_FORMAT_RGBA5551:
2053 case DRM_FORMAT_BGRA5551:
2054 case DRM_FORMAT_RGB565:
2055 case DRM_FORMAT_BGR565:
2056 case DRM_FORMAT_RGB888:
2057 case DRM_FORMAT_BGR888:
2058 case DRM_FORMAT_XRGB8888:
2059 case DRM_FORMAT_XBGR8888:
2060 case DRM_FORMAT_RGBX8888:
2061 case DRM_FORMAT_BGRX8888:
2062 case DRM_FORMAT_ARGB8888:
2063 case DRM_FORMAT_ABGR8888:
2064 case DRM_FORMAT_RGBA8888:
2065 case DRM_FORMAT_BGRA8888:
2066 case DRM_FORMAT_XRGB2101010:
2067 case DRM_FORMAT_XBGR2101010:
2068 case DRM_FORMAT_RGBX1010102:
2069 case DRM_FORMAT_BGRX1010102:
2070 case DRM_FORMAT_ARGB2101010:
2071 case DRM_FORMAT_ABGR2101010:
2072 case DRM_FORMAT_RGBA1010102:
2073 case DRM_FORMAT_BGRA1010102:
2074 case DRM_FORMAT_YUYV:
2075 case DRM_FORMAT_YVYU:
2076 case DRM_FORMAT_UYVY:
2077 case DRM_FORMAT_VYUY:
2078 case DRM_FORMAT_AYUV:
2079 case DRM_FORMAT_NV12:
2080 case DRM_FORMAT_NV21:
2081 case DRM_FORMAT_NV16:
2082 case DRM_FORMAT_NV61:
2083 case DRM_FORMAT_YUV410:
2084 case DRM_FORMAT_YVU410:
2085 case DRM_FORMAT_YUV411:
2086 case DRM_FORMAT_YVU411:
2087 case DRM_FORMAT_YUV420:
2088 case DRM_FORMAT_YVU420:
2089 case DRM_FORMAT_YUV422:
2090 case DRM_FORMAT_YVU422:
2091 case DRM_FORMAT_YUV444:
2092 case DRM_FORMAT_YVU444:
2093 return 0;
2094 default:
2095 return -EINVAL;
2096 }
2097 }
2098
2099 /**
2100 * drm_mode_addfb2 - add an FB to the graphics configuration
2101 * @inode: inode from the ioctl
2102 * @filp: file * from the ioctl
2103 * @cmd: cmd from ioctl
2104 * @arg: arg from ioctl
2105 *
2106 * LOCKING:
2107 * Takes mode config lock.
2108 *
2109 * Add a new FB to the specified CRTC, given a user request with format.
2110 *
2111 * Called by the user via ioctl.
2112 *
2113 * RETURNS:
2114 * Zero on success, errno on failure.
2115 */
2116 int drm_mode_addfb2(struct drm_device *dev,
2117 void *data, struct drm_file *file_priv)
2118 {
2119 struct drm_mode_fb_cmd2 *r = data;
2120 struct drm_mode_config *config = &dev->mode_config;
2121 struct drm_framebuffer *fb;
2122 int ret = 0;
2123
2124 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2125 return -EINVAL;
2126
2127 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2128 DRM_ERROR("bad framebuffer width %d, should be >= %d && <= %d\n",
2129 r->width, config->min_width, config->max_width);
2130 return -EINVAL;
2131 }
2132 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2133 DRM_ERROR("bad framebuffer height %d, should be >= %d && <= %d\n",
2134 r->height, config->min_height, config->max_height);
2135 return -EINVAL;
2136 }
2137
2138 ret = format_check(r);
2139 if (ret) {
2140 DRM_ERROR("bad framebuffer format 0x%08x\n", r->pixel_format);
2141 return ret;
2142 }
2143
2144 mutex_lock(&dev->mode_config.mutex);
2145
2146 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2147 if (IS_ERR(fb)) {
2148 DRM_ERROR("could not create framebuffer\n");
2149 ret = PTR_ERR(fb);
2150 goto out;
2151 }
2152
2153 r->fb_id = fb->base.id;
2154 list_add(&fb->filp_head, &file_priv->fbs);
2155 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2156
2157 out:
2158 mutex_unlock(&dev->mode_config.mutex);
2159 return ret;
2160 }
2161
2162 /**
2163 * drm_mode_rmfb - remove an FB from the configuration
2164 * @inode: inode from the ioctl
2165 * @filp: file * from the ioctl
2166 * @cmd: cmd from ioctl
2167 * @arg: arg from ioctl
2168 *
2169 * LOCKING:
2170 * Takes mode config lock.
2171 *
2172 * Remove the FB specified by the user.
2173 *
2174 * Called by the user via ioctl.
2175 *
2176 * RETURNS:
2177 * Zero on success, errno on failure.
2178 */
2179 int drm_mode_rmfb(struct drm_device *dev,
2180 void *data, struct drm_file *file_priv)
2181 {
2182 struct drm_mode_object *obj;
2183 struct drm_framebuffer *fb = NULL;
2184 struct drm_framebuffer *fbl = NULL;
2185 uint32_t *id = data;
2186 int ret = 0;
2187 int found = 0;
2188
2189 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2190 return -EINVAL;
2191
2192 mutex_lock(&dev->mode_config.mutex);
2193 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
2194 /* TODO check that we really get a framebuffer back. */
2195 if (!obj) {
2196 ret = -EINVAL;
2197 goto out;
2198 }
2199 fb = obj_to_fb(obj);
2200
2201 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2202 if (fb == fbl)
2203 found = 1;
2204
2205 if (!found) {
2206 ret = -EINVAL;
2207 goto out;
2208 }
2209
2210 /* TODO release all crtc connected to the framebuffer */
2211 /* TODO unhock the destructor from the buffer object */
2212
2213 list_del(&fb->filp_head);
2214 fb->funcs->destroy(fb);
2215
2216 out:
2217 mutex_unlock(&dev->mode_config.mutex);
2218 return ret;
2219 }
2220
2221 /**
2222 * drm_mode_getfb - get FB info
2223 * @inode: inode from the ioctl
2224 * @filp: file * from the ioctl
2225 * @cmd: cmd from ioctl
2226 * @arg: arg from ioctl
2227 *
2228 * LOCKING:
2229 * Takes mode config lock.
2230 *
2231 * Lookup the FB given its ID and return info about it.
2232 *
2233 * Called by the user via ioctl.
2234 *
2235 * RETURNS:
2236 * Zero on success, errno on failure.
2237 */
2238 int drm_mode_getfb(struct drm_device *dev,
2239 void *data, struct drm_file *file_priv)
2240 {
2241 struct drm_mode_fb_cmd *r = data;
2242 struct drm_mode_object *obj;
2243 struct drm_framebuffer *fb;
2244 int ret = 0;
2245
2246 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2247 return -EINVAL;
2248
2249 mutex_lock(&dev->mode_config.mutex);
2250 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2251 if (!obj) {
2252 ret = -EINVAL;
2253 goto out;
2254 }
2255 fb = obj_to_fb(obj);
2256
2257 r->height = fb->height;
2258 r->width = fb->width;
2259 r->depth = fb->depth;
2260 r->bpp = fb->bits_per_pixel;
2261 r->pitch = fb->pitches[0];
2262 fb->funcs->create_handle(fb, file_priv, &r->handle);
2263
2264 out:
2265 mutex_unlock(&dev->mode_config.mutex);
2266 return ret;
2267 }
2268
2269 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2270 void *data, struct drm_file *file_priv)
2271 {
2272 struct drm_clip_rect __user *clips_ptr;
2273 struct drm_clip_rect *clips = NULL;
2274 struct drm_mode_fb_dirty_cmd *r = data;
2275 struct drm_mode_object *obj;
2276 struct drm_framebuffer *fb;
2277 unsigned flags;
2278 int num_clips;
2279 int ret = 0;
2280
2281 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2282 return -EINVAL;
2283
2284 mutex_lock(&dev->mode_config.mutex);
2285 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2286 if (!obj) {
2287 ret = -EINVAL;
2288 goto out_err1;
2289 }
2290 fb = obj_to_fb(obj);
2291
2292 num_clips = r->num_clips;
2293 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2294
2295 if (!num_clips != !clips_ptr) {
2296 ret = -EINVAL;
2297 goto out_err1;
2298 }
2299
2300 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2301
2302 /* If userspace annotates copy, clips must come in pairs */
2303 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2304 ret = -EINVAL;
2305 goto out_err1;
2306 }
2307
2308 if (num_clips && clips_ptr) {
2309 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2310 ret = -EINVAL;
2311 goto out_err1;
2312 }
2313 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2314 if (!clips) {
2315 ret = -ENOMEM;
2316 goto out_err1;
2317 }
2318
2319 ret = copy_from_user(clips, clips_ptr,
2320 num_clips * sizeof(*clips));
2321 if (ret) {
2322 ret = -EFAULT;
2323 goto out_err2;
2324 }
2325 }
2326
2327 if (fb->funcs->dirty) {
2328 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2329 clips, num_clips);
2330 } else {
2331 ret = -ENOSYS;
2332 goto out_err2;
2333 }
2334
2335 out_err2:
2336 kfree(clips);
2337 out_err1:
2338 mutex_unlock(&dev->mode_config.mutex);
2339 return ret;
2340 }
2341
2342
2343 /**
2344 * drm_fb_release - remove and free the FBs on this file
2345 * @filp: file * from the ioctl
2346 *
2347 * LOCKING:
2348 * Takes mode config lock.
2349 *
2350 * Destroy all the FBs associated with @filp.
2351 *
2352 * Called by the user via ioctl.
2353 *
2354 * RETURNS:
2355 * Zero on success, errno on failure.
2356 */
2357 void drm_fb_release(struct drm_file *priv)
2358 {
2359 struct drm_device *dev = priv->minor->dev;
2360 struct drm_framebuffer *fb, *tfb;
2361
2362 mutex_lock(&dev->mode_config.mutex);
2363 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2364 list_del(&fb->filp_head);
2365 fb->funcs->destroy(fb);
2366 }
2367 mutex_unlock(&dev->mode_config.mutex);
2368 }
2369
2370 /**
2371 * drm_mode_attachmode - add a mode to the user mode list
2372 * @dev: DRM device
2373 * @connector: connector to add the mode to
2374 * @mode: mode to add
2375 *
2376 * Add @mode to @connector's user mode list.
2377 */
2378 static int drm_mode_attachmode(struct drm_device *dev,
2379 struct drm_connector *connector,
2380 struct drm_display_mode *mode)
2381 {
2382 int ret = 0;
2383
2384 list_add_tail(&mode->head, &connector->user_modes);
2385 return ret;
2386 }
2387
2388 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2389 struct drm_display_mode *mode)
2390 {
2391 struct drm_connector *connector;
2392 int ret = 0;
2393 struct drm_display_mode *dup_mode;
2394 int need_dup = 0;
2395 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2396 if (!connector->encoder)
2397 break;
2398 if (connector->encoder->crtc == crtc) {
2399 if (need_dup)
2400 dup_mode = drm_mode_duplicate(dev, mode);
2401 else
2402 dup_mode = mode;
2403 ret = drm_mode_attachmode(dev, connector, dup_mode);
2404 if (ret)
2405 return ret;
2406 need_dup = 1;
2407 }
2408 }
2409 return 0;
2410 }
2411 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2412
2413 static int drm_mode_detachmode(struct drm_device *dev,
2414 struct drm_connector *connector,
2415 struct drm_display_mode *mode)
2416 {
2417 int found = 0;
2418 int ret = 0;
2419 struct drm_display_mode *match_mode, *t;
2420
2421 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2422 if (drm_mode_equal(match_mode, mode)) {
2423 list_del(&match_mode->head);
2424 drm_mode_destroy(dev, match_mode);
2425 found = 1;
2426 break;
2427 }
2428 }
2429
2430 if (!found)
2431 ret = -EINVAL;
2432
2433 return ret;
2434 }
2435
2436 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2437 {
2438 struct drm_connector *connector;
2439
2440 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2441 drm_mode_detachmode(dev, connector, mode);
2442 }
2443 return 0;
2444 }
2445 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2446
2447 /**
2448 * drm_fb_attachmode - Attach a user mode to an connector
2449 * @inode: inode from the ioctl
2450 * @filp: file * from the ioctl
2451 * @cmd: cmd from ioctl
2452 * @arg: arg from ioctl
2453 *
2454 * This attaches a user specified mode to an connector.
2455 * Called by the user via ioctl.
2456 *
2457 * RETURNS:
2458 * Zero on success, errno on failure.
2459 */
2460 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2461 void *data, struct drm_file *file_priv)
2462 {
2463 struct drm_mode_mode_cmd *mode_cmd = data;
2464 struct drm_connector *connector;
2465 struct drm_display_mode *mode;
2466 struct drm_mode_object *obj;
2467 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2468 int ret = 0;
2469
2470 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2471 return -EINVAL;
2472
2473 mutex_lock(&dev->mode_config.mutex);
2474
2475 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2476 if (!obj) {
2477 ret = -EINVAL;
2478 goto out;
2479 }
2480 connector = obj_to_connector(obj);
2481
2482 mode = drm_mode_create(dev);
2483 if (!mode) {
2484 ret = -ENOMEM;
2485 goto out;
2486 }
2487
2488 drm_crtc_convert_umode(mode, umode);
2489
2490 ret = drm_mode_attachmode(dev, connector, mode);
2491 out:
2492 mutex_unlock(&dev->mode_config.mutex);
2493 return ret;
2494 }
2495
2496
2497 /**
2498 * drm_fb_detachmode - Detach a user specified mode from an connector
2499 * @inode: inode from the ioctl
2500 * @filp: file * from the ioctl
2501 * @cmd: cmd from ioctl
2502 * @arg: arg from ioctl
2503 *
2504 * Called by the user via ioctl.
2505 *
2506 * RETURNS:
2507 * Zero on success, errno on failure.
2508 */
2509 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2510 void *data, struct drm_file *file_priv)
2511 {
2512 struct drm_mode_object *obj;
2513 struct drm_mode_mode_cmd *mode_cmd = data;
2514 struct drm_connector *connector;
2515 struct drm_display_mode mode;
2516 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2517 int ret = 0;
2518
2519 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2520 return -EINVAL;
2521
2522 mutex_lock(&dev->mode_config.mutex);
2523
2524 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2525 if (!obj) {
2526 ret = -EINVAL;
2527 goto out;
2528 }
2529 connector = obj_to_connector(obj);
2530
2531 drm_crtc_convert_umode(&mode, umode);
2532 ret = drm_mode_detachmode(dev, connector, &mode);
2533 out:
2534 mutex_unlock(&dev->mode_config.mutex);
2535 return ret;
2536 }
2537
2538 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2539 const char *name, int num_values)
2540 {
2541 struct drm_property *property = NULL;
2542
2543 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2544 if (!property)
2545 return NULL;
2546
2547 if (num_values) {
2548 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2549 if (!property->values)
2550 goto fail;
2551 }
2552
2553 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2554 property->flags = flags;
2555 property->num_values = num_values;
2556 INIT_LIST_HEAD(&property->enum_blob_list);
2557
2558 if (name) {
2559 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2560 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2561 }
2562
2563 list_add_tail(&property->head, &dev->mode_config.property_list);
2564 return property;
2565 fail:
2566 kfree(property);
2567 return NULL;
2568 }
2569 EXPORT_SYMBOL(drm_property_create);
2570
2571 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2572 const char *name,
2573 const struct drm_prop_enum_list *props,
2574 int num_values)
2575 {
2576 struct drm_property *property;
2577 int i, ret;
2578
2579 flags |= DRM_MODE_PROP_ENUM;
2580
2581 property = drm_property_create(dev, flags, name, num_values);
2582 if (!property)
2583 return NULL;
2584
2585 for (i = 0; i < num_values; i++) {
2586 ret = drm_property_add_enum(property, i,
2587 props[i].type,
2588 props[i].name);
2589 if (ret) {
2590 drm_property_destroy(dev, property);
2591 return NULL;
2592 }
2593 }
2594
2595 return property;
2596 }
2597 EXPORT_SYMBOL(drm_property_create_enum);
2598
2599 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2600 const char *name,
2601 uint64_t min, uint64_t max)
2602 {
2603 struct drm_property *property;
2604
2605 flags |= DRM_MODE_PROP_RANGE;
2606
2607 property = drm_property_create(dev, flags, name, 2);
2608 if (!property)
2609 return NULL;
2610
2611 property->values[0] = min;
2612 property->values[1] = max;
2613
2614 return property;
2615 }
2616 EXPORT_SYMBOL(drm_property_create_range);
2617
2618 int drm_property_add_enum(struct drm_property *property, int index,
2619 uint64_t value, const char *name)
2620 {
2621 struct drm_property_enum *prop_enum;
2622
2623 if (!(property->flags & DRM_MODE_PROP_ENUM))
2624 return -EINVAL;
2625
2626 if (!list_empty(&property->enum_blob_list)) {
2627 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2628 if (prop_enum->value == value) {
2629 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2630 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2631 return 0;
2632 }
2633 }
2634 }
2635
2636 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2637 if (!prop_enum)
2638 return -ENOMEM;
2639
2640 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2641 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2642 prop_enum->value = value;
2643
2644 property->values[index] = value;
2645 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2646 return 0;
2647 }
2648 EXPORT_SYMBOL(drm_property_add_enum);
2649
2650 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2651 {
2652 struct drm_property_enum *prop_enum, *pt;
2653
2654 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2655 list_del(&prop_enum->head);
2656 kfree(prop_enum);
2657 }
2658
2659 if (property->num_values)
2660 kfree(property->values);
2661 drm_mode_object_put(dev, &property->base);
2662 list_del(&property->head);
2663 kfree(property);
2664 }
2665 EXPORT_SYMBOL(drm_property_destroy);
2666
2667 int drm_connector_attach_property(struct drm_connector *connector,
2668 struct drm_property *property, uint64_t init_val)
2669 {
2670 int i;
2671
2672 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2673 if (connector->property_ids[i] == 0) {
2674 connector->property_ids[i] = property->base.id;
2675 connector->property_values[i] = init_val;
2676 break;
2677 }
2678 }
2679
2680 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2681 return -EINVAL;
2682 return 0;
2683 }
2684 EXPORT_SYMBOL(drm_connector_attach_property);
2685
2686 int drm_connector_property_set_value(struct drm_connector *connector,
2687 struct drm_property *property, uint64_t value)
2688 {
2689 int i;
2690
2691 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2692 if (connector->property_ids[i] == property->base.id) {
2693 connector->property_values[i] = value;
2694 break;
2695 }
2696 }
2697
2698 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2699 return -EINVAL;
2700 return 0;
2701 }
2702 EXPORT_SYMBOL(drm_connector_property_set_value);
2703
2704 int drm_connector_property_get_value(struct drm_connector *connector,
2705 struct drm_property *property, uint64_t *val)
2706 {
2707 int i;
2708
2709 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2710 if (connector->property_ids[i] == property->base.id) {
2711 *val = connector->property_values[i];
2712 break;
2713 }
2714 }
2715
2716 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2717 return -EINVAL;
2718 return 0;
2719 }
2720 EXPORT_SYMBOL(drm_connector_property_get_value);
2721
2722 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2723 void *data, struct drm_file *file_priv)
2724 {
2725 struct drm_mode_object *obj;
2726 struct drm_mode_get_property *out_resp = data;
2727 struct drm_property *property;
2728 int enum_count = 0;
2729 int blob_count = 0;
2730 int value_count = 0;
2731 int ret = 0, i;
2732 int copied;
2733 struct drm_property_enum *prop_enum;
2734 struct drm_mode_property_enum __user *enum_ptr;
2735 struct drm_property_blob *prop_blob;
2736 uint32_t __user *blob_id_ptr;
2737 uint64_t __user *values_ptr;
2738 uint32_t __user *blob_length_ptr;
2739
2740 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2741 return -EINVAL;
2742
2743 mutex_lock(&dev->mode_config.mutex);
2744 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2745 if (!obj) {
2746 ret = -EINVAL;
2747 goto done;
2748 }
2749 property = obj_to_property(obj);
2750
2751 if (property->flags & DRM_MODE_PROP_ENUM) {
2752 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2753 enum_count++;
2754 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2755 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2756 blob_count++;
2757 }
2758
2759 value_count = property->num_values;
2760
2761 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2762 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2763 out_resp->flags = property->flags;
2764
2765 if ((out_resp->count_values >= value_count) && value_count) {
2766 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
2767 for (i = 0; i < value_count; i++) {
2768 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2769 ret = -EFAULT;
2770 goto done;
2771 }
2772 }
2773 }
2774 out_resp->count_values = value_count;
2775
2776 if (property->flags & DRM_MODE_PROP_ENUM) {
2777 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2778 copied = 0;
2779 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
2780 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2781
2782 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2783 ret = -EFAULT;
2784 goto done;
2785 }
2786
2787 if (copy_to_user(&enum_ptr[copied].name,
2788 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2789 ret = -EFAULT;
2790 goto done;
2791 }
2792 copied++;
2793 }
2794 }
2795 out_resp->count_enum_blobs = enum_count;
2796 }
2797
2798 if (property->flags & DRM_MODE_PROP_BLOB) {
2799 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2800 copied = 0;
2801 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2802 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
2803
2804 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2805 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2806 ret = -EFAULT;
2807 goto done;
2808 }
2809
2810 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2811 ret = -EFAULT;
2812 goto done;
2813 }
2814
2815 copied++;
2816 }
2817 }
2818 out_resp->count_enum_blobs = blob_count;
2819 }
2820 done:
2821 mutex_unlock(&dev->mode_config.mutex);
2822 return ret;
2823 }
2824
2825 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2826 void *data)
2827 {
2828 struct drm_property_blob *blob;
2829
2830 if (!length || !data)
2831 return NULL;
2832
2833 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2834 if (!blob)
2835 return NULL;
2836
2837 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2838 blob->length = length;
2839
2840 memcpy(blob->data, data, length);
2841
2842 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2843
2844 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2845 return blob;
2846 }
2847
2848 static void drm_property_destroy_blob(struct drm_device *dev,
2849 struct drm_property_blob *blob)
2850 {
2851 drm_mode_object_put(dev, &blob->base);
2852 list_del(&blob->head);
2853 kfree(blob);
2854 }
2855
2856 int drm_mode_getblob_ioctl(struct drm_device *dev,
2857 void *data, struct drm_file *file_priv)
2858 {
2859 struct drm_mode_object *obj;
2860 struct drm_mode_get_blob *out_resp = data;
2861 struct drm_property_blob *blob;
2862 int ret = 0;
2863 void __user *blob_ptr;
2864
2865 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2866 return -EINVAL;
2867
2868 mutex_lock(&dev->mode_config.mutex);
2869 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2870 if (!obj) {
2871 ret = -EINVAL;
2872 goto done;
2873 }
2874 blob = obj_to_blob(obj);
2875
2876 if (out_resp->length == blob->length) {
2877 blob_ptr = (void __user *)(unsigned long)out_resp->data;
2878 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2879 ret = -EFAULT;
2880 goto done;
2881 }
2882 }
2883 out_resp->length = blob->length;
2884
2885 done:
2886 mutex_unlock(&dev->mode_config.mutex);
2887 return ret;
2888 }
2889
2890 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2891 struct edid *edid)
2892 {
2893 struct drm_device *dev = connector->dev;
2894 int ret = 0, size;
2895
2896 if (connector->edid_blob_ptr)
2897 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2898
2899 /* Delete edid, when there is none. */
2900 if (!edid) {
2901 connector->edid_blob_ptr = NULL;
2902 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2903 return ret;
2904 }
2905
2906 size = EDID_LENGTH * (1 + edid->extensions);
2907 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
2908 size, edid);
2909
2910 ret = drm_connector_property_set_value(connector,
2911 dev->mode_config.edid_property,
2912 connector->edid_blob_ptr->base.id);
2913
2914 return ret;
2915 }
2916 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2917
2918 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2919 void *data, struct drm_file *file_priv)
2920 {
2921 struct drm_mode_connector_set_property *out_resp = data;
2922 struct drm_mode_object *obj;
2923 struct drm_property *property;
2924 struct drm_connector *connector;
2925 int ret = -EINVAL;
2926 int i;
2927
2928 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2929 return -EINVAL;
2930
2931 mutex_lock(&dev->mode_config.mutex);
2932
2933 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2934 if (!obj) {
2935 goto out;
2936 }
2937 connector = obj_to_connector(obj);
2938
2939 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2940 if (connector->property_ids[i] == out_resp->prop_id)
2941 break;
2942 }
2943
2944 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2945 goto out;
2946 }
2947
2948 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2949 if (!obj) {
2950 goto out;
2951 }
2952 property = obj_to_property(obj);
2953
2954 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2955 goto out;
2956
2957 if (property->flags & DRM_MODE_PROP_RANGE) {
2958 if (out_resp->value < property->values[0])
2959 goto out;
2960
2961 if (out_resp->value > property->values[1])
2962 goto out;
2963 } else {
2964 int found = 0;
2965 for (i = 0; i < property->num_values; i++) {
2966 if (property->values[i] == out_resp->value) {
2967 found = 1;
2968 break;
2969 }
2970 }
2971 if (!found) {
2972 goto out;
2973 }
2974 }
2975
2976 /* Do DPMS ourselves */
2977 if (property == connector->dev->mode_config.dpms_property) {
2978 if (connector->funcs->dpms)
2979 (*connector->funcs->dpms)(connector, (int) out_resp->value);
2980 ret = 0;
2981 } else if (connector->funcs->set_property)
2982 ret = connector->funcs->set_property(connector, property, out_resp->value);
2983
2984 /* store the property value if successful */
2985 if (!ret)
2986 drm_connector_property_set_value(connector, property, out_resp->value);
2987 out:
2988 mutex_unlock(&dev->mode_config.mutex);
2989 return ret;
2990 }
2991
2992 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2993 struct drm_encoder *encoder)
2994 {
2995 int i;
2996
2997 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2998 if (connector->encoder_ids[i] == 0) {
2999 connector->encoder_ids[i] = encoder->base.id;
3000 return 0;
3001 }
3002 }
3003 return -ENOMEM;
3004 }
3005 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3006
3007 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3008 struct drm_encoder *encoder)
3009 {
3010 int i;
3011 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3012 if (connector->encoder_ids[i] == encoder->base.id) {
3013 connector->encoder_ids[i] = 0;
3014 if (connector->encoder == encoder)
3015 connector->encoder = NULL;
3016 break;
3017 }
3018 }
3019 }
3020 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3021
3022 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3023 int gamma_size)
3024 {
3025 crtc->gamma_size = gamma_size;
3026
3027 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3028 if (!crtc->gamma_store) {
3029 crtc->gamma_size = 0;
3030 return -ENOMEM;
3031 }
3032
3033 return 0;
3034 }
3035 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3036
3037 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3038 void *data, struct drm_file *file_priv)
3039 {
3040 struct drm_mode_crtc_lut *crtc_lut = data;
3041 struct drm_mode_object *obj;
3042 struct drm_crtc *crtc;
3043 void *r_base, *g_base, *b_base;
3044 int size;
3045 int ret = 0;
3046
3047 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3048 return -EINVAL;
3049
3050 mutex_lock(&dev->mode_config.mutex);
3051 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3052 if (!obj) {
3053 ret = -EINVAL;
3054 goto out;
3055 }
3056 crtc = obj_to_crtc(obj);
3057
3058 /* memcpy into gamma store */
3059 if (crtc_lut->gamma_size != crtc->gamma_size) {
3060 ret = -EINVAL;
3061 goto out;
3062 }
3063
3064 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3065 r_base = crtc->gamma_store;
3066 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3067 ret = -EFAULT;
3068 goto out;
3069 }
3070
3071 g_base = r_base + size;
3072 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3073 ret = -EFAULT;
3074 goto out;
3075 }
3076
3077 b_base = g_base + size;
3078 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3079 ret = -EFAULT;
3080 goto out;
3081 }
3082
3083 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3084
3085 out:
3086 mutex_unlock(&dev->mode_config.mutex);
3087 return ret;
3088
3089 }
3090
3091 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3092 void *data, struct drm_file *file_priv)
3093 {
3094 struct drm_mode_crtc_lut *crtc_lut = data;
3095 struct drm_mode_object *obj;
3096 struct drm_crtc *crtc;
3097 void *r_base, *g_base, *b_base;
3098 int size;
3099 int ret = 0;
3100
3101 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3102 return -EINVAL;
3103
3104 mutex_lock(&dev->mode_config.mutex);
3105 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3106 if (!obj) {
3107 ret = -EINVAL;
3108 goto out;
3109 }
3110 crtc = obj_to_crtc(obj);
3111
3112 /* memcpy into gamma store */
3113 if (crtc_lut->gamma_size != crtc->gamma_size) {
3114 ret = -EINVAL;
3115 goto out;
3116 }
3117
3118 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3119 r_base = crtc->gamma_store;
3120 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3121 ret = -EFAULT;
3122 goto out;
3123 }
3124
3125 g_base = r_base + size;
3126 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3127 ret = -EFAULT;
3128 goto out;
3129 }
3130
3131 b_base = g_base + size;
3132 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3133 ret = -EFAULT;
3134 goto out;
3135 }
3136 out:
3137 mutex_unlock(&dev->mode_config.mutex);
3138 return ret;
3139 }
3140
3141 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3142 void *data, struct drm_file *file_priv)
3143 {
3144 struct drm_mode_crtc_page_flip *page_flip = data;
3145 struct drm_mode_object *obj;
3146 struct drm_crtc *crtc;
3147 struct drm_framebuffer *fb;
3148 struct drm_pending_vblank_event *e = NULL;
3149 unsigned long flags;
3150 int ret = -EINVAL;
3151
3152 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3153 page_flip->reserved != 0)
3154 return -EINVAL;
3155
3156 mutex_lock(&dev->mode_config.mutex);
3157 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3158 if (!obj)
3159 goto out;
3160 crtc = obj_to_crtc(obj);
3161
3162 if (crtc->fb == NULL) {
3163 /* The framebuffer is currently unbound, presumably
3164 * due to a hotplug event, that userspace has not
3165 * yet discovered.
3166 */
3167 ret = -EBUSY;
3168 goto out;
3169 }
3170
3171 if (crtc->funcs->page_flip == NULL)
3172 goto out;
3173
3174 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3175 if (!obj)
3176 goto out;
3177 fb = obj_to_fb(obj);
3178
3179 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3180 ret = -ENOMEM;
3181 spin_lock_irqsave(&dev->event_lock, flags);
3182 if (file_priv->event_space < sizeof e->event) {
3183 spin_unlock_irqrestore(&dev->event_lock, flags);
3184 goto out;
3185 }
3186 file_priv->event_space -= sizeof e->event;
3187 spin_unlock_irqrestore(&dev->event_lock, flags);
3188
3189 e = kzalloc(sizeof *e, GFP_KERNEL);
3190 if (e == NULL) {
3191 spin_lock_irqsave(&dev->event_lock, flags);
3192 file_priv->event_space += sizeof e->event;
3193 spin_unlock_irqrestore(&dev->event_lock, flags);
3194 goto out;
3195 }
3196
3197 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3198 e->event.base.length = sizeof e->event;
3199 e->event.user_data = page_flip->user_data;
3200 e->base.event = &e->event.base;
3201 e->base.file_priv = file_priv;
3202 e->base.destroy =
3203 (void (*) (struct drm_pending_event *)) kfree;
3204 }
3205
3206 ret = crtc->funcs->page_flip(crtc, fb, e);
3207 if (ret) {
3208 spin_lock_irqsave(&dev->event_lock, flags);
3209 file_priv->event_space += sizeof e->event;
3210 spin_unlock_irqrestore(&dev->event_lock, flags);
3211 kfree(e);
3212 }
3213
3214 out:
3215 mutex_unlock(&dev->mode_config.mutex);
3216 return ret;
3217 }
3218
3219 void drm_mode_config_reset(struct drm_device *dev)
3220 {
3221 struct drm_crtc *crtc;
3222 struct drm_encoder *encoder;
3223 struct drm_connector *connector;
3224
3225 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3226 if (crtc->funcs->reset)
3227 crtc->funcs->reset(crtc);
3228
3229 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3230 if (encoder->funcs->reset)
3231 encoder->funcs->reset(encoder);
3232
3233 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3234 if (connector->funcs->reset)
3235 connector->funcs->reset(connector);
3236 }
3237 EXPORT_SYMBOL(drm_mode_config_reset);
3238
3239 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3240 void *data, struct drm_file *file_priv)
3241 {
3242 struct drm_mode_create_dumb *args = data;
3243
3244 if (!dev->driver->dumb_create)
3245 return -ENOSYS;
3246 return dev->driver->dumb_create(file_priv, dev, args);
3247 }
3248
3249 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3250 void *data, struct drm_file *file_priv)
3251 {
3252 struct drm_mode_map_dumb *args = data;
3253
3254 /* call driver ioctl to get mmap offset */
3255 if (!dev->driver->dumb_map_offset)
3256 return -ENOSYS;
3257
3258 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3259 }
3260
3261 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3262 void *data, struct drm_file *file_priv)
3263 {
3264 struct drm_mode_destroy_dumb *args = data;
3265
3266 if (!dev->driver->dumb_destroy)
3267 return -ENOSYS;
3268
3269 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3270 }
3271
3272 /*
3273 * Just need to support RGB formats here for compat with code that doesn't
3274 * use pixel formats directly yet.
3275 */
3276 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3277 int *bpp)
3278 {
3279 switch (format) {
3280 case DRM_FORMAT_RGB332:
3281 case DRM_FORMAT_BGR233:
3282 *depth = 8;
3283 *bpp = 8;
3284 break;
3285 case DRM_FORMAT_XRGB1555:
3286 case DRM_FORMAT_XBGR1555:
3287 case DRM_FORMAT_RGBX5551:
3288 case DRM_FORMAT_BGRX5551:
3289 case DRM_FORMAT_ARGB1555:
3290 case DRM_FORMAT_ABGR1555:
3291 case DRM_FORMAT_RGBA5551:
3292 case DRM_FORMAT_BGRA5551:
3293 *depth = 15;
3294 *bpp = 16;
3295 break;
3296 case DRM_FORMAT_RGB565:
3297 case DRM_FORMAT_BGR565:
3298 *depth = 16;
3299 *bpp = 16;
3300 break;
3301 case DRM_FORMAT_RGB888:
3302 case DRM_FORMAT_BGR888:
3303 *depth = 24;
3304 *bpp = 24;
3305 break;
3306 case DRM_FORMAT_XRGB8888:
3307 case DRM_FORMAT_XBGR8888:
3308 case DRM_FORMAT_RGBX8888:
3309 case DRM_FORMAT_BGRX8888:
3310 *depth = 24;
3311 *bpp = 32;
3312 break;
3313 case DRM_FORMAT_XRGB2101010:
3314 case DRM_FORMAT_XBGR2101010:
3315 case DRM_FORMAT_RGBX1010102:
3316 case DRM_FORMAT_BGRX1010102:
3317 case DRM_FORMAT_ARGB2101010:
3318 case DRM_FORMAT_ABGR2101010:
3319 case DRM_FORMAT_RGBA1010102:
3320 case DRM_FORMAT_BGRA1010102:
3321 *depth = 30;
3322 *bpp = 32;
3323 break;
3324 case DRM_FORMAT_ARGB8888:
3325 case DRM_FORMAT_ABGR8888:
3326 case DRM_FORMAT_RGBA8888:
3327 case DRM_FORMAT_BGRA8888:
3328 *depth = 32;
3329 *bpp = 32;
3330 break;
3331 default:
3332 DRM_DEBUG_KMS("unsupported pixel format\n");
3333 *depth = 0;
3334 *bpp = 0;
3335 break;
3336 }
3337 }
3338 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
This page took 0.161916 seconds and 4 git commands to generate.