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