drm: Change drm_display_mode::type to unsigned
[deliverable/linux.git] / drivers / gpu / drm / drm_crtc.c
CommitLineData
f453ba04
DA
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>
5a0e3ad6 33#include <linux/slab.h>
2d1a8a48 34#include <linux/export.h>
f453ba04
DA
35#include "drm.h"
36#include "drmP.h"
37#include "drm_crtc.h"
7466f4cc 38#include "drm_edid.h"
308e5bcb 39#include "drm_fourcc.h"
f453ba04 40
f453ba04
DA
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 */
56static 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
63DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
64
65/*
66 * Optional properties
67 */
68static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
69{
53bd8389
JB
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" },
f453ba04
DA
74};
75
76static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
77{
78 { DRM_MODE_DITHERING_OFF, "Off" },
79 { DRM_MODE_DITHERING_ON, "On" },
92897b5c 80 { DRM_MODE_DITHERING_AUTO, "Automatic" },
f453ba04
DA
81};
82
83/*
84 * Non-global properties, but "required" for certain connectors.
85 */
86static 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
93DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
94
95static 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
102DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
103 drm_dvi_i_subconnector_enum_list)
104
105static 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 */
aeaa1ad3 111 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
f453ba04
DA
112};
113
114DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
115
116static 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 */
aeaa1ad3 122 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
f453ba04
DA
123};
124
125DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
126 drm_tv_subconnector_enum_list)
127
884840aa
JB
128static 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
134DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
135 drm_dirty_info_enum_list)
136
f453ba04
DA
137struct drm_conn_prop_enum_list {
138 int type;
139 char *name;
140 int count;
141};
142
143/*
144 * Connector and encoder types.
145 */
146static 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 },
e76116ca
AD
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 },
74bd3c26 160 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
e76116ca 161 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
a7331e5c 162 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
f453ba04
DA
163};
164
165static 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" },
a7331e5c 171 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
f453ba04
DA
172};
173
174char *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}
13a8195b 183EXPORT_SYMBOL(drm_get_encoder_name);
f453ba04
DA
184
185char *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}
194EXPORT_SYMBOL(drm_get_connector_name);
195
196char *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:
f453ba04
DA
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 */
221static 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
f453ba04
DA
227again:
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
ad2563c2 233 mutex_lock(&dev->mode_config.idr_mutex);
f453ba04 234 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
ad2563c2 235 mutex_unlock(&dev->mode_config.idr_mutex);
f453ba04
DA
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 */
254static void drm_mode_object_put(struct drm_device *dev,
255 struct drm_mode_object *object)
256{
ad2563c2 257 mutex_lock(&dev->mode_config.idr_mutex);
f453ba04 258 idr_remove(&dev->mode_config.crtc_idr, object->id);
ad2563c2 259 mutex_unlock(&dev->mode_config.idr_mutex);
f453ba04
DA
260}
261
7a9c9060
DV
262struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
263 uint32_t id, uint32_t type)
f453ba04 264{
ad2563c2 265 struct drm_mode_object *obj = NULL;
f453ba04 266
ad2563c2 267 mutex_lock(&dev->mode_config.idr_mutex);
f453ba04
DA
268 obj = idr_find(&dev->mode_config.crtc_idr, id);
269 if (!obj || (obj->type != type) || (obj->id != id))
ad2563c2
JB
270 obj = NULL;
271 mutex_unlock(&dev->mode_config.idr_mutex);
f453ba04
DA
272
273 return obj;
274}
275EXPORT_SYMBOL(drm_mode_object_find);
276
f453ba04
DA
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:
af901ca1 288 * Zero on success, error code on failure.
f453ba04
DA
289 */
290int 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}
307EXPORT_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 */
319void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
320{
321 struct drm_device *dev = fb->dev;
322 struct drm_crtc *crtc;
8cf5c917 323 struct drm_plane *plane;
5ef5f72f
DA
324 struct drm_mode_set set;
325 int ret;
f453ba04
DA
326
327 /* remove from any CRTC */
328 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5ef5f72f
DA
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 }
f453ba04
DA
338 }
339
8cf5c917
JB
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");
a9971157
RC
346 /* disconnect the plane from the fb and crtc: */
347 plane->fb = NULL;
348 plane->crtc = NULL;
8cf5c917
JB
349 }
350 }
351
f453ba04
DA
352 drm_mode_object_put(dev, &fb->base);
353 list_del(&fb->head);
354 dev->mode_config.num_fb--;
355}
356EXPORT_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 */
369void 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}
382EXPORT_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 */
394void 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}
407EXPORT_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 */
419void drm_mode_probed_add(struct drm_connector *connector,
420 struct drm_display_mode *mode)
421{
422 list_add(&mode->head, &connector->probed_modes);
423}
424EXPORT_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 */
436void drm_mode_remove(struct drm_connector *connector,
437 struct drm_display_mode *mode)
438{
439 list_del(&mode->head);
554f1d78 440 drm_mode_destroy(connector->dev, mode);
f453ba04
DA
441}
442EXPORT_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:
b20f3867 452 * Takes mode config lock.
f453ba04
DA
453 *
454 * Initialises a preallocated connector. Connectors should be
455 * subclassed as part of driver connector objects.
456 */
457void 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
a7331e5c
TH
478 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
479 drm_connector_attach_property(connector,
480 dev->mode_config.edid_property,
481 0);
f453ba04
DA
482
483 drm_connector_attach_property(connector,
484 dev->mode_config.dpms_property, 0);
485
486 mutex_unlock(&dev->mode_config.mutex);
487}
488EXPORT_SYMBOL(drm_connector_init);
489
490/**
491 * drm_connector_cleanup - cleans up an initialised connector
492 * @connector: connector to cleanup
493 *
494 * LOCKING:
b20f3867 495 * Takes mode config lock.
f453ba04
DA
496 *
497 * Cleans up the connector but doesn't free the object.
498 */
499void 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);
6380c509 516 dev->mode_config.num_connector--;
f453ba04
DA
517 mutex_unlock(&dev->mode_config.mutex);
518}
519EXPORT_SYMBOL(drm_connector_cleanup);
520
521void 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}
539EXPORT_SYMBOL(drm_encoder_init);
540
541void 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);
6380c509 547 dev->mode_config.num_encoder--;
f453ba04
DA
548 mutex_unlock(&dev->mode_config.mutex);
549}
550EXPORT_SYMBOL(drm_encoder_cleanup);
551
8cf5c917
JB
552int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
553 unsigned long possible_crtcs,
554 const struct drm_plane_funcs *funcs,
0a7eb243
RC
555 const uint32_t *formats, uint32_t format_count,
556 bool priv)
8cf5c917
JB
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);
10bf573b 568 mutex_unlock(&dev->mode_config.mutex);
8cf5c917
JB
569 return -ENOMEM;
570 }
571
308e5bcb 572 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
8cf5c917
JB
573 plane->format_count = format_count;
574 plane->possible_crtcs = possible_crtcs;
575
0a7eb243
RC
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 }
8cf5c917
JB
586
587 mutex_unlock(&dev->mode_config.mutex);
588
589 return 0;
590}
591EXPORT_SYMBOL(drm_plane_init);
592
593void 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);
0a7eb243
RC
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 }
8cf5c917
JB
605 mutex_unlock(&dev->mode_config.mutex);
606}
607EXPORT_SYMBOL(drm_plane_cleanup);
608
f453ba04
DA
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 */
621struct 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}
632EXPORT_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 */
644void 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}
650EXPORT_SYMBOL(drm_mode_destroy);
651
652static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
653{
654 struct drm_property *edid;
655 struct drm_property *dpms;
f453ba04
DA
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
4a67d391
SH
665 dpms = drm_property_create_enum(dev, 0,
666 "DPMS", drm_dpms_enum_list,
667 ARRAY_SIZE(drm_dpms_enum_list));
f453ba04
DA
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 */
679int 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;
f453ba04
DA
683
684 if (dev->mode_config.dvi_i_select_subconnector_property)
685 return 0;
686
687 dvi_i_selector =
4a67d391 688 drm_property_create_enum(dev, 0,
f453ba04 689 "select subconnector",
4a67d391 690 drm_dvi_i_select_enum_list,
f453ba04 691 ARRAY_SIZE(drm_dvi_i_select_enum_list));
f453ba04
DA
692 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
693
4a67d391 694 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
f453ba04 695 "subconnector",
4a67d391 696 drm_dvi_i_subconnector_enum_list,
f453ba04 697 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
f453ba04
DA
698 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
699
700 return 0;
701}
702EXPORT_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 */
715int 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 */
4a67d391 728 tv_selector = drm_property_create_enum(dev, 0,
f453ba04 729 "select subconnector",
4a67d391 730 drm_tv_select_enum_list,
f453ba04 731 ARRAY_SIZE(drm_tv_select_enum_list));
f453ba04
DA
732 dev->mode_config.tv_select_subconnector_property = tv_selector;
733
734 tv_subconnector =
4a67d391
SH
735 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
736 "subconnector",
737 drm_tv_subconnector_enum_list,
f453ba04 738 ARRAY_SIZE(drm_tv_subconnector_enum_list));
f453ba04
DA
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 =
d9bc3c02 745 drm_property_create_range(dev, 0, "left margin", 0, 100);
f453ba04
DA
746
747 dev->mode_config.tv_right_margin_property =
d9bc3c02 748 drm_property_create_range(dev, 0, "right margin", 0, 100);
f453ba04
DA
749
750 dev->mode_config.tv_top_margin_property =
d9bc3c02 751 drm_property_create_range(dev, 0, "top margin", 0, 100);
f453ba04
DA
752
753 dev->mode_config.tv_bottom_margin_property =
d9bc3c02 754 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
f453ba04
DA
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
b6b7902e 763 dev->mode_config.tv_brightness_property =
d9bc3c02 764 drm_property_create_range(dev, 0, "brightness", 0, 100);
b6b7902e
FJ
765
766 dev->mode_config.tv_contrast_property =
d9bc3c02 767 drm_property_create_range(dev, 0, "contrast", 0, 100);
b6b7902e
FJ
768
769 dev->mode_config.tv_flicker_reduction_property =
d9bc3c02 770 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
b6b7902e 771
a75f0236 772 dev->mode_config.tv_overscan_property =
d9bc3c02 773 drm_property_create_range(dev, 0, "overscan", 0, 100);
a75f0236
FJ
774
775 dev->mode_config.tv_saturation_property =
d9bc3c02 776 drm_property_create_range(dev, 0, "saturation", 0, 100);
a75f0236
FJ
777
778 dev->mode_config.tv_hue_property =
d9bc3c02 779 drm_property_create_range(dev, 0, "hue", 0, 100);
a75f0236 780
f453ba04
DA
781 return 0;
782}
783EXPORT_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 */
792int drm_mode_create_scaling_mode_property(struct drm_device *dev)
793{
794 struct drm_property *scaling_mode;
f453ba04
DA
795
796 if (dev->mode_config.scaling_mode_property)
797 return 0;
798
799 scaling_mode =
4a67d391
SH
800 drm_property_create_enum(dev, 0, "scaling mode",
801 drm_scaling_mode_enum_list,
f453ba04 802 ARRAY_SIZE(drm_scaling_mode_enum_list));
f453ba04
DA
803
804 dev->mode_config.scaling_mode_property = scaling_mode;
805
806 return 0;
807}
808EXPORT_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 */
817int drm_mode_create_dithering_property(struct drm_device *dev)
818{
819 struct drm_property *dithering_mode;
f453ba04
DA
820
821 if (dev->mode_config.dithering_mode_property)
822 return 0;
823
824 dithering_mode =
4a67d391
SH
825 drm_property_create_enum(dev, 0, "dithering",
826 drm_dithering_mode_enum_list,
f453ba04 827 ARRAY_SIZE(drm_dithering_mode_enum_list));
f453ba04
DA
828 dev->mode_config.dithering_mode_property = dithering_mode;
829
830 return 0;
831}
832EXPORT_SYMBOL(drm_mode_create_dithering_property);
833
884840aa
JB
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 */
841int drm_mode_create_dirty_info_property(struct drm_device *dev)
842{
843 struct drm_property *dirty_info;
884840aa
JB
844
845 if (dev->mode_config.dirty_info_property)
846 return 0;
847
848 dirty_info =
4a67d391 849 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
884840aa 850 "dirty",
4a67d391 851 drm_dirty_info_enum_list,
884840aa 852 ARRAY_SIZE(drm_dirty_info_enum_list));
884840aa
JB
853 dev->mode_config.dirty_info_property = dirty_info;
854
855 return 0;
856}
857EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
858
f453ba04
DA
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 */
869void drm_mode_config_init(struct drm_device *dev)
870{
871 mutex_init(&dev->mode_config.mutex);
ad2563c2 872 mutex_init(&dev->mode_config.idr_mutex);
f453ba04 873 INIT_LIST_HEAD(&dev->mode_config.fb_list);
f453ba04
DA
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);
8cf5c917 879 INIT_LIST_HEAD(&dev->mode_config.plane_list);
f453ba04
DA
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;
f453ba04
DA
891}
892EXPORT_SYMBOL(drm_mode_config_init);
893
894int 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
f453ba04
DA
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
912int 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 */
949void 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;
8cf5c917 956 struct drm_plane *plane, *plt;
f453ba04
DA
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
8cf5c917
JB
981 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
982 head) {
983 plane->funcs->destroy(plane);
984 }
59ce062e
SH
985
986 idr_remove_all(&dev->mode_config.crtc_idr);
987 idr_destroy(&dev->mode_config.crtc_idr);
f453ba04
DA
988}
989EXPORT_SYMBOL(drm_mode_config_cleanup);
990
f453ba04
DA
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 */
1002void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1003 struct drm_display_mode *in)
1004{
1005 out->clock = in->clock;
1006 out->hdisplay = in->hdisplay;
1007 out->hsync_start = in->hsync_start;
1008 out->hsync_end = in->hsync_end;
1009 out->htotal = in->htotal;
1010 out->hskew = in->hskew;
1011 out->vdisplay = in->vdisplay;
1012 out->vsync_start = in->vsync_start;
1013 out->vsync_end = in->vsync_end;
1014 out->vtotal = in->vtotal;
1015 out->vscan = in->vscan;
1016 out->vrefresh = in->vrefresh;
1017 out->flags = in->flags;
1018 out->type = in->type;
1019 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1020 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1021}
1022
1023/**
1024 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1025 * @out: drm_display_mode to return to the user
1026 * @in: drm_mode_modeinfo to use
1027 *
1028 * LOCKING:
1029 * None.
1030 *
1031 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1032 * the caller.
1033 */
1034void drm_crtc_convert_umode(struct drm_display_mode *out,
1035 struct drm_mode_modeinfo *in)
1036{
1037 out->clock = in->clock;
1038 out->hdisplay = in->hdisplay;
1039 out->hsync_start = in->hsync_start;
1040 out->hsync_end = in->hsync_end;
1041 out->htotal = in->htotal;
1042 out->hskew = in->hskew;
1043 out->vdisplay = in->vdisplay;
1044 out->vsync_start = in->vsync_start;
1045 out->vsync_end = in->vsync_end;
1046 out->vtotal = in->vtotal;
1047 out->vscan = in->vscan;
1048 out->vrefresh = in->vrefresh;
1049 out->flags = in->flags;
1050 out->type = in->type;
1051 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1052 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1053}
1054
1055/**
1056 * drm_mode_getresources - get graphics configuration
1057 * @inode: inode from the ioctl
1058 * @filp: file * from the ioctl
1059 * @cmd: cmd from ioctl
1060 * @arg: arg from ioctl
1061 *
1062 * LOCKING:
1063 * Takes mode config lock.
1064 *
1065 * Construct a set of configuration description structures and return
1066 * them to the user, including CRTC, connector and framebuffer configuration.
1067 *
1068 * Called by the user via ioctl.
1069 *
1070 * RETURNS:
1071 * Zero on success, errno on failure.
1072 */
1073int drm_mode_getresources(struct drm_device *dev, void *data,
1074 struct drm_file *file_priv)
1075{
1076 struct drm_mode_card_res *card_res = data;
1077 struct list_head *lh;
1078 struct drm_framebuffer *fb;
1079 struct drm_connector *connector;
1080 struct drm_crtc *crtc;
1081 struct drm_encoder *encoder;
1082 int ret = 0;
1083 int connector_count = 0;
1084 int crtc_count = 0;
1085 int fb_count = 0;
1086 int encoder_count = 0;
1087 int copied = 0, i;
1088 uint32_t __user *fb_id;
1089 uint32_t __user *crtc_id;
1090 uint32_t __user *connector_id;
1091 uint32_t __user *encoder_id;
1092 struct drm_mode_group *mode_group;
1093
fb3b06c8
DA
1094 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1095 return -EINVAL;
1096
f453ba04
DA
1097 mutex_lock(&dev->mode_config.mutex);
1098
1099 /*
1100 * For the non-control nodes we need to limit the list of resources
1101 * by IDs in the group list for this node
1102 */
1103 list_for_each(lh, &file_priv->fbs)
1104 fb_count++;
1105
1106 mode_group = &file_priv->master->minor->mode_group;
1107 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1108
1109 list_for_each(lh, &dev->mode_config.crtc_list)
1110 crtc_count++;
1111
1112 list_for_each(lh, &dev->mode_config.connector_list)
1113 connector_count++;
1114
1115 list_for_each(lh, &dev->mode_config.encoder_list)
1116 encoder_count++;
1117 } else {
1118
1119 crtc_count = mode_group->num_crtcs;
1120 connector_count = mode_group->num_connectors;
1121 encoder_count = mode_group->num_encoders;
1122 }
1123
1124 card_res->max_height = dev->mode_config.max_height;
1125 card_res->min_height = dev->mode_config.min_height;
1126 card_res->max_width = dev->mode_config.max_width;
1127 card_res->min_width = dev->mode_config.min_width;
1128
1129 /* handle this in 4 parts */
1130 /* FBs */
1131 if (card_res->count_fbs >= fb_count) {
1132 copied = 0;
1133 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
618c75e4 1134 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
f453ba04
DA
1135 if (put_user(fb->base.id, fb_id + copied)) {
1136 ret = -EFAULT;
1137 goto out;
1138 }
1139 copied++;
1140 }
1141 }
1142 card_res->count_fbs = fb_count;
1143
1144 /* CRTCs */
1145 if (card_res->count_crtcs >= crtc_count) {
1146 copied = 0;
1147 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1148 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1149 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1150 head) {
9440106b 1151 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
f453ba04
DA
1152 if (put_user(crtc->base.id, crtc_id + copied)) {
1153 ret = -EFAULT;
1154 goto out;
1155 }
1156 copied++;
1157 }
1158 } else {
1159 for (i = 0; i < mode_group->num_crtcs; i++) {
1160 if (put_user(mode_group->id_list[i],
1161 crtc_id + copied)) {
1162 ret = -EFAULT;
1163 goto out;
1164 }
1165 copied++;
1166 }
1167 }
1168 }
1169 card_res->count_crtcs = crtc_count;
1170
1171 /* Encoders */
1172 if (card_res->count_encoders >= encoder_count) {
1173 copied = 0;
1174 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1175 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1176 list_for_each_entry(encoder,
1177 &dev->mode_config.encoder_list,
1178 head) {
9440106b
JG
1179 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1180 drm_get_encoder_name(encoder));
f453ba04
DA
1181 if (put_user(encoder->base.id, encoder_id +
1182 copied)) {
1183 ret = -EFAULT;
1184 goto out;
1185 }
1186 copied++;
1187 }
1188 } else {
1189 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1190 if (put_user(mode_group->id_list[i],
1191 encoder_id + copied)) {
1192 ret = -EFAULT;
1193 goto out;
1194 }
1195 copied++;
1196 }
1197
1198 }
1199 }
1200 card_res->count_encoders = encoder_count;
1201
1202 /* Connectors */
1203 if (card_res->count_connectors >= connector_count) {
1204 copied = 0;
1205 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1206 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1207 list_for_each_entry(connector,
1208 &dev->mode_config.connector_list,
1209 head) {
9440106b
JG
1210 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1211 connector->base.id,
1212 drm_get_connector_name(connector));
f453ba04
DA
1213 if (put_user(connector->base.id,
1214 connector_id + copied)) {
1215 ret = -EFAULT;
1216 goto out;
1217 }
1218 copied++;
1219 }
1220 } else {
1221 int start = mode_group->num_crtcs +
1222 mode_group->num_encoders;
1223 for (i = start; i < start + mode_group->num_connectors; i++) {
1224 if (put_user(mode_group->id_list[i],
1225 connector_id + copied)) {
1226 ret = -EFAULT;
1227 goto out;
1228 }
1229 copied++;
1230 }
1231 }
1232 }
1233 card_res->count_connectors = connector_count;
1234
9440106b 1235 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
f453ba04
DA
1236 card_res->count_connectors, card_res->count_encoders);
1237
1238out:
1239 mutex_unlock(&dev->mode_config.mutex);
1240 return ret;
1241}
1242
1243/**
1244 * drm_mode_getcrtc - get CRTC configuration
1245 * @inode: inode from the ioctl
1246 * @filp: file * from the ioctl
1247 * @cmd: cmd from ioctl
1248 * @arg: arg from ioctl
1249 *
1250 * LOCKING:
b20f3867 1251 * Takes mode config lock.
f453ba04
DA
1252 *
1253 * Construct a CRTC configuration structure to return to the user.
1254 *
1255 * Called by the user via ioctl.
1256 *
1257 * RETURNS:
1258 * Zero on success, errno on failure.
1259 */
1260int drm_mode_getcrtc(struct drm_device *dev,
1261 void *data, struct drm_file *file_priv)
1262{
1263 struct drm_mode_crtc *crtc_resp = data;
1264 struct drm_crtc *crtc;
1265 struct drm_mode_object *obj;
1266 int ret = 0;
1267
fb3b06c8
DA
1268 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1269 return -EINVAL;
1270
f453ba04
DA
1271 mutex_lock(&dev->mode_config.mutex);
1272
1273 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1274 DRM_MODE_OBJECT_CRTC);
1275 if (!obj) {
1276 ret = -EINVAL;
1277 goto out;
1278 }
1279 crtc = obj_to_crtc(obj);
1280
1281 crtc_resp->x = crtc->x;
1282 crtc_resp->y = crtc->y;
1283 crtc_resp->gamma_size = crtc->gamma_size;
1284 if (crtc->fb)
1285 crtc_resp->fb_id = crtc->fb->base.id;
1286 else
1287 crtc_resp->fb_id = 0;
1288
1289 if (crtc->enabled) {
1290
1291 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1292 crtc_resp->mode_valid = 1;
1293
1294 } else {
1295 crtc_resp->mode_valid = 0;
1296 }
1297
1298out:
1299 mutex_unlock(&dev->mode_config.mutex);
1300 return ret;
1301}
1302
1303/**
1304 * drm_mode_getconnector - get connector configuration
1305 * @inode: inode from the ioctl
1306 * @filp: file * from the ioctl
1307 * @cmd: cmd from ioctl
1308 * @arg: arg from ioctl
1309 *
1310 * LOCKING:
b20f3867 1311 * Takes mode config lock.
f453ba04
DA
1312 *
1313 * Construct a connector configuration structure to return to the user.
1314 *
1315 * Called by the user via ioctl.
1316 *
1317 * RETURNS:
1318 * Zero on success, errno on failure.
1319 */
1320int drm_mode_getconnector(struct drm_device *dev, void *data,
1321 struct drm_file *file_priv)
1322{
1323 struct drm_mode_get_connector *out_resp = data;
1324 struct drm_mode_object *obj;
1325 struct drm_connector *connector;
1326 struct drm_display_mode *mode;
1327 int mode_count = 0;
1328 int props_count = 0;
1329 int encoders_count = 0;
1330 int ret = 0;
1331 int copied = 0;
1332 int i;
1333 struct drm_mode_modeinfo u_mode;
1334 struct drm_mode_modeinfo __user *mode_ptr;
1335 uint32_t __user *prop_ptr;
1336 uint64_t __user *prop_values;
1337 uint32_t __user *encoder_ptr;
1338
fb3b06c8
DA
1339 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1340 return -EINVAL;
1341
f453ba04
DA
1342 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1343
9440106b 1344 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
f453ba04
DA
1345
1346 mutex_lock(&dev->mode_config.mutex);
1347
1348 obj = drm_mode_object_find(dev, out_resp->connector_id,
1349 DRM_MODE_OBJECT_CONNECTOR);
1350 if (!obj) {
1351 ret = -EINVAL;
1352 goto out;
1353 }
1354 connector = obj_to_connector(obj);
1355
1356 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1357 if (connector->property_ids[i] != 0) {
1358 props_count++;
1359 }
1360 }
1361
1362 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1363 if (connector->encoder_ids[i] != 0) {
1364 encoders_count++;
1365 }
1366 }
1367
1368 if (out_resp->count_modes == 0) {
1369 connector->funcs->fill_modes(connector,
1370 dev->mode_config.max_width,
1371 dev->mode_config.max_height);
1372 }
1373
1374 /* delayed so we get modes regardless of pre-fill_modes state */
1375 list_for_each_entry(mode, &connector->modes, head)
1376 mode_count++;
1377
1378 out_resp->connector_id = connector->base.id;
1379 out_resp->connector_type = connector->connector_type;
1380 out_resp->connector_type_id = connector->connector_type_id;
1381 out_resp->mm_width = connector->display_info.width_mm;
1382 out_resp->mm_height = connector->display_info.height_mm;
1383 out_resp->subpixel = connector->display_info.subpixel_order;
1384 out_resp->connection = connector->status;
1385 if (connector->encoder)
1386 out_resp->encoder_id = connector->encoder->base.id;
1387 else
1388 out_resp->encoder_id = 0;
1389
1390 /*
1391 * This ioctl is called twice, once to determine how much space is
1392 * needed, and the 2nd time to fill it.
1393 */
1394 if ((out_resp->count_modes >= mode_count) && mode_count) {
1395 copied = 0;
81f6c7f8 1396 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
f453ba04
DA
1397 list_for_each_entry(mode, &connector->modes, head) {
1398 drm_crtc_convert_to_umode(&u_mode, mode);
1399 if (copy_to_user(mode_ptr + copied,
1400 &u_mode, sizeof(u_mode))) {
1401 ret = -EFAULT;
1402 goto out;
1403 }
1404 copied++;
1405 }
1406 }
1407 out_resp->count_modes = mode_count;
1408
1409 if ((out_resp->count_props >= props_count) && props_count) {
1410 copied = 0;
81f6c7f8
VS
1411 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1412 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
f453ba04
DA
1413 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1414 if (connector->property_ids[i] != 0) {
1415 if (put_user(connector->property_ids[i],
1416 prop_ptr + copied)) {
1417 ret = -EFAULT;
1418 goto out;
1419 }
1420
1421 if (put_user(connector->property_values[i],
1422 prop_values + copied)) {
1423 ret = -EFAULT;
1424 goto out;
1425 }
1426 copied++;
1427 }
1428 }
1429 }
1430 out_resp->count_props = props_count;
1431
1432 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1433 copied = 0;
81f6c7f8 1434 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
f453ba04
DA
1435 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1436 if (connector->encoder_ids[i] != 0) {
1437 if (put_user(connector->encoder_ids[i],
1438 encoder_ptr + copied)) {
1439 ret = -EFAULT;
1440 goto out;
1441 }
1442 copied++;
1443 }
1444 }
1445 }
1446 out_resp->count_encoders = encoders_count;
1447
1448out:
1449 mutex_unlock(&dev->mode_config.mutex);
1450 return ret;
1451}
1452
1453int drm_mode_getencoder(struct drm_device *dev, void *data,
1454 struct drm_file *file_priv)
1455{
1456 struct drm_mode_get_encoder *enc_resp = data;
1457 struct drm_mode_object *obj;
1458 struct drm_encoder *encoder;
1459 int ret = 0;
1460
fb3b06c8
DA
1461 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1462 return -EINVAL;
1463
f453ba04
DA
1464 mutex_lock(&dev->mode_config.mutex);
1465 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1466 DRM_MODE_OBJECT_ENCODER);
1467 if (!obj) {
1468 ret = -EINVAL;
1469 goto out;
1470 }
1471 encoder = obj_to_encoder(obj);
1472
1473 if (encoder->crtc)
1474 enc_resp->crtc_id = encoder->crtc->base.id;
1475 else
1476 enc_resp->crtc_id = 0;
1477 enc_resp->encoder_type = encoder->encoder_type;
1478 enc_resp->encoder_id = encoder->base.id;
1479 enc_resp->possible_crtcs = encoder->possible_crtcs;
1480 enc_resp->possible_clones = encoder->possible_clones;
1481
1482out:
1483 mutex_unlock(&dev->mode_config.mutex);
1484 return ret;
1485}
1486
8cf5c917
JB
1487/**
1488 * drm_mode_getplane_res - get plane info
1489 * @dev: DRM device
1490 * @data: ioctl data
1491 * @file_priv: DRM file info
1492 *
b20f3867
SH
1493 * LOCKING:
1494 * Takes mode config lock.
1495 *
8cf5c917
JB
1496 * Return an plane count and set of IDs.
1497 */
1498int drm_mode_getplane_res(struct drm_device *dev, void *data,
1499 struct drm_file *file_priv)
1500{
1501 struct drm_mode_get_plane_res *plane_resp = data;
1502 struct drm_mode_config *config;
1503 struct drm_plane *plane;
1504 uint32_t __user *plane_ptr;
1505 int copied = 0, ret = 0;
1506
1507 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1508 return -EINVAL;
1509
1510 mutex_lock(&dev->mode_config.mutex);
1511 config = &dev->mode_config;
1512
1513 /*
1514 * This ioctl is called twice, once to determine how much space is
1515 * needed, and the 2nd time to fill it.
1516 */
1517 if (config->num_plane &&
1518 (plane_resp->count_planes >= config->num_plane)) {
81f6c7f8 1519 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
8cf5c917
JB
1520
1521 list_for_each_entry(plane, &config->plane_list, head) {
1522 if (put_user(plane->base.id, plane_ptr + copied)) {
1523 ret = -EFAULT;
1524 goto out;
1525 }
1526 copied++;
1527 }
1528 }
1529 plane_resp->count_planes = config->num_plane;
1530
1531out:
1532 mutex_unlock(&dev->mode_config.mutex);
1533 return ret;
1534}
1535
1536/**
1537 * drm_mode_getplane - get plane info
1538 * @dev: DRM device
1539 * @data: ioctl data
1540 * @file_priv: DRM file info
1541 *
b20f3867
SH
1542 * LOCKING:
1543 * Takes mode config lock.
1544 *
8cf5c917
JB
1545 * Return plane info, including formats supported, gamma size, any
1546 * current fb, etc.
1547 */
1548int drm_mode_getplane(struct drm_device *dev, void *data,
1549 struct drm_file *file_priv)
1550{
1551 struct drm_mode_get_plane *plane_resp = data;
1552 struct drm_mode_object *obj;
1553 struct drm_plane *plane;
1554 uint32_t __user *format_ptr;
1555 int ret = 0;
1556
1557 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1558 return -EINVAL;
1559
1560 mutex_lock(&dev->mode_config.mutex);
1561 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1562 DRM_MODE_OBJECT_PLANE);
1563 if (!obj) {
1564 ret = -ENOENT;
1565 goto out;
1566 }
1567 plane = obj_to_plane(obj);
1568
1569 if (plane->crtc)
1570 plane_resp->crtc_id = plane->crtc->base.id;
1571 else
1572 plane_resp->crtc_id = 0;
1573
1574 if (plane->fb)
1575 plane_resp->fb_id = plane->fb->base.id;
1576 else
1577 plane_resp->fb_id = 0;
1578
1579 plane_resp->plane_id = plane->base.id;
1580 plane_resp->possible_crtcs = plane->possible_crtcs;
1581 plane_resp->gamma_size = plane->gamma_size;
1582
1583 /*
1584 * This ioctl is called twice, once to determine how much space is
1585 * needed, and the 2nd time to fill it.
1586 */
1587 if (plane->format_count &&
1588 (plane_resp->count_format_types >= plane->format_count)) {
81f6c7f8 1589 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
8cf5c917
JB
1590 if (copy_to_user(format_ptr,
1591 plane->format_types,
1592 sizeof(uint32_t) * plane->format_count)) {
1593 ret = -EFAULT;
1594 goto out;
1595 }
1596 }
1597 plane_resp->count_format_types = plane->format_count;
1598
1599out:
1600 mutex_unlock(&dev->mode_config.mutex);
1601 return ret;
1602}
1603
1604/**
1605 * drm_mode_setplane - set up or tear down an plane
1606 * @dev: DRM device
1607 * @data: ioctl data*
1608 * @file_prive: DRM file info
1609 *
b20f3867
SH
1610 * LOCKING:
1611 * Takes mode config lock.
1612 *
8cf5c917
JB
1613 * Set plane info, including placement, fb, scaling, and other factors.
1614 * Or pass a NULL fb to disable.
1615 */
1616int drm_mode_setplane(struct drm_device *dev, void *data,
1617 struct drm_file *file_priv)
1618{
1619 struct drm_mode_set_plane *plane_req = data;
1620 struct drm_mode_object *obj;
1621 struct drm_plane *plane;
1622 struct drm_crtc *crtc;
1623 struct drm_framebuffer *fb;
1624 int ret = 0;
42ef8789 1625 unsigned int fb_width, fb_height;
62443be6 1626 int i;
8cf5c917
JB
1627
1628 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1629 return -EINVAL;
1630
1631 mutex_lock(&dev->mode_config.mutex);
1632
1633 /*
1634 * First, find the plane, crtc, and fb objects. If not available,
1635 * we don't bother to call the driver.
1636 */
1637 obj = drm_mode_object_find(dev, plane_req->plane_id,
1638 DRM_MODE_OBJECT_PLANE);
1639 if (!obj) {
1640 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1641 plane_req->plane_id);
1642 ret = -ENOENT;
1643 goto out;
1644 }
1645 plane = obj_to_plane(obj);
1646
1647 /* No fb means shut it down */
1648 if (!plane_req->fb_id) {
1649 plane->funcs->disable_plane(plane);
e5e3b44c
VS
1650 plane->crtc = NULL;
1651 plane->fb = NULL;
8cf5c917
JB
1652 goto out;
1653 }
1654
1655 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1656 DRM_MODE_OBJECT_CRTC);
1657 if (!obj) {
1658 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1659 plane_req->crtc_id);
1660 ret = -ENOENT;
1661 goto out;
1662 }
1663 crtc = obj_to_crtc(obj);
1664
1665 obj = drm_mode_object_find(dev, plane_req->fb_id,
1666 DRM_MODE_OBJECT_FB);
1667 if (!obj) {
1668 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1669 plane_req->fb_id);
1670 ret = -ENOENT;
1671 goto out;
1672 }
1673 fb = obj_to_fb(obj);
1674
62443be6
VS
1675 /* Check whether this plane supports the fb pixel format. */
1676 for (i = 0; i < plane->format_count; i++)
1677 if (fb->pixel_format == plane->format_types[i])
1678 break;
1679 if (i == plane->format_count) {
1680 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1681 ret = -EINVAL;
1682 goto out;
1683 }
1684
42ef8789
VS
1685 fb_width = fb->width << 16;
1686 fb_height = fb->height << 16;
1687
1688 /* Make sure source coordinates are inside the fb. */
1689 if (plane_req->src_w > fb_width ||
1690 plane_req->src_x > fb_width - plane_req->src_w ||
1691 plane_req->src_h > fb_height ||
1692 plane_req->src_y > fb_height - plane_req->src_h) {
1693 DRM_DEBUG_KMS("Invalid source coordinates "
1694 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1695 plane_req->src_w >> 16,
1696 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1697 plane_req->src_h >> 16,
1698 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1699 plane_req->src_x >> 16,
1700 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1701 plane_req->src_y >> 16,
1702 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1703 ret = -ENOSPC;
1704 goto out;
1705 }
1706
687a0400
VS
1707 /* Give drivers some help against integer overflows */
1708 if (plane_req->crtc_w > INT_MAX ||
1709 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1710 plane_req->crtc_h > INT_MAX ||
1711 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1712 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1713 plane_req->crtc_w, plane_req->crtc_h,
1714 plane_req->crtc_x, plane_req->crtc_y);
1715 ret = -ERANGE;
1716 goto out;
1717 }
1718
8cf5c917
JB
1719 ret = plane->funcs->update_plane(plane, crtc, fb,
1720 plane_req->crtc_x, plane_req->crtc_y,
1721 plane_req->crtc_w, plane_req->crtc_h,
1722 plane_req->src_x, plane_req->src_y,
1723 plane_req->src_w, plane_req->src_h);
1724 if (!ret) {
1725 plane->crtc = crtc;
1726 plane->fb = fb;
1727 }
1728
1729out:
1730 mutex_unlock(&dev->mode_config.mutex);
1731
1732 return ret;
1733}
1734
f453ba04
DA
1735/**
1736 * drm_mode_setcrtc - set CRTC configuration
1737 * @inode: inode from the ioctl
1738 * @filp: file * from the ioctl
1739 * @cmd: cmd from ioctl
1740 * @arg: arg from ioctl
1741 *
1742 * LOCKING:
b20f3867 1743 * Takes mode config lock.
f453ba04
DA
1744 *
1745 * Build a new CRTC configuration based on user request.
1746 *
1747 * Called by the user via ioctl.
1748 *
1749 * RETURNS:
1750 * Zero on success, errno on failure.
1751 */
1752int drm_mode_setcrtc(struct drm_device *dev, void *data,
1753 struct drm_file *file_priv)
1754{
1755 struct drm_mode_config *config = &dev->mode_config;
1756 struct drm_mode_crtc *crtc_req = data;
1757 struct drm_mode_object *obj;
6653cc8d 1758 struct drm_crtc *crtc;
f453ba04
DA
1759 struct drm_connector **connector_set = NULL, *connector;
1760 struct drm_framebuffer *fb = NULL;
1761 struct drm_display_mode *mode = NULL;
1762 struct drm_mode_set set;
1763 uint32_t __user *set_connectors_ptr;
1764 int ret = 0;
1765 int i;
1766
fb3b06c8
DA
1767 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1768 return -EINVAL;
1769
f453ba04
DA
1770 mutex_lock(&dev->mode_config.mutex);
1771 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1772 DRM_MODE_OBJECT_CRTC);
1773 if (!obj) {
58367ed6 1774 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
f453ba04
DA
1775 ret = -EINVAL;
1776 goto out;
1777 }
1778 crtc = obj_to_crtc(obj);
9440106b 1779 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
f453ba04
DA
1780
1781 if (crtc_req->mode_valid) {
1782 /* If we have a mode we need a framebuffer. */
1783 /* If we pass -1, set the mode with the currently bound fb */
1784 if (crtc_req->fb_id == -1) {
6653cc8d
VS
1785 if (!crtc->fb) {
1786 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1787 ret = -EINVAL;
1788 goto out;
f453ba04 1789 }
6653cc8d 1790 fb = crtc->fb;
f453ba04
DA
1791 } else {
1792 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1793 DRM_MODE_OBJECT_FB);
1794 if (!obj) {
58367ed6
ZY
1795 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1796 crtc_req->fb_id);
f453ba04
DA
1797 ret = -EINVAL;
1798 goto out;
1799 }
1800 fb = obj_to_fb(obj);
1801 }
1802
1803 mode = drm_mode_create(dev);
1804 drm_crtc_convert_umode(mode, &crtc_req->mode);
1805 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1806 }
1807
1808 if (crtc_req->count_connectors == 0 && mode) {
58367ed6 1809 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
f453ba04
DA
1810 ret = -EINVAL;
1811 goto out;
1812 }
1813
7781de74 1814 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
58367ed6 1815 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
f453ba04
DA
1816 crtc_req->count_connectors);
1817 ret = -EINVAL;
1818 goto out;
1819 }
1820
1821 if (crtc_req->count_connectors > 0) {
1822 u32 out_id;
1823
1824 /* Avoid unbounded kernel memory allocation */
1825 if (crtc_req->count_connectors > config->num_connector) {
1826 ret = -EINVAL;
1827 goto out;
1828 }
1829
1830 connector_set = kmalloc(crtc_req->count_connectors *
1831 sizeof(struct drm_connector *),
1832 GFP_KERNEL);
1833 if (!connector_set) {
1834 ret = -ENOMEM;
1835 goto out;
1836 }
1837
1838 for (i = 0; i < crtc_req->count_connectors; i++) {
81f6c7f8 1839 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
f453ba04
DA
1840 if (get_user(out_id, &set_connectors_ptr[i])) {
1841 ret = -EFAULT;
1842 goto out;
1843 }
1844
1845 obj = drm_mode_object_find(dev, out_id,
1846 DRM_MODE_OBJECT_CONNECTOR);
1847 if (!obj) {
58367ed6
ZY
1848 DRM_DEBUG_KMS("Connector id %d unknown\n",
1849 out_id);
f453ba04
DA
1850 ret = -EINVAL;
1851 goto out;
1852 }
1853 connector = obj_to_connector(obj);
9440106b
JG
1854 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1855 connector->base.id,
1856 drm_get_connector_name(connector));
f453ba04
DA
1857
1858 connector_set[i] = connector;
1859 }
1860 }
1861
1862 set.crtc = crtc;
1863 set.x = crtc_req->x;
1864 set.y = crtc_req->y;
1865 set.mode = mode;
1866 set.connectors = connector_set;
1867 set.num_connectors = crtc_req->count_connectors;
5ef5f72f 1868 set.fb = fb;
f453ba04
DA
1869 ret = crtc->funcs->set_config(&set);
1870
1871out:
1872 kfree(connector_set);
1873 mutex_unlock(&dev->mode_config.mutex);
1874 return ret;
1875}
1876
1877int drm_mode_cursor_ioctl(struct drm_device *dev,
1878 void *data, struct drm_file *file_priv)
1879{
1880 struct drm_mode_cursor *req = data;
1881 struct drm_mode_object *obj;
1882 struct drm_crtc *crtc;
1883 int ret = 0;
1884
fb3b06c8
DA
1885 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1886 return -EINVAL;
1887
acb4b992 1888 if (!req->flags)
f453ba04 1889 return -EINVAL;
f453ba04
DA
1890
1891 mutex_lock(&dev->mode_config.mutex);
e0c8463a 1892 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
f453ba04 1893 if (!obj) {
58367ed6 1894 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
f453ba04
DA
1895 ret = -EINVAL;
1896 goto out;
1897 }
1898 crtc = obj_to_crtc(obj);
1899
1900 if (req->flags & DRM_MODE_CURSOR_BO) {
1901 if (!crtc->funcs->cursor_set) {
f453ba04
DA
1902 ret = -ENXIO;
1903 goto out;
1904 }
1905 /* Turns off the cursor if handle is 0 */
1906 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1907 req->width, req->height);
1908 }
1909
1910 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1911 if (crtc->funcs->cursor_move) {
1912 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1913 } else {
f453ba04
DA
1914 ret = -EFAULT;
1915 goto out;
1916 }
1917 }
1918out:
1919 mutex_unlock(&dev->mode_config.mutex);
1920 return ret;
1921}
1922
308e5bcb
JB
1923/* Original addfb only supported RGB formats, so figure out which one */
1924uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
1925{
1926 uint32_t fmt;
1927
1928 switch (bpp) {
1929 case 8:
04b3924d 1930 fmt = DRM_FORMAT_RGB332;
308e5bcb
JB
1931 break;
1932 case 16:
1933 if (depth == 15)
04b3924d 1934 fmt = DRM_FORMAT_XRGB1555;
308e5bcb 1935 else
04b3924d 1936 fmt = DRM_FORMAT_RGB565;
308e5bcb
JB
1937 break;
1938 case 24:
04b3924d 1939 fmt = DRM_FORMAT_RGB888;
308e5bcb
JB
1940 break;
1941 case 32:
1942 if (depth == 24)
04b3924d 1943 fmt = DRM_FORMAT_XRGB8888;
308e5bcb 1944 else if (depth == 30)
04b3924d 1945 fmt = DRM_FORMAT_XRGB2101010;
308e5bcb 1946 else
04b3924d 1947 fmt = DRM_FORMAT_ARGB8888;
308e5bcb
JB
1948 break;
1949 default:
04b3924d
VS
1950 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
1951 fmt = DRM_FORMAT_XRGB8888;
308e5bcb
JB
1952 break;
1953 }
1954
1955 return fmt;
1956}
1957EXPORT_SYMBOL(drm_mode_legacy_fb_format);
1958
f453ba04
DA
1959/**
1960 * drm_mode_addfb - add an FB to the graphics configuration
1961 * @inode: inode from the ioctl
1962 * @filp: file * from the ioctl
1963 * @cmd: cmd from ioctl
1964 * @arg: arg from ioctl
1965 *
1966 * LOCKING:
1967 * Takes mode config lock.
1968 *
1969 * Add a new FB to the specified CRTC, given a user request.
1970 *
1971 * Called by the user via ioctl.
1972 *
1973 * RETURNS:
1974 * Zero on success, errno on failure.
1975 */
1976int drm_mode_addfb(struct drm_device *dev,
1977 void *data, struct drm_file *file_priv)
1978{
308e5bcb
JB
1979 struct drm_mode_fb_cmd *or = data;
1980 struct drm_mode_fb_cmd2 r = {};
1981 struct drm_mode_config *config = &dev->mode_config;
1982 struct drm_framebuffer *fb;
1983 int ret = 0;
1984
1985 /* Use new struct with format internally */
1986 r.fb_id = or->fb_id;
1987 r.width = or->width;
1988 r.height = or->height;
1989 r.pitches[0] = or->pitch;
1990 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
1991 r.handles[0] = or->handle;
1992
1993 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1994 return -EINVAL;
1995
acb4b992 1996 if ((config->min_width > r.width) || (r.width > config->max_width))
308e5bcb 1997 return -EINVAL;
acb4b992
JB
1998
1999 if ((config->min_height > r.height) || (r.height > config->max_height))
308e5bcb 2000 return -EINVAL;
308e5bcb
JB
2001
2002 mutex_lock(&dev->mode_config.mutex);
2003
2004 /* TODO check buffer is sufficiently large */
2005 /* TODO setup destructor callback */
2006
2007 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2008 if (IS_ERR(fb)) {
2009 DRM_ERROR("could not create framebuffer\n");
2010 ret = PTR_ERR(fb);
2011 goto out;
2012 }
2013
2014 or->fb_id = fb->base.id;
2015 list_add(&fb->filp_head, &file_priv->fbs);
2016 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2017
2018out:
2019 mutex_unlock(&dev->mode_config.mutex);
2020 return ret;
2021}
2022
935b5977
VS
2023static int format_check(struct drm_mode_fb_cmd2 *r)
2024{
2025 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2026
2027 switch (format) {
2028 case DRM_FORMAT_C8:
2029 case DRM_FORMAT_RGB332:
2030 case DRM_FORMAT_BGR233:
2031 case DRM_FORMAT_XRGB4444:
2032 case DRM_FORMAT_XBGR4444:
2033 case DRM_FORMAT_RGBX4444:
2034 case DRM_FORMAT_BGRX4444:
2035 case DRM_FORMAT_ARGB4444:
2036 case DRM_FORMAT_ABGR4444:
2037 case DRM_FORMAT_RGBA4444:
2038 case DRM_FORMAT_BGRA4444:
2039 case DRM_FORMAT_XRGB1555:
2040 case DRM_FORMAT_XBGR1555:
2041 case DRM_FORMAT_RGBX5551:
2042 case DRM_FORMAT_BGRX5551:
2043 case DRM_FORMAT_ARGB1555:
2044 case DRM_FORMAT_ABGR1555:
2045 case DRM_FORMAT_RGBA5551:
2046 case DRM_FORMAT_BGRA5551:
2047 case DRM_FORMAT_RGB565:
2048 case DRM_FORMAT_BGR565:
2049 case DRM_FORMAT_RGB888:
2050 case DRM_FORMAT_BGR888:
2051 case DRM_FORMAT_XRGB8888:
2052 case DRM_FORMAT_XBGR8888:
2053 case DRM_FORMAT_RGBX8888:
2054 case DRM_FORMAT_BGRX8888:
2055 case DRM_FORMAT_ARGB8888:
2056 case DRM_FORMAT_ABGR8888:
2057 case DRM_FORMAT_RGBA8888:
2058 case DRM_FORMAT_BGRA8888:
2059 case DRM_FORMAT_XRGB2101010:
2060 case DRM_FORMAT_XBGR2101010:
2061 case DRM_FORMAT_RGBX1010102:
2062 case DRM_FORMAT_BGRX1010102:
2063 case DRM_FORMAT_ARGB2101010:
2064 case DRM_FORMAT_ABGR2101010:
2065 case DRM_FORMAT_RGBA1010102:
2066 case DRM_FORMAT_BGRA1010102:
2067 case DRM_FORMAT_YUYV:
2068 case DRM_FORMAT_YVYU:
2069 case DRM_FORMAT_UYVY:
2070 case DRM_FORMAT_VYUY:
2071 case DRM_FORMAT_AYUV:
2072 case DRM_FORMAT_NV12:
2073 case DRM_FORMAT_NV21:
2074 case DRM_FORMAT_NV16:
2075 case DRM_FORMAT_NV61:
2076 case DRM_FORMAT_YUV410:
2077 case DRM_FORMAT_YVU410:
2078 case DRM_FORMAT_YUV411:
2079 case DRM_FORMAT_YVU411:
2080 case DRM_FORMAT_YUV420:
2081 case DRM_FORMAT_YVU420:
2082 case DRM_FORMAT_YUV422:
2083 case DRM_FORMAT_YVU422:
2084 case DRM_FORMAT_YUV444:
2085 case DRM_FORMAT_YVU444:
2086 return 0;
2087 default:
2088 return -EINVAL;
2089 }
2090}
2091
308e5bcb
JB
2092/**
2093 * drm_mode_addfb2 - add an FB to the graphics configuration
2094 * @inode: inode from the ioctl
2095 * @filp: file * from the ioctl
2096 * @cmd: cmd from ioctl
2097 * @arg: arg from ioctl
2098 *
2099 * LOCKING:
2100 * Takes mode config lock.
2101 *
2102 * Add a new FB to the specified CRTC, given a user request with format.
2103 *
2104 * Called by the user via ioctl.
2105 *
2106 * RETURNS:
2107 * Zero on success, errno on failure.
2108 */
2109int drm_mode_addfb2(struct drm_device *dev,
2110 void *data, struct drm_file *file_priv)
2111{
2112 struct drm_mode_fb_cmd2 *r = data;
f453ba04
DA
2113 struct drm_mode_config *config = &dev->mode_config;
2114 struct drm_framebuffer *fb;
2115 int ret = 0;
2116
fb3b06c8
DA
2117 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2118 return -EINVAL;
2119
f453ba04 2120 if ((config->min_width > r->width) || (r->width > config->max_width)) {
8cf5c917
JB
2121 DRM_ERROR("bad framebuffer width %d, should be >= %d && <= %d\n",
2122 r->width, config->min_width, config->max_width);
f453ba04
DA
2123 return -EINVAL;
2124 }
2125 if ((config->min_height > r->height) || (r->height > config->max_height)) {
8cf5c917
JB
2126 DRM_ERROR("bad framebuffer height %d, should be >= %d && <= %d\n",
2127 r->height, config->min_height, config->max_height);
f453ba04
DA
2128 return -EINVAL;
2129 }
2130
935b5977
VS
2131 ret = format_check(r);
2132 if (ret) {
2133 DRM_ERROR("bad framebuffer format 0x%08x\n", r->pixel_format);
2134 return ret;
2135 }
2136
f453ba04
DA
2137 mutex_lock(&dev->mode_config.mutex);
2138
f453ba04 2139 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
cce13ff7 2140 if (IS_ERR(fb)) {
f453ba04 2141 DRM_ERROR("could not create framebuffer\n");
cce13ff7 2142 ret = PTR_ERR(fb);
f453ba04
DA
2143 goto out;
2144 }
2145
e0c8463a 2146 r->fb_id = fb->base.id;
f453ba04 2147 list_add(&fb->filp_head, &file_priv->fbs);
9440106b 2148 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
f453ba04
DA
2149
2150out:
2151 mutex_unlock(&dev->mode_config.mutex);
2152 return ret;
2153}
2154
2155/**
2156 * drm_mode_rmfb - remove an FB from the configuration
2157 * @inode: inode from the ioctl
2158 * @filp: file * from the ioctl
2159 * @cmd: cmd from ioctl
2160 * @arg: arg from ioctl
2161 *
2162 * LOCKING:
2163 * Takes mode config lock.
2164 *
2165 * Remove the FB specified by the user.
2166 *
2167 * Called by the user via ioctl.
2168 *
2169 * RETURNS:
2170 * Zero on success, errno on failure.
2171 */
2172int drm_mode_rmfb(struct drm_device *dev,
2173 void *data, struct drm_file *file_priv)
2174{
2175 struct drm_mode_object *obj;
2176 struct drm_framebuffer *fb = NULL;
2177 struct drm_framebuffer *fbl = NULL;
2178 uint32_t *id = data;
2179 int ret = 0;
2180 int found = 0;
2181
fb3b06c8
DA
2182 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2183 return -EINVAL;
2184
f453ba04
DA
2185 mutex_lock(&dev->mode_config.mutex);
2186 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
25985edc 2187 /* TODO check that we really get a framebuffer back. */
f453ba04 2188 if (!obj) {
f453ba04
DA
2189 ret = -EINVAL;
2190 goto out;
2191 }
2192 fb = obj_to_fb(obj);
2193
2194 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2195 if (fb == fbl)
2196 found = 1;
2197
2198 if (!found) {
f453ba04
DA
2199 ret = -EINVAL;
2200 goto out;
2201 }
2202
2203 /* TODO release all crtc connected to the framebuffer */
2204 /* TODO unhock the destructor from the buffer object */
2205
2206 list_del(&fb->filp_head);
2207 fb->funcs->destroy(fb);
2208
2209out:
2210 mutex_unlock(&dev->mode_config.mutex);
2211 return ret;
2212}
2213
2214/**
2215 * drm_mode_getfb - get FB info
2216 * @inode: inode from the ioctl
2217 * @filp: file * from the ioctl
2218 * @cmd: cmd from ioctl
2219 * @arg: arg from ioctl
2220 *
2221 * LOCKING:
b20f3867 2222 * Takes mode config lock.
f453ba04
DA
2223 *
2224 * Lookup the FB given its ID and return info about it.
2225 *
2226 * Called by the user via ioctl.
2227 *
2228 * RETURNS:
2229 * Zero on success, errno on failure.
2230 */
2231int drm_mode_getfb(struct drm_device *dev,
2232 void *data, struct drm_file *file_priv)
2233{
2234 struct drm_mode_fb_cmd *r = data;
2235 struct drm_mode_object *obj;
2236 struct drm_framebuffer *fb;
2237 int ret = 0;
2238
fb3b06c8
DA
2239 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2240 return -EINVAL;
2241
f453ba04 2242 mutex_lock(&dev->mode_config.mutex);
e0c8463a 2243 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
f453ba04 2244 if (!obj) {
f453ba04
DA
2245 ret = -EINVAL;
2246 goto out;
2247 }
2248 fb = obj_to_fb(obj);
2249
2250 r->height = fb->height;
2251 r->width = fb->width;
2252 r->depth = fb->depth;
2253 r->bpp = fb->bits_per_pixel;
01f2c773 2254 r->pitch = fb->pitches[0];
f453ba04
DA
2255 fb->funcs->create_handle(fb, file_priv, &r->handle);
2256
2257out:
2258 mutex_unlock(&dev->mode_config.mutex);
2259 return ret;
2260}
2261
884840aa
JB
2262int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2263 void *data, struct drm_file *file_priv)
2264{
2265 struct drm_clip_rect __user *clips_ptr;
2266 struct drm_clip_rect *clips = NULL;
2267 struct drm_mode_fb_dirty_cmd *r = data;
2268 struct drm_mode_object *obj;
2269 struct drm_framebuffer *fb;
2270 unsigned flags;
2271 int num_clips;
2272 int ret = 0;
2273
fb3b06c8
DA
2274 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2275 return -EINVAL;
2276
884840aa
JB
2277 mutex_lock(&dev->mode_config.mutex);
2278 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2279 if (!obj) {
884840aa
JB
2280 ret = -EINVAL;
2281 goto out_err1;
2282 }
2283 fb = obj_to_fb(obj);
2284
2285 num_clips = r->num_clips;
81f6c7f8 2286 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
884840aa
JB
2287
2288 if (!num_clips != !clips_ptr) {
2289 ret = -EINVAL;
2290 goto out_err1;
2291 }
2292
2293 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2294
2295 /* If userspace annotates copy, clips must come in pairs */
2296 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2297 ret = -EINVAL;
2298 goto out_err1;
2299 }
2300
2301 if (num_clips && clips_ptr) {
a5cd3351
XW
2302 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2303 ret = -EINVAL;
2304 goto out_err1;
2305 }
884840aa
JB
2306 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2307 if (!clips) {
2308 ret = -ENOMEM;
2309 goto out_err1;
2310 }
2311
2312 ret = copy_from_user(clips, clips_ptr,
2313 num_clips * sizeof(*clips));
e902a358
DC
2314 if (ret) {
2315 ret = -EFAULT;
884840aa 2316 goto out_err2;
e902a358 2317 }
884840aa
JB
2318 }
2319
2320 if (fb->funcs->dirty) {
02b00162
TH
2321 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2322 clips, num_clips);
884840aa
JB
2323 } else {
2324 ret = -ENOSYS;
2325 goto out_err2;
2326 }
2327
2328out_err2:
2329 kfree(clips);
2330out_err1:
2331 mutex_unlock(&dev->mode_config.mutex);
2332 return ret;
2333}
2334
2335
f453ba04
DA
2336/**
2337 * drm_fb_release - remove and free the FBs on this file
2338 * @filp: file * from the ioctl
2339 *
2340 * LOCKING:
2341 * Takes mode config lock.
2342 *
2343 * Destroy all the FBs associated with @filp.
2344 *
2345 * Called by the user via ioctl.
2346 *
2347 * RETURNS:
2348 * Zero on success, errno on failure.
2349 */
ea39f835 2350void drm_fb_release(struct drm_file *priv)
f453ba04 2351{
f453ba04
DA
2352 struct drm_device *dev = priv->minor->dev;
2353 struct drm_framebuffer *fb, *tfb;
2354
2355 mutex_lock(&dev->mode_config.mutex);
2356 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2357 list_del(&fb->filp_head);
2358 fb->funcs->destroy(fb);
2359 }
2360 mutex_unlock(&dev->mode_config.mutex);
2361}
2362
2363/**
2364 * drm_mode_attachmode - add a mode to the user mode list
2365 * @dev: DRM device
2366 * @connector: connector to add the mode to
2367 * @mode: mode to add
2368 *
2369 * Add @mode to @connector's user mode list.
2370 */
2371static int drm_mode_attachmode(struct drm_device *dev,
2372 struct drm_connector *connector,
2373 struct drm_display_mode *mode)
2374{
2375 int ret = 0;
2376
2377 list_add_tail(&mode->head, &connector->user_modes);
2378 return ret;
2379}
2380
2381int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2382 struct drm_display_mode *mode)
2383{
2384 struct drm_connector *connector;
2385 int ret = 0;
2386 struct drm_display_mode *dup_mode;
2387 int need_dup = 0;
2388 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2389 if (!connector->encoder)
2390 break;
2391 if (connector->encoder->crtc == crtc) {
2392 if (need_dup)
2393 dup_mode = drm_mode_duplicate(dev, mode);
2394 else
2395 dup_mode = mode;
2396 ret = drm_mode_attachmode(dev, connector, dup_mode);
2397 if (ret)
2398 return ret;
2399 need_dup = 1;
2400 }
2401 }
2402 return 0;
2403}
2404EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2405
2406static int drm_mode_detachmode(struct drm_device *dev,
2407 struct drm_connector *connector,
2408 struct drm_display_mode *mode)
2409{
2410 int found = 0;
2411 int ret = 0;
2412 struct drm_display_mode *match_mode, *t;
2413
2414 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2415 if (drm_mode_equal(match_mode, mode)) {
2416 list_del(&match_mode->head);
2417 drm_mode_destroy(dev, match_mode);
2418 found = 1;
2419 break;
2420 }
2421 }
2422
2423 if (!found)
2424 ret = -EINVAL;
2425
2426 return ret;
2427}
2428
2429int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2430{
2431 struct drm_connector *connector;
2432
2433 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2434 drm_mode_detachmode(dev, connector, mode);
2435 }
2436 return 0;
2437}
2438EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2439
2440/**
2441 * drm_fb_attachmode - Attach a user mode to an connector
2442 * @inode: inode from the ioctl
2443 * @filp: file * from the ioctl
2444 * @cmd: cmd from ioctl
2445 * @arg: arg from ioctl
2446 *
2447 * This attaches a user specified mode to an connector.
2448 * Called by the user via ioctl.
2449 *
2450 * RETURNS:
2451 * Zero on success, errno on failure.
2452 */
2453int drm_mode_attachmode_ioctl(struct drm_device *dev,
2454 void *data, struct drm_file *file_priv)
2455{
2456 struct drm_mode_mode_cmd *mode_cmd = data;
2457 struct drm_connector *connector;
2458 struct drm_display_mode *mode;
2459 struct drm_mode_object *obj;
2460 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2461 int ret = 0;
2462
fb3b06c8
DA
2463 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2464 return -EINVAL;
2465
f453ba04
DA
2466 mutex_lock(&dev->mode_config.mutex);
2467
2468 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2469 if (!obj) {
2470 ret = -EINVAL;
2471 goto out;
2472 }
2473 connector = obj_to_connector(obj);
2474
2475 mode = drm_mode_create(dev);
2476 if (!mode) {
2477 ret = -ENOMEM;
2478 goto out;
2479 }
2480
2481 drm_crtc_convert_umode(mode, umode);
2482
2483 ret = drm_mode_attachmode(dev, connector, mode);
2484out:
2485 mutex_unlock(&dev->mode_config.mutex);
2486 return ret;
2487}
2488
2489
2490/**
2491 * drm_fb_detachmode - Detach a user specified mode from an connector
2492 * @inode: inode from the ioctl
2493 * @filp: file * from the ioctl
2494 * @cmd: cmd from ioctl
2495 * @arg: arg from ioctl
2496 *
2497 * Called by the user via ioctl.
2498 *
2499 * RETURNS:
2500 * Zero on success, errno on failure.
2501 */
2502int drm_mode_detachmode_ioctl(struct drm_device *dev,
2503 void *data, struct drm_file *file_priv)
2504{
2505 struct drm_mode_object *obj;
2506 struct drm_mode_mode_cmd *mode_cmd = data;
2507 struct drm_connector *connector;
2508 struct drm_display_mode mode;
2509 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2510 int ret = 0;
2511
fb3b06c8
DA
2512 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2513 return -EINVAL;
2514
f453ba04
DA
2515 mutex_lock(&dev->mode_config.mutex);
2516
2517 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2518 if (!obj) {
2519 ret = -EINVAL;
2520 goto out;
2521 }
2522 connector = obj_to_connector(obj);
2523
2524 drm_crtc_convert_umode(&mode, umode);
2525 ret = drm_mode_detachmode(dev, connector, &mode);
2526out:
2527 mutex_unlock(&dev->mode_config.mutex);
2528 return ret;
2529}
2530
2531struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2532 const char *name, int num_values)
2533{
2534 struct drm_property *property = NULL;
2535
2536 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2537 if (!property)
2538 return NULL;
2539
2540 if (num_values) {
2541 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2542 if (!property->values)
2543 goto fail;
2544 }
2545
2546 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2547 property->flags = flags;
2548 property->num_values = num_values;
2549 INIT_LIST_HEAD(&property->enum_blob_list);
2550
471dd2ef 2551 if (name) {
f453ba04 2552 strncpy(property->name, name, DRM_PROP_NAME_LEN);
471dd2ef
VL
2553 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2554 }
f453ba04
DA
2555
2556 list_add_tail(&property->head, &dev->mode_config.property_list);
2557 return property;
2558fail:
2559 kfree(property);
2560 return NULL;
2561}
2562EXPORT_SYMBOL(drm_property_create);
2563
4a67d391
SH
2564struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2565 const char *name,
2566 const struct drm_prop_enum_list *props,
2567 int num_values)
2568{
2569 struct drm_property *property;
2570 int i, ret;
2571
2572 flags |= DRM_MODE_PROP_ENUM;
2573
2574 property = drm_property_create(dev, flags, name, num_values);
2575 if (!property)
2576 return NULL;
2577
2578 for (i = 0; i < num_values; i++) {
2579 ret = drm_property_add_enum(property, i,
2580 props[i].type,
2581 props[i].name);
2582 if (ret) {
2583 drm_property_destroy(dev, property);
2584 return NULL;
2585 }
2586 }
2587
2588 return property;
2589}
2590EXPORT_SYMBOL(drm_property_create_enum);
2591
d9bc3c02
SH
2592struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2593 const char *name,
2594 uint64_t min, uint64_t max)
2595{
2596 struct drm_property *property;
2597
2598 flags |= DRM_MODE_PROP_RANGE;
2599
2600 property = drm_property_create(dev, flags, name, 2);
2601 if (!property)
2602 return NULL;
2603
2604 property->values[0] = min;
2605 property->values[1] = max;
2606
2607 return property;
2608}
2609EXPORT_SYMBOL(drm_property_create_range);
2610
f453ba04
DA
2611int drm_property_add_enum(struct drm_property *property, int index,
2612 uint64_t value, const char *name)
2613{
2614 struct drm_property_enum *prop_enum;
2615
2616 if (!(property->flags & DRM_MODE_PROP_ENUM))
2617 return -EINVAL;
2618
2619 if (!list_empty(&property->enum_blob_list)) {
2620 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2621 if (prop_enum->value == value) {
2622 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2623 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2624 return 0;
2625 }
2626 }
2627 }
2628
2629 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2630 if (!prop_enum)
2631 return -ENOMEM;
2632
2633 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2634 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2635 prop_enum->value = value;
2636
2637 property->values[index] = value;
2638 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2639 return 0;
2640}
2641EXPORT_SYMBOL(drm_property_add_enum);
2642
2643void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2644{
2645 struct drm_property_enum *prop_enum, *pt;
2646
2647 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2648 list_del(&prop_enum->head);
2649 kfree(prop_enum);
2650 }
2651
2652 if (property->num_values)
2653 kfree(property->values);
2654 drm_mode_object_put(dev, &property->base);
2655 list_del(&property->head);
2656 kfree(property);
2657}
2658EXPORT_SYMBOL(drm_property_destroy);
2659
2660int drm_connector_attach_property(struct drm_connector *connector,
2661 struct drm_property *property, uint64_t init_val)
2662{
2663 int i;
2664
2665 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2666 if (connector->property_ids[i] == 0) {
2667 connector->property_ids[i] = property->base.id;
2668 connector->property_values[i] = init_val;
2669 break;
2670 }
2671 }
2672
2673 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2674 return -EINVAL;
2675 return 0;
2676}
2677EXPORT_SYMBOL(drm_connector_attach_property);
2678
2679int drm_connector_property_set_value(struct drm_connector *connector,
2680 struct drm_property *property, uint64_t value)
2681{
2682 int i;
2683
2684 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2685 if (connector->property_ids[i] == property->base.id) {
2686 connector->property_values[i] = value;
2687 break;
2688 }
2689 }
2690
2691 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2692 return -EINVAL;
2693 return 0;
2694}
2695EXPORT_SYMBOL(drm_connector_property_set_value);
2696
2697int drm_connector_property_get_value(struct drm_connector *connector,
2698 struct drm_property *property, uint64_t *val)
2699{
2700 int i;
2701
2702 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2703 if (connector->property_ids[i] == property->base.id) {
2704 *val = connector->property_values[i];
2705 break;
2706 }
2707 }
2708
2709 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2710 return -EINVAL;
2711 return 0;
2712}
2713EXPORT_SYMBOL(drm_connector_property_get_value);
2714
2715int drm_mode_getproperty_ioctl(struct drm_device *dev,
2716 void *data, struct drm_file *file_priv)
2717{
2718 struct drm_mode_object *obj;
2719 struct drm_mode_get_property *out_resp = data;
2720 struct drm_property *property;
2721 int enum_count = 0;
2722 int blob_count = 0;
2723 int value_count = 0;
2724 int ret = 0, i;
2725 int copied;
2726 struct drm_property_enum *prop_enum;
2727 struct drm_mode_property_enum __user *enum_ptr;
2728 struct drm_property_blob *prop_blob;
81f6c7f8 2729 uint32_t __user *blob_id_ptr;
f453ba04
DA
2730 uint64_t __user *values_ptr;
2731 uint32_t __user *blob_length_ptr;
2732
fb3b06c8
DA
2733 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2734 return -EINVAL;
2735
f453ba04
DA
2736 mutex_lock(&dev->mode_config.mutex);
2737 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2738 if (!obj) {
2739 ret = -EINVAL;
2740 goto done;
2741 }
2742 property = obj_to_property(obj);
2743
2744 if (property->flags & DRM_MODE_PROP_ENUM) {
2745 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2746 enum_count++;
2747 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2748 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2749 blob_count++;
2750 }
2751
2752 value_count = property->num_values;
2753
2754 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2755 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2756 out_resp->flags = property->flags;
2757
2758 if ((out_resp->count_values >= value_count) && value_count) {
81f6c7f8 2759 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
f453ba04
DA
2760 for (i = 0; i < value_count; i++) {
2761 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2762 ret = -EFAULT;
2763 goto done;
2764 }
2765 }
2766 }
2767 out_resp->count_values = value_count;
2768
2769 if (property->flags & DRM_MODE_PROP_ENUM) {
2770 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2771 copied = 0;
81f6c7f8 2772 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
f453ba04
DA
2773 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2774
2775 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2776 ret = -EFAULT;
2777 goto done;
2778 }
2779
2780 if (copy_to_user(&enum_ptr[copied].name,
2781 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2782 ret = -EFAULT;
2783 goto done;
2784 }
2785 copied++;
2786 }
2787 }
2788 out_resp->count_enum_blobs = enum_count;
2789 }
2790
2791 if (property->flags & DRM_MODE_PROP_BLOB) {
2792 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2793 copied = 0;
81f6c7f8
VS
2794 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2795 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
f453ba04
DA
2796
2797 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2798 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2799 ret = -EFAULT;
2800 goto done;
2801 }
2802
2803 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2804 ret = -EFAULT;
2805 goto done;
2806 }
2807
2808 copied++;
2809 }
2810 }
2811 out_resp->count_enum_blobs = blob_count;
2812 }
2813done:
2814 mutex_unlock(&dev->mode_config.mutex);
2815 return ret;
2816}
2817
2818static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2819 void *data)
2820{
2821 struct drm_property_blob *blob;
2822
2823 if (!length || !data)
2824 return NULL;
2825
2826 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2827 if (!blob)
2828 return NULL;
2829
2830 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2831 blob->length = length;
2832
2833 memcpy(blob->data, data, length);
2834
2835 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2836
2837 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2838 return blob;
2839}
2840
2841static void drm_property_destroy_blob(struct drm_device *dev,
2842 struct drm_property_blob *blob)
2843{
2844 drm_mode_object_put(dev, &blob->base);
2845 list_del(&blob->head);
2846 kfree(blob);
2847}
2848
2849int drm_mode_getblob_ioctl(struct drm_device *dev,
2850 void *data, struct drm_file *file_priv)
2851{
2852 struct drm_mode_object *obj;
2853 struct drm_mode_get_blob *out_resp = data;
2854 struct drm_property_blob *blob;
2855 int ret = 0;
81f6c7f8 2856 void __user *blob_ptr;
f453ba04 2857
fb3b06c8
DA
2858 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2859 return -EINVAL;
2860
f453ba04
DA
2861 mutex_lock(&dev->mode_config.mutex);
2862 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2863 if (!obj) {
2864 ret = -EINVAL;
2865 goto done;
2866 }
2867 blob = obj_to_blob(obj);
2868
2869 if (out_resp->length == blob->length) {
81f6c7f8 2870 blob_ptr = (void __user *)(unsigned long)out_resp->data;
f453ba04
DA
2871 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2872 ret = -EFAULT;
2873 goto done;
2874 }
2875 }
2876 out_resp->length = blob->length;
2877
2878done:
2879 mutex_unlock(&dev->mode_config.mutex);
2880 return ret;
2881}
2882
2883int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2884 struct edid *edid)
2885{
2886 struct drm_device *dev = connector->dev;
7466f4cc 2887 int ret = 0, size;
f453ba04
DA
2888
2889 if (connector->edid_blob_ptr)
2890 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2891
2892 /* Delete edid, when there is none. */
2893 if (!edid) {
2894 connector->edid_blob_ptr = NULL;
2895 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2896 return ret;
2897 }
2898
7466f4cc
AJ
2899 size = EDID_LENGTH * (1 + edid->extensions);
2900 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
2901 size, edid);
f453ba04
DA
2902
2903 ret = drm_connector_property_set_value(connector,
2904 dev->mode_config.edid_property,
2905 connector->edid_blob_ptr->base.id);
2906
2907 return ret;
2908}
2909EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2910
2911int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2912 void *data, struct drm_file *file_priv)
2913{
2914 struct drm_mode_connector_set_property *out_resp = data;
2915 struct drm_mode_object *obj;
2916 struct drm_property *property;
2917 struct drm_connector *connector;
2918 int ret = -EINVAL;
2919 int i;
2920
fb3b06c8
DA
2921 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2922 return -EINVAL;
2923
f453ba04
DA
2924 mutex_lock(&dev->mode_config.mutex);
2925
2926 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2927 if (!obj) {
2928 goto out;
2929 }
2930 connector = obj_to_connector(obj);
2931
2932 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2933 if (connector->property_ids[i] == out_resp->prop_id)
2934 break;
2935 }
2936
2937 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2938 goto out;
2939 }
2940
2941 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2942 if (!obj) {
2943 goto out;
2944 }
2945 property = obj_to_property(obj);
2946
2947 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2948 goto out;
2949
2950 if (property->flags & DRM_MODE_PROP_RANGE) {
2951 if (out_resp->value < property->values[0])
2952 goto out;
2953
2954 if (out_resp->value > property->values[1])
2955 goto out;
2956 } else {
2957 int found = 0;
2958 for (i = 0; i < property->num_values; i++) {
2959 if (property->values[i] == out_resp->value) {
2960 found = 1;
2961 break;
2962 }
2963 }
2964 if (!found) {
2965 goto out;
2966 }
2967 }
2968
c9fb15f6
KP
2969 /* Do DPMS ourselves */
2970 if (property == connector->dev->mode_config.dpms_property) {
2971 if (connector->funcs->dpms)
2972 (*connector->funcs->dpms)(connector, (int) out_resp->value);
2973 ret = 0;
2974 } else if (connector->funcs->set_property)
f453ba04
DA
2975 ret = connector->funcs->set_property(connector, property, out_resp->value);
2976
af901ca1 2977 /* store the property value if successful */
f453ba04
DA
2978 if (!ret)
2979 drm_connector_property_set_value(connector, property, out_resp->value);
2980out:
2981 mutex_unlock(&dev->mode_config.mutex);
2982 return ret;
2983}
2984
f453ba04
DA
2985int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2986 struct drm_encoder *encoder)
2987{
2988 int i;
2989
2990 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2991 if (connector->encoder_ids[i] == 0) {
2992 connector->encoder_ids[i] = encoder->base.id;
2993 return 0;
2994 }
2995 }
2996 return -ENOMEM;
2997}
2998EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2999
3000void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3001 struct drm_encoder *encoder)
3002{
3003 int i;
3004 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3005 if (connector->encoder_ids[i] == encoder->base.id) {
3006 connector->encoder_ids[i] = 0;
3007 if (connector->encoder == encoder)
3008 connector->encoder = NULL;
3009 break;
3010 }
3011 }
3012}
3013EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3014
4cae5b84 3015int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
f453ba04
DA
3016 int gamma_size)
3017{
3018 crtc->gamma_size = gamma_size;
3019
3020 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3021 if (!crtc->gamma_store) {
3022 crtc->gamma_size = 0;
4cae5b84 3023 return -ENOMEM;
f453ba04
DA
3024 }
3025
4cae5b84 3026 return 0;
f453ba04
DA
3027}
3028EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3029
3030int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3031 void *data, struct drm_file *file_priv)
3032{
3033 struct drm_mode_crtc_lut *crtc_lut = data;
3034 struct drm_mode_object *obj;
3035 struct drm_crtc *crtc;
3036 void *r_base, *g_base, *b_base;
3037 int size;
3038 int ret = 0;
3039
fb3b06c8
DA
3040 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3041 return -EINVAL;
3042
f453ba04
DA
3043 mutex_lock(&dev->mode_config.mutex);
3044 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3045 if (!obj) {
3046 ret = -EINVAL;
3047 goto out;
3048 }
3049 crtc = obj_to_crtc(obj);
3050
3051 /* memcpy into gamma store */
3052 if (crtc_lut->gamma_size != crtc->gamma_size) {
3053 ret = -EINVAL;
3054 goto out;
3055 }
3056
3057 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3058 r_base = crtc->gamma_store;
3059 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3060 ret = -EFAULT;
3061 goto out;
3062 }
3063
3064 g_base = r_base + size;
3065 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3066 ret = -EFAULT;
3067 goto out;
3068 }
3069
3070 b_base = g_base + size;
3071 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3072 ret = -EFAULT;
3073 goto out;
3074 }
3075
7203425a 3076 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
f453ba04
DA
3077
3078out:
3079 mutex_unlock(&dev->mode_config.mutex);
3080 return ret;
3081
3082}
3083
3084int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3085 void *data, struct drm_file *file_priv)
3086{
3087 struct drm_mode_crtc_lut *crtc_lut = data;
3088 struct drm_mode_object *obj;
3089 struct drm_crtc *crtc;
3090 void *r_base, *g_base, *b_base;
3091 int size;
3092 int ret = 0;
3093
fb3b06c8
DA
3094 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3095 return -EINVAL;
3096
f453ba04
DA
3097 mutex_lock(&dev->mode_config.mutex);
3098 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3099 if (!obj) {
3100 ret = -EINVAL;
3101 goto out;
3102 }
3103 crtc = obj_to_crtc(obj);
3104
3105 /* memcpy into gamma store */
3106 if (crtc_lut->gamma_size != crtc->gamma_size) {
3107 ret = -EINVAL;
3108 goto out;
3109 }
3110
3111 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3112 r_base = crtc->gamma_store;
3113 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3114 ret = -EFAULT;
3115 goto out;
3116 }
3117
3118 g_base = r_base + size;
3119 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3120 ret = -EFAULT;
3121 goto out;
3122 }
3123
3124 b_base = g_base + size;
3125 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3126 ret = -EFAULT;
3127 goto out;
3128 }
3129out:
3130 mutex_unlock(&dev->mode_config.mutex);
3131 return ret;
3132}
d91d8a3f
KH
3133
3134int drm_mode_page_flip_ioctl(struct drm_device *dev,
3135 void *data, struct drm_file *file_priv)
3136{
3137 struct drm_mode_crtc_page_flip *page_flip = data;
3138 struct drm_mode_object *obj;
3139 struct drm_crtc *crtc;
3140 struct drm_framebuffer *fb;
3141 struct drm_pending_vblank_event *e = NULL;
3142 unsigned long flags;
3143 int ret = -EINVAL;
3144
3145 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3146 page_flip->reserved != 0)
3147 return -EINVAL;
3148
3149 mutex_lock(&dev->mode_config.mutex);
3150 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3151 if (!obj)
3152 goto out;
3153 crtc = obj_to_crtc(obj);
3154
90c1efdd
CW
3155 if (crtc->fb == NULL) {
3156 /* The framebuffer is currently unbound, presumably
3157 * due to a hotplug event, that userspace has not
3158 * yet discovered.
3159 */
3160 ret = -EBUSY;
3161 goto out;
3162 }
3163
d91d8a3f
KH
3164 if (crtc->funcs->page_flip == NULL)
3165 goto out;
3166
3167 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3168 if (!obj)
3169 goto out;
3170 fb = obj_to_fb(obj);
3171
3172 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3173 ret = -ENOMEM;
3174 spin_lock_irqsave(&dev->event_lock, flags);
3175 if (file_priv->event_space < sizeof e->event) {
3176 spin_unlock_irqrestore(&dev->event_lock, flags);
3177 goto out;
3178 }
3179 file_priv->event_space -= sizeof e->event;
3180 spin_unlock_irqrestore(&dev->event_lock, flags);
3181
3182 e = kzalloc(sizeof *e, GFP_KERNEL);
3183 if (e == NULL) {
3184 spin_lock_irqsave(&dev->event_lock, flags);
3185 file_priv->event_space += sizeof e->event;
3186 spin_unlock_irqrestore(&dev->event_lock, flags);
3187 goto out;
3188 }
3189
7bd4d7be 3190 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
d91d8a3f
KH
3191 e->event.base.length = sizeof e->event;
3192 e->event.user_data = page_flip->user_data;
3193 e->base.event = &e->event.base;
3194 e->base.file_priv = file_priv;
3195 e->base.destroy =
3196 (void (*) (struct drm_pending_event *)) kfree;
3197 }
3198
3199 ret = crtc->funcs->page_flip(crtc, fb, e);
3200 if (ret) {
3201 spin_lock_irqsave(&dev->event_lock, flags);
3202 file_priv->event_space += sizeof e->event;
3203 spin_unlock_irqrestore(&dev->event_lock, flags);
3204 kfree(e);
3205 }
3206
3207out:
3208 mutex_unlock(&dev->mode_config.mutex);
3209 return ret;
3210}
eb033556
CW
3211
3212void drm_mode_config_reset(struct drm_device *dev)
3213{
3214 struct drm_crtc *crtc;
3215 struct drm_encoder *encoder;
3216 struct drm_connector *connector;
3217
3218 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3219 if (crtc->funcs->reset)
3220 crtc->funcs->reset(crtc);
3221
3222 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3223 if (encoder->funcs->reset)
3224 encoder->funcs->reset(encoder);
3225
3226 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3227 if (connector->funcs->reset)
3228 connector->funcs->reset(connector);
3229}
3230EXPORT_SYMBOL(drm_mode_config_reset);
ff72145b
DA
3231
3232int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3233 void *data, struct drm_file *file_priv)
3234{
3235 struct drm_mode_create_dumb *args = data;
3236
3237 if (!dev->driver->dumb_create)
3238 return -ENOSYS;
3239 return dev->driver->dumb_create(file_priv, dev, args);
3240}
3241
3242int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3243 void *data, struct drm_file *file_priv)
3244{
3245 struct drm_mode_map_dumb *args = data;
3246
3247 /* call driver ioctl to get mmap offset */
3248 if (!dev->driver->dumb_map_offset)
3249 return -ENOSYS;
3250
3251 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3252}
3253
3254int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3255 void *data, struct drm_file *file_priv)
3256{
3257 struct drm_mode_destroy_dumb *args = data;
3258
3259 if (!dev->driver->dumb_destroy)
3260 return -ENOSYS;
3261
3262 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3263}
248dbc23
DA
3264
3265/*
3266 * Just need to support RGB formats here for compat with code that doesn't
3267 * use pixel formats directly yet.
3268 */
3269void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3270 int *bpp)
3271{
3272 switch (format) {
04b3924d
VS
3273 case DRM_FORMAT_RGB332:
3274 case DRM_FORMAT_BGR233:
248dbc23
DA
3275 *depth = 8;
3276 *bpp = 8;
3277 break;
04b3924d
VS
3278 case DRM_FORMAT_XRGB1555:
3279 case DRM_FORMAT_XBGR1555:
3280 case DRM_FORMAT_RGBX5551:
3281 case DRM_FORMAT_BGRX5551:
3282 case DRM_FORMAT_ARGB1555:
3283 case DRM_FORMAT_ABGR1555:
3284 case DRM_FORMAT_RGBA5551:
3285 case DRM_FORMAT_BGRA5551:
248dbc23
DA
3286 *depth = 15;
3287 *bpp = 16;
3288 break;
04b3924d
VS
3289 case DRM_FORMAT_RGB565:
3290 case DRM_FORMAT_BGR565:
248dbc23
DA
3291 *depth = 16;
3292 *bpp = 16;
3293 break;
04b3924d
VS
3294 case DRM_FORMAT_RGB888:
3295 case DRM_FORMAT_BGR888:
3296 *depth = 24;
3297 *bpp = 24;
3298 break;
3299 case DRM_FORMAT_XRGB8888:
3300 case DRM_FORMAT_XBGR8888:
3301 case DRM_FORMAT_RGBX8888:
3302 case DRM_FORMAT_BGRX8888:
248dbc23
DA
3303 *depth = 24;
3304 *bpp = 32;
3305 break;
04b3924d
VS
3306 case DRM_FORMAT_XRGB2101010:
3307 case DRM_FORMAT_XBGR2101010:
3308 case DRM_FORMAT_RGBX1010102:
3309 case DRM_FORMAT_BGRX1010102:
3310 case DRM_FORMAT_ARGB2101010:
3311 case DRM_FORMAT_ABGR2101010:
3312 case DRM_FORMAT_RGBA1010102:
3313 case DRM_FORMAT_BGRA1010102:
248dbc23
DA
3314 *depth = 30;
3315 *bpp = 32;
3316 break;
04b3924d
VS
3317 case DRM_FORMAT_ARGB8888:
3318 case DRM_FORMAT_ABGR8888:
3319 case DRM_FORMAT_RGBA8888:
3320 case DRM_FORMAT_BGRA8888:
248dbc23
DA
3321 *depth = 32;
3322 *bpp = 32;
3323 break;
3324 default:
3325 DRM_DEBUG_KMS("unsupported pixel format\n");
3326 *depth = 0;
3327 *bpp = 0;
3328 break;
3329 }
3330}
3331EXPORT_SYMBOL(drm_fb_get_bpp_depth);
This page took 0.558173 seconds and 5 git commands to generate.