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