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