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